Autopostback for Textbox in a DataList

Discussion in 'ASP .Net' started by milbrand@gmail.com, Feb 2, 2009.

  1. Guest

    I have a textbox in a datalist that I need an auto postback on. To
    complicate matters, I have a javascript on this textbox as well. I am
    having two issues. 1) I am only able to get the textbox to fire the
    autopostback when the field is blank and 2) I do not know where to
    capture the autopostback in the code behind when it happens.

    Thanks for your help. Here are some examples of my code (simplified
    for this post):

    ASPX Page:
    <script type="text/javascript">
    function validatethisdecimal(i, cssclass)
    {
    if(i.value.length>0) {
    i.value = i.value.replace(/[^\d\.]+/g, '');
    }
    if(i.value.length>0) {
    i.className='normal';
    }
    else
    {
    i.className=cssclass;
    }
    return true;
    }
    </script>
    ..
    ..
    ..
    <asp:datalist id="DL_Principal" runat="server" Width="100%"
    OnItemCommand="Principal_ItemCommand"
    OnItemDataBound="Principal_DataBound">
    <ItemTemplate>
    <asp:textbox id="tb_principalname" runat="server" MaxLength="100"
    Width="200px" CssClass="required" text='<%#DataBinder.Eval
    (Container.DataItem, "PName")%>'></asp:textbox>
    <asp:textbox id="tb_perown" runat="server" MaxLength="6" Width="50px"
    CssClass="required" AutoPostBack="True" text='<%#DataBinder.Eval
    (Container.DataItem, "PPer")%>'></asp:textbox>
    </ItemTemplate>
    </asp:datalist>


    Code Behind (C#)


    public void Principal_DataBound(object sender,
    System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    if((e.Item.ItemType == ListItemType.AlternatingItem) ||
    (e.Item.ItemType == ListItemType.Item))
    {
    DataListItem dItem = e.Item;

    tb = (TextBox)dItem.FindControl("tb_perown");
    tb.Attributes.Add("onKeyup", "validatethisdecimal(this,
    'required');");

    if(tb.Text.ToString().Length > 0)
    {
    tb.CssClass = "normal";
    }
    }
    }

    //This does not fire on the autopostback
    public void Principal_ItemCommand(Object sender,
    DataListCommandEventArgs e)
    {
    DataListItem dItem = e.Item;

    if((e.Item.ItemType == ListItemType.AlternatingItem) ||
    (e.Item.ItemType == ListItemType.Item))
    {
    .
    .
    .
    }
    }

    //This also does not fire on the autopostback
    private void DL_Principal_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
    TextBox tb = (TextBox)sender;
    DataListItem dItem = (DataListItem)tb.Parent.Parent;
    .
    .
    .
    }
    , Feb 2, 2009
    #1
    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,532
    Ben Fidge
    Jan 30, 2004
  2. Neo
    Replies:
    1
    Views:
    476
    avnrao
    Jun 24, 2004
  3. - Steve -

    AutoPostBack on TextBox loses Focus

    - Steve -, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,519
    - Steve -
    Jun 29, 2004
  4. Nevyn Twyll
    Replies:
    8
    Views:
    1,833
  5. =?Utf-8?B?Vmk=?=

    AutopostBack for a TextBox

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

Share This Page