Jump to content
Sign in to follow this  
Bhushan

Finding a control

Recommended Posts

Hi,I have a TextBox inside a HTML table which is inside a DataGrid.I used TextBox t=(TextBox)DetailsGrid.FindControl("txtName");Response.Write(t.Text);But its not working. Please let me know.

Share this post


Link to post
Share on other sites

Are you getting an erro or just nothing??You forgot to add a (.)

TextBox t=(TextBox).DetailsGrid.FindControl("txtName");

Share this post


Link to post
Share on other sites

Hi,When I add (.) it gave me error saying TextBox defines a class where a variable is expected. In the first place, do I have to include anything else??.I have hierarchy like this.I am facing a problem in my project where in I have made use of a Data Grid and inside it I have used a HTML table and inside <td> I am using a asp TextBox.When I tried to find out this TextBox using string str=((TextBox)e.Item.Cells[0].FindControl("txtName")).Text;Response.Write(str);It is giving me an error saying “bject reference not set to an instance of an Object”Please suggest me how can I resolve this??. I am also mailing you the piece of code which might be useful for you to pin point my mistake !. I tried here something different. But even this didn't work out. ASPX code: I have the following textbox under <table> which is inside a DataGrid. <td><asp:TextBox ID="txtName" Runat="server" ReadOnly=True Text='<%#DataBinder.Eval(Container.DataItem,"ResourceName")%>'></asp:TextBox></td> <FooterTemplate><asp:Button ID="btnEdit" Runat="server" Text="EDIT" CommandName="edit_here" /></FooterTemplate> I have a ItemCommand=”mCommandHandler” as my DataGrid property. CodeBehind: public void mCommandHandler(object sender,DataGridCommandEventArgs e) { if(e.CommandName=="edit_here") { TextBox t=(TextBox)e.Item.FindControl("txtName"); string str=t.Text; Response.Write(str); } } Please help me in getting rid of this problem.Thanks in advance,Bhushan

Share this post


Link to post
Share on other sites

why not just use

Page.FindControl("txtName");

If it exists on the page it will be found. That way you don't have to worry about searching the correct celss, etc.

Share this post


Link to post
Share on other sites

Hi ,I am catching the ItemCommand event as below and trying to find a control.public void mCommandHandler(object sender,DataGridCommandEventArgs e) { if(e.CommandName=="edit_here") { TextBox t=(TextBox)Page.FindControl("txtName"); labl.Text=t.Text; } }It's throwing me the same error saying "Object reference is not set to an instance of an Object".Do you have any Idea ??.HTML code is as follows :<asp:datagrid id="DetailsGrid" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 160px" runat="server" HeaderStyle-ForeColor="blue" HeaderStyle-Font-Bold="True" OnItemCommand="mCommandHandler" AutoGenerateColumns="False" EditItemStyle-BackColor="#cccfff" BackColor="Transparent" BorderColor="Transparent" OnCancelCommand="cancel" OnEditCommand="edithere" ShowFooter="True"> <EditItemStyle BackColor="#CCCFFF"></EditItemStyle> <HeaderStyle Font-Bold="True" Wrap="False" ForeColor="White" BackColor="Navy"></HeaderStyle> <FooterStyle BackColor="White"></FooterStyle> <Columns> <asp:TemplateColumn HeaderText="Employee Details"> <ItemTemplate> <table> <tr> <td> Name </td> <td> <asp:TextBox ID="txtName" Runat="server" ReadOnly="True" Text="Hello man"></asp:TextBox> </td> </tr> <tr> <td> NT ID </td> <td> <asp:TextBox ID="Textbox1" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SecureNTID")%>'> </asp:TextBox> </td> </tr> <tr> <td> Designation </td> <td> <asp:TextBox ID="Textbox2" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Designation")%>'> </asp:TextBox> </td> </tr> <tr> <td> Address </td> <td> <asp:TextBox ID="Textbox3" Runat="server" TextMode="MultiLine" Text='<%#DataBinder.Eval(Container.DataItem,"ResourceAddress")%>'> </asp:TextBox> </td> </tr> </table> </ItemTemplate> <FooterTemplate> <asp:Button ID="btnEdit" Runat="server" Text="EDIT" CommandName="edit_here" /> </FooterTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...