AutopostBack for a TextBox

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,
 
G

Guest

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.
 
G

Guest

(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();
}
 
G

Guest

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();
}
 
M

Matt Berther

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top