AutopostBack for a TextBox

Discussion in 'ASP .Net' started by =?Utf-8?B?Vmk=?=, Oct 28, 2004.

  1. 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,
     
    =?Utf-8?B?Vmk=?=, Oct 28, 2004
    #1
    1. Advertising

  2. Try printing out the value of sender.GetType().FullName.

    "Vi" wrote:

    > 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,
     
    =?Utf-8?B?SGFhY2tlZA==?=, Oct 28, 2004
    #2
    1. Advertising

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


    "Haacked" wrote:

    > Try printing out the value of sender.GetType().FullName.
    >
    > "Vi" wrote:
    >
    > > 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,
     
    =?Utf-8?B?Vmk=?=, Oct 28, 2004
    #3
  4. (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();
    }

    "Vi" wrote:

    > 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.
    >
    >
    > "Haacked" wrote:
    >
    > > Try printing out the value of sender.GetType().FullName.
    > >
    > > "Vi" wrote:
    > >
    > > > 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,
     
    =?Utf-8?B?SGFhY2tlZA==?=, Oct 29, 2004
    #4
  5. it worked,
    thanks a lot.

    "Haacked" wrote:

    > (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();
    > }
    >
    > "Vi" wrote:
    >
    > > 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.
    > >
    > >
    > > "Haacked" wrote:
    > >
    > > > Try printing out the value of sender.GetType().FullName.
    > > >
    > > > "Vi" wrote:
    > > >
    > > > > 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,
     
    =?Utf-8?B?Vmk=?=, Oct 29, 2004
    #5
  6. =?Utf-8?B?Vmk=?=

    Matt Berther Guest

    Hello Vi,

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

    --
    Matt Berther
    http://www.mattberther.com

    > 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,
    >
     
    Matt Berther, Oct 29, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Fidge
    Replies:
    2
    Views:
    3,548
    Ben Fidge
    Jan 30, 2004
  2. Neo
    Replies:
    1
    Views:
    490
    avnrao
    Jun 24, 2004
  3. - Steve -

    AutoPostBack on TextBox loses Focus

    - Steve -, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,545
    - Steve -
    Jun 29, 2004
  4. =?Utf-8?B?UmVuYXVkIExhbmdpcw==?=

    TextBox AutoPostBack=false question (bug?)

    =?Utf-8?B?UmVuYXVkIExhbmdpcw==?=, Jun 4, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    402
    =?Utf-8?B?UmVuYXVkIExhbmdpcw==?=
    Jun 5, 2007
  5. Replies:
    0
    Views:
    506
Loading...

Share This Page