How to refer a text box in gridview

18 views
Skip to first unread message

Sreenivas

unread,
Sep 4, 2008, 5:44:25 AM9/4/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,
my program contains one gridview .gridview has dropdown list and
text as two columns.
drop down contains only 2 items..that are "Internal" and "External".If
select Internal the Text box
should be set to invisible...
Here goes my code..
=============================================================
<asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="False"
OnRowCancelingEdit="gvNews_RowCancelingEdit"
OnRowEditing="gvNews_RowEditing" AllowPaging="True"
OnRowUpdating="gvNews_RowUpdating"
OnPageIndexChanging="gvNews_PageIndexChanging"
CssClass="bdytxt" BorderColor="LightSteelBlue"
CellPadding="3" OnRowDataBound="gvNews_RowDataBound">
<HeaderStyle CssClass="menutxt" BackColor="SteelBlue" /
>
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblNewsId" runat="server"
Text='<%#Bind("NewsId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server"
Text='<%#Bind("Title")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server"
Text='<%#Eval("Title")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rvTitle"
ControlToValidate="txtTitle" runat="server"
ErrorMessage="Title required">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription"
runat="server" Text='<%#Bind("Description")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription"
runat="server" TextMode="MultiLine" Text='<%# Eval("Description")%>'></
asp:TextBox>
<asp:RequiredFieldValidator
ID="rvDescription" runat="server" ControlToValidate="txtDescription"
ErrorMessage="Description Required">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="lblType" runat="server"
Text='<%#Bind("Type")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddType"
AutoPostBack="true" DataSource='<%#GetType()%>' DataTextField="Type"
DataValueField="Type" runat="server"
SelectedValue='<%#Bind("Type")%>'
OnSelectedIndexChanged="ddType_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="URL">
<ItemTemplate>
<asp:Label ID="lblURL" runat="server"
Text='<%#Bind("URL")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtURL" runat="server"
Text='<%#Eval("URL")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MM\DD\YY Time">
<ItemTemplate>
<asp:Label ID="lblNewsDate" runat="server"
Text='<%#Bind("NewsDate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:ImageButton ID="lbUpdate"
runat="server" CommandName="Update" ImageUrl="~/Images/icon-
floppy.gif">
</asp:ImageButton>
<asp:ImageButton ID="lbCancel"
runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-pencil-
x.gif">
</asp:ImageButton>
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="lbEdit"
runat="server" CommandName="Edit" ImageUrl="~/Images/icon-pencil.gif">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
========================================================================
protected void ddType_SelectedIndexChanged(object sender, EventArgs
e)
{
string value = ((DropDownList)sender).SelectedValue;
if (value == "Internal")
{

TextBox txtUrl = (TextBox)(gvNews.FindControl("txtURL"));
// TextBox txtUrl = (TextBox)
((GridView)sender).FindControl("txtURL");
// txtUrl.Visible = false;

}
}

Now the in the above function ..I am unable to refer Text box(txtURL)
I am getting null reference exception...
I am a newbie..if i am doing any thing wrong ..kindly execuse me..
Can anybody has work around ..
Help me out..
Thanks in advance..

Joeizy

unread,
Sep 5, 2008, 11:30:46 AM9/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
So the problem is that the grid view's children tree is very, very
complex. FindControl isn't smart enough to search through it all, and
even if it was, it wouldn't know which row you were talking about.
When you bind the data grid you could potentially have 100s of rows,
there would be no way for FindControl to know which row you were
talking about.

This is the solution:
<EditItemTemplate>
<asp:TextBox ID="txtURL" runat="server"
Text='<%#Eval("URL")%>' Visible='<%# Eval("Type").ToString() !=
"Internal" %>'></asp:TextBox>
</EditItemTemplate>

Please excuse any syntax errors, I typed this in gmail (the syntax
highlighting, and C# compiler in gmail/goolge chrome isn't so great...
haha).
There are a couple of other ways that you could approach this problem,
but I think this will be the most succinct, and clean way of doing it.
Everything else I can think of requires more code.

On Sep 4, 3:44 am, Sreenivas <thatiparthysreeni...@gmail.com> wrote:
> Hi,
>      my program contains onegridview.gridviewhas dropdown list and
> text  as two columns.
> drop down contains only 2 items..that are "Internal" and "External".If
> select Internal the Text box
> should be set to invisible...
> Here goes my code..
> =============================================================
> <asp:GridViewID="gvNews" runat="server" AutoGenerateColumns="False"
> OnRowCancelingEdit="gvNews_RowCancelingEdit"
>                 OnRowEditing="gvNews_RowEditing" AllowPaging="True"
> OnRowUpdating="gvNews_RowUpdating"
>                 OnPageIndexChanging="gvNews_PageIndexChanging"
> CssClass="bdytxt" BorderColor="LightSteelBlue"
>                 CellPadding="3" OnRowDataBound="gvNews_RowDataBound">
>                 <HeaderStyle CssClass="menutxt" BackColor="SteelBlue" /
>
>                 <Columns>
>                     <asp:TemplateField HeaderText="Id">
>                         <ItemTemplate>
>                             <asp:Label ID="lblNewsId" runat="server"
> Text='<%#Bind("NewsId")%>'></asp:Label>
>                         </ItemTemplate>
>                     </asp:TemplateField>
>                     <asp:TemplateField HeaderText="Title">
>                         <ItemTemplate>
>                             <asp:Label ID="lblTitle" runat="server"
> Text='<%#Bind("Title")%>'></asp:Label>
>                         </ItemTemplate>
>                         <EditItemTemplate>
>                             <asp:TextBoxID="txtTitle" runat="server"
> Text='<%#Eval("Title")%>'></asp:TextBox>
>                             <asp:RequiredFieldValidator ID="rvTitle"
> ControlToValidate="txtTitle" runat="server"
>                             ErrorMessage="Title required">
>                             </asp:RequiredFieldValidator>
>                         </EditItemTemplate>
>                     </asp:TemplateField>
>                     <asp:TemplateField HeaderText="Description">
>                         <ItemTemplate>
>                             <asp:Label ID="lblDescription"
> runat="server" Text='<%#Bind("Description")%>'>
>                             </asp:Label>
>                         </ItemTemplate>
>                         <EditItemTemplate>
>                             <asp:TextBoxID="txtDescription"
>                             <asp:TextBoxID="txtURL" runat="server"
>            //TextBoxtxtUrl = (TextBox)
Reply all
Reply to author
Forward
0 new messages