AutopostBack for a TextBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I want to be able to reload a DropDownList when a TextBox changes its value.
So I set the AutopostBack property of the TextBox to true and in the code
behind I do something like:

if(IsPostBack)
{
if( ((TextBox)sender).ID == "myTextBox" )
myDropDownList.DataBind();
}

I receive an error "Specified cast is not valid" in the if(
((TextBox)sender).ID == "myTextBox" ) line.
What am I doing wrong?

Thank you,
 
sender.GetType().FullName returns "ASP.WebOrderForm_aspx", which is the
name of the page.
Is there a way to determine that it was the TextBox that generated the
postback?
Thanks.
 
(Assuming VS.NET 2003) In the designer select the TextBox control and in the
properties window, click on the yellow lightning bold. Type in OnTextChanged
next to the Action "TextChanged".

In the code behind, you should see that the method InitializeComponent has
changed to add a line that attaches an event handler to the TextChanged event
of the TextBox:

private void InitializeComponent()
{
this.TextBox1.TextChanged += new System.EventHandler(this.OnTextChanged);
this.Load += new System.EventHandler(this.Page_Load);

}

And

Now in the method called by the TextChanged event handler, you can bind your
dropdown.

private void OnTextChanged(object sender, System.EventArgs e)
{
myDropDownList.DataBind();
}
 
it worked,
thanks a lot.

Haacked said:
(Assuming VS.NET 2003) In the designer select the TextBox control and in the
properties window, click on the yellow lightning bold. Type in OnTextChanged
next to the Action "TextChanged".

In the code behind, you should see that the method InitializeComponent has
changed to add a line that attaches an event handler to the TextChanged event
of the TextBox:

private void InitializeComponent()
{
this.TextBox1.TextChanged += new System.EventHandler(this.OnTextChanged);
this.Load += new System.EventHandler(this.Page_Load);

}

And

Now in the method called by the TextChanged event handler, you can bind your
dropdown.

private void OnTextChanged(object sender, System.EventArgs e)
{
myDropDownList.DataBind();
}
 
Hello Vi,

Why would you not just catch the TextChanged event on the text box in question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top