1

I have the following control on my ASPX page:

<asp:TextBox ID="txtISBN13" runat="server" ClientIDMode="Static" />

I need to access this element without referring to it explicitly. Because this control does not yet exist during the Page_Load event, I have to access it later in the Page Life Cycle.

I've tried overriding the OnUnload event, because this is the final event, but I still can't access my control by either:

Control c = new Control();
c.FindControl("txtISBN13");

Or:

Control c = Page.FindControl("txtISBN13");

Am I overriding the incorrect event? Is ClientIDMode screwing with me? Even if I try similar code on a button click event, I get no luck. Do I need some kind of recursive FindControl? Should I be using Page.FindControl or Control.FindControl?

For this situation, I am unable to do:

this.txtISBN13.Text = "Foo";

Instead, I'll have a DataColumn (previousValue), and I need to find that control and set it's value. I'm trying:

string pendingID = String.Format("txt{0}", previousValue.ColumnName);

TextBox txt = new TextBox();
txt.ID = pendingID;
txt.Text = "Foo";
Phil Scholtes
  • 409
  • 2
  • 9
  • 22
  • What is the container for the textbox? Is it in a repeater, user control, gridview, etc.? If it's directly on the webform and *not* contained in something else, it should be directly accessible. – Anthony Pegram Oct 21 '11 at 17:21
  • The control is not nested, just on the form. – Phil Scholtes Oct 21 '11 at 17:28
  • OK. Perhaps I'm missing something. It now looks like you are dynamically adding the control to the page, you do not actually have the markup from the top of your question on your form. This is not a trivial detail. – Anthony Pegram Oct 21 '11 at 17:57
  • The control exists, but I want to dynamically get to it from a DataColumn that has a ColumnName equal to the textbox ID (more or less). I'm now trying to loop through the Page.Controls collection, but to make matters worse, I'm using a master page. – Phil Scholtes Oct 21 '11 at 18:05
  • 1
    OK. I think I see what you're asking. Regarding the actual problem, I don't have a full answer at the moment. But for the event, I would say if you need something *after* Load, use PreRender. By the time Unload fires, you can't affect the content that gets sent to the user. Try using PreRender, do `TextBox tb = (TextBox)this.FindControl("theConstructedTextboxName"); tb.Text = "Foo";` and see where that gets you. – Anthony Pegram Oct 21 '11 at 18:18

2 Answers2

0

Assuming that the inputs are in some sort of container (PlaceHolder, Panel, etc.) you should be able to do this:

Markup:

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:TextBox ID="txtISBN1" runat="server" />
    <asp:TextBox ID="txtISBN2" runat="server" />
    ....
    <asp:TextBox ID="txtISBN13" runat="server" />
</asp:PlaceHolder>

Code-behind:

TextBox txt = PlaceHolder1.Controls.OfType<TextBox>().FirstOrDefault(x => x.ID.ToUpper().Contains("ISBN13"));
if (txt != null)
{
    txt.Text = row.Field<string>("ISBN13");
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • My problem is that when I need to edit this textbox, I'll only have a DataColumn. The ColumnName will be "ISBN13" and from that, I'll need to construct the ID by doing String.Format("txt{0}",ColumnName). Then, I need to get at the control from that ID value. Make sense? – Phil Scholtes Oct 21 '11 at 17:30
  • Are you creating the control dynamically, or is it declared in your markup? – James Johnson Oct 21 '11 at 17:37
  • If the TextBox is declared in the markup, you should be able to reference it directly by the ID, i.e. `txtISBN13.Text = "foo";` – James Johnson Oct 21 '11 at 17:42
  • James, not that simple for this case. See my updated question for more detail. – Phil Scholtes Oct 21 '11 at 17:50
  • Okay, your question is very misleading. You are creating the control dynamically. Remove the markup from your question. – James Johnson Oct 21 '11 at 17:55
  • Not dynamically creating the control, but dynamically accessing the control. – Phil Scholtes Oct 21 '11 at 18:06
  • @James, I think I see his deal now. He *has* defined the control, but it's a *dynamic value* that is driving the decision of what control to use. So he has txt1, txt2, txt3, etc., and he has a value of "3" which tells him to try to find "txt3". – Anthony Pegram Oct 21 '11 at 18:10
0

What container is your control in? You'll need to call containingControl.FindControl("txtISBN13"), or search recursively in the Page.Controls - C#, FindControl.

Community
  • 1
  • 1
jrummell
  • 42,637
  • 17
  • 112
  • 171