Ryan
Ryan

Reputation: 1791

How to get the ID of <asp:TextBox>?

For example I got a <asp:FormView> and there are IDs inside. Why is that I can't get one of the ID inside the <asp:FormView>, the error shows as "The name 'commentsLabel' does not exist in the current context"

How to get the ID of one of the <asp:TextBox> that is inside <asp:FormView> ? Thank you.

Upvotes: 0

Views: 4042

Answers (3)

Jakub Januszkiewicz
Jakub Januszkiewicz

Reputation: 4418

The question isn't very clear, but I suppose this is what you have:

<asp:FormView runat="server" ID="aFormView">
  <ItemTemplate>
    <asp:Label runat="server" ID="commentsLabel"/>
  </ItemTemplate>
</asp:FormView>

and you want to access commentsLabel in the code behind of the control/page on which you have this markup.

Since FormView is a naming container, you can find the controls inside it like this:

Label commentsLabel = (Label)aFormView.FindControl("commentsLabel");

Upvotes: 1

Amir Ismail
Amir Ismail

Reputation: 3883

you can do that by FindControl method

try this

TextBox t = MyFormView.FindControl("TextBox1") as TextBox;
String textBoxID = t.ID;

Upvotes: 2

Syeda
Syeda

Reputation: 1205

If textbox is accessible on page it is very simple to get its ID. Might be you are doing mistake somewhere.

ForExample: I have a textbox and label

  <asp:Label ID="lblCode" runat="server" Text="Code"></asp:Label>
  <asp:TextBox ID="tbCode" runat="server" ></asp:TextBox>

Their IDs can be easily accessible...

        string ID = tbCode.ID;
        string LableID = lblCode.ID;

If you can explain your question little more than we can help you in right direction..

Upvotes: 0

Related Questions