OnTextChanged event for a Textbox in a DataGrid

Posted on 2005-05-13
Last Modified: 2012-06-22
I have Textbox in a DataGrid. I want to use the OnTextChanged event for that
textbox to run code:

<asp:TextBox id=quantityTextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>' ></asp:TextBox>

I would like the OnTextChanged of the above to run this Sub:

Private Sub grid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grid.UpdateCommand
      Dim productId As String = grid.DataKeys(e.Item.ItemIndex)
      Dim quantity As String = CType(e.Item.FindControl("quantityTextBox"), TextBox).Text
         ShoppingCart.UpdateProductQuantity(productId, quantity)
Can this be done? thanks
Question by:MikeMCSD
    LVL 23

    Accepted Solution

    <asp:TextBox id=quantityTextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>' OnTextChanged="TextBox_Changed" ></asp:TextBox>

    Public Sub TextBox_Changed(ByVal sender As Object,ByVal e As EventArgs)
          Dim ctl As System.Web.UI.Control = CType(sender,System.Web.UI.Control)
          Dim gridItem As DataGridItem = DirectCast(ctl.BindingContainer,DataGridItem)
          Dim args As New DataGridCommandEventArgs(gridItem,Nothing,New CommandEventArgs(Nothing,Nothing))
    End Sub

    Private Sub grid_UpdateCommand(ByVal source As Object,ByVal e As DataGridCommandEventArgs)
    End Sub
    LVL 23

    Expert Comment

    explanation is in order. I assure you there's no voodoo magic in the code =)

    In the MSDN documentation, this is said of the BindingContainer:
    "This member supports the .NET Framework infrastructure and is not intended to be used directly from your code." LOL!
    Every control has this property called BindingContainer. The intellisense does not show it but it is there. Now the Binding Container is essentially the immediate/nearest parent that implements the INamingContainer. The implementation of the INamingContainer essentially means that any child controls within that control can only be accessed via the ControlCollection and never is visible via the Page.FindControls() method.

    Hence, since this TextBox is inside a DataGridItem, the BindingContainer is the DataGridItem. We therefore can get the DataGridItem and pass it in the constructor of the DataGridCommandEventArgs and as they say, the rest is history.
    LVL 16

    Author Comment

    Excellent! works great . .  thanks . .
    I would have never figured that out in a million years!
    Job well done . .

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now