Using AutoPostBack with a TextBox in a Repeater

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Oct 3, 2009.

  1. I have a Repeater who's ItemTemplate contains a TextBox that has
    AutoPostBack set to True. However, I am not sure where to handle this
    postback. When I have a Button in the ItemTemplate, I simply use the
    Repeater's ItemCommand event handler, but this event handler is not
    triggered for what would normally be the TextBox's TextChanged event. Can
    someone help me figure out how to handle this postback? Any help would be
    appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Oct 3, 2009
    #1
    1. Advertising

  2. Nathan Sokalski

    Patrice Guest

    Note sure to understand the problem. As you said it should be textbox
    TextChanged event... Have you tried to handle this event ? At which point
    are you stuck ? (you don't know where to start or you tried something and it
    doesn't work ?)

    BTW, which programming language do you use ? (lloks you'll need a sample)

    --
    Patrice
    "Nathan Sokalski" <> a écrit dans le message de
    news:...
    >I have a Repeater who's ItemTemplate contains a TextBox that has
    >AutoPostBack set to True. However, I am not sure where to handle this
    >postback. When I have a Button in the ItemTemplate, I simply use the
    >Repeater's ItemCommand event handler, but this event handler is not
    >triggered for what would normally be the TextBox's TextChanged event. Can
    >someone help me figure out how to handle this postback? Any help would be
    >appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Patrice, Oct 3, 2009
    #2
    1. Advertising

  3. Nathan Sokalski

    Phil Harvey Guest

    Hi Nathan,

    A simple solution to this problem is to handle the TextChanged event on the
    TextBox.

    In your repeater, set the OnTextChanged property to an event handler method
    in your code-behind.

    <asp:Repeater ID="rptData" runat="server">
    <ItemTemplate>
    <asp:TextBox runat="server" Text=<%#
    Container.DataItem.ToString() %> OnTextChanged="TextChanged"
    AutoPostBack="true" />
    </ItemTemplate>
    </asp:Repeater>

    In your code behind, have something like the following (I hope c# is ok).

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    string[] data = new string[] { "s1", "s2" };
    this.rptData.DataSource = data;
    this.rptData.DataBind();
    }
    }

    protected void TextChanged(object sender, EventArgs e)
    {
    TextBox textbox = sender as TextBox;
    Debug.WriteLine("New text: " + textbox.Text);
    }

    Remember to only Databind the repeater if the request is not a postback,
    otherwise the TextChanged won't be fired and the new value will be lost.

    Regards,

    --
    Phil Harvey
    www.anotherblog.com



    "Nathan Sokalski" wrote:

    > I have a Repeater who's ItemTemplate contains a TextBox that has
    > AutoPostBack set to True. However, I am not sure where to handle this
    > postback. When I have a Button in the ItemTemplate, I simply use the
    > Repeater's ItemCommand event handler, but this event handler is not
    > triggered for what would normally be the TextBox's TextChanged event. Can
    > someone help me figure out how to handle this postback? Any help would be
    > appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
     
    Phil Harvey, Oct 3, 2009
    #3
  4. I had thought of that, but I didn't think it would work, since the TextBox
    was inside the Repeater and was not available in the list of controls in the
    codebehind in Visual Studio. But I have tried it now, since I have been told
    that it did work, and thanks to you, it did! Yes, I guess I do have to plead
    guilty of thinking of something and not trying it, so I guess I've learned
    my lesson. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Phil Harvey" <> wrote in message
    news:...
    > Hi Nathan,
    >
    > A simple solution to this problem is to handle the TextChanged event on
    > the
    > TextBox.
    >
    > In your repeater, set the OnTextChanged property to an event handler
    > method
    > in your code-behind.
    >
    > <asp:Repeater ID="rptData" runat="server">
    > <ItemTemplate>
    > <asp:TextBox runat="server" Text=<%#
    > Container.DataItem.ToString() %> OnTextChanged="TextChanged"
    > AutoPostBack="true" />
    > </ItemTemplate>
    > </asp:Repeater>
    >
    > In your code behind, have something like the following (I hope c# is ok).
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!Page.IsPostBack)
    > {
    > string[] data = new string[] { "s1", "s2" };
    > this.rptData.DataSource = data;
    > this.rptData.DataBind();
    > }
    > }
    >
    > protected void TextChanged(object sender, EventArgs e)
    > {
    > TextBox textbox = sender as TextBox;
    > Debug.WriteLine("New text: " + textbox.Text);
    > }
    >
    > Remember to only Databind the repeater if the request is not a postback,
    > otherwise the TextChanged won't be fired and the new value will be lost.
    >
    > Regards,
    >
    > --
    > Phil Harvey
    > www.anotherblog.com
    >
    >
    >
    > "Nathan Sokalski" wrote:
    >
    >> I have a Repeater who's ItemTemplate contains a TextBox that has
    >> AutoPostBack set to True. However, I am not sure where to handle this
    >> postback. When I have a Button in the ItemTemplate, I simply use the
    >> Repeater's ItemCommand event handler, but this event handler is not
    >> triggered for what would normally be the TextBox's TextChanged event. Can
    >> someone help me figure out how to handle this postback? Any help would be
    >> appreciated. Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >>
    >>
     
    Nathan Sokalski, Oct 4, 2009
    #4
  5. Nathan Sokalski

    Enis Hyseni Guest

    Nathan Sokalski did you ever solved the problem that you had, because I`m
    stucked at that.

    If you did please tell mehow can I do that?

    "Nathan Sokalski" wrote:

    > I had thought of that, but I didn't think it would work, since the TextBox
    > was inside the Repeater and was not available in the list of controls in the
    > codebehind in Visual Studio. But I have tried it now, since I have been told
    > that it did work, and thanks to you, it did! Yes, I guess I do have to plead
    > guilty of thinking of something and not trying it, so I guess I've learned
    > my lesson. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Phil Harvey" <> wrote in message
    > news:...
    > > Hi Nathan,
    > >
    > > A simple solution to this problem is to handle the TextChanged event on
    > > the
    > > TextBox.
    > >
    > > In your repeater, set the OnTextChanged property to an event handler
    > > method
    > > in your code-behind.
    > >
    > > <asp:Repeater ID="rptData" runat="server">
    > > <ItemTemplate>
    > > <asp:TextBox runat="server" Text=<%#
    > > Container.DataItem.ToString() %> OnTextChanged="TextChanged"
    > > AutoPostBack="true" />
    > > </ItemTemplate>
    > > </asp:Repeater>
    > >
    > > In your code behind, have something like the following (I hope c# is ok).
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > if (!Page.IsPostBack)
    > > {
    > > string[] data = new string[] { "s1", "s2" };
    > > this.rptData.DataSource = data;
    > > this.rptData.DataBind();
    > > }
    > > }
    > >
    > > protected void TextChanged(object sender, EventArgs e)
    > > {
    > > TextBox textbox = sender as TextBox;
    > > Debug.WriteLine("New text: " + textbox.Text);
    > > }
    > >
    > > Remember to only Databind the repeater if the request is not a postback,
    > > otherwise the TextChanged won't be fired and the new value will be lost.
    > >
    > > Regards,
    > >
    > > --
    > > Phil Harvey
    > > www.anotherblog.com
    > >
    > >
    > >
    > > "Nathan Sokalski" wrote:
    > >
    > >> I have a Repeater who's ItemTemplate contains a TextBox that has
    > >> AutoPostBack set to True. However, I am not sure where to handle this
    > >> postback. When I have a Button in the ItemTemplate, I simply use the
    > >> Repeater's ItemCommand event handler, but this event handler is not
    > >> triggered for what would normally be the TextBox's TextChanged event. Can
    > >> someone help me figure out how to handle this postback? Any help would be
    > >> appreciated. Thanks.
    > >> --
    > >> Nathan Sokalski
    > >>
    > >> http://www.nathansokalski.com/
    > >>
    > >>
    > >>

    >
    >
    >
     
    Enis Hyseni, Oct 16, 2009
    #5
    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,594
    Ben Fidge
    Jan 30, 2004
  2. Neo
    Replies:
    1
    Views:
    519
    avnrao
    Jun 24, 2004
  3. - Steve -

    AutoPostBack on TextBox loses Focus

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

    AutopostBack for a TextBox

    =?Utf-8?B?Vmk=?=, Oct 28, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    635
    Matt Berther
    Oct 29, 2004
  5. Nathan Sokalski

    Using AutoPostBack with a TextBox in a Repeater

    Nathan Sokalski, Oct 3, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    3,789
    Enis Hyseni
    Oct 16, 2009
Loading...

Share This Page