0

I have the following code in my aspx file:

<input type="text" runat="server" id="test" name="test" onBlur="OnBlurFunction()"/>

When I click a submit button:

<asp:Button runat="server" id="SubmitButton" OnClick="SubmitButton" Text="Submit" />

The function "SubmitButton" contains the following:

System.Diagnostics.Debug.WriteLine(this.FindControl("test"));

When I run this, it always prints out a blank value and I'm not sure why. If I set the value in the HTML line as follows:

<input type="text" runat="server" id="test" name="test" value="hello" onBlur="OnBlurFunction()"/>

It prints "hello" with no issues. But when I manually change the value in the text box to something else, it always to print the first value out. It's as if something isn't dynamic. Am I doing something fundamentally wrong here?

Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
Jonathan
  • 147
  • 1
  • 2
  • 10
  • the best thing to do is to FindControl and convert it into the right type i.e TextBox. Then use the Text property to grab its value. For instance: var txtCtrl = this.FindControl("test") as TextBox; if (txtCtrl != null) { Debug.WriteLine(txtCtrl.Text); } – Ahmed ilyas Jul 26 '15 at 13:45
  • or use an asp:TextBox – box86rowh Jul 26 '15 at 14:07

2 Answers2

0

Try This -

HtmlInputText tb1 = this.test;
System.Diagnostics.Debug.WriteLine(tb1.Value);

This is the thread - find control and html tags

Community
  • 1
  • 1
Atanu Roy
  • 1,384
  • 2
  • 17
  • 29
0

You must use parent control(like asp:panel or runat server div) instead of this to call FindControl method. Like this:

pnl1.FindControl("test")
Fred
  • 3,365
  • 4
  • 36
  • 57