0
votes

I have a requirement where i have to update a textbox if any of the value in my grid view changes.. I have a gridview with 2 rows ..

one a template field with label and another template field with a textbox...

my Grid view looks like

                   name                 value
                   a                     (empty textbox)
                   b                     (empty textbox)
                   c                      (empty textbox)

now when the user enters a value in teh text box it should automatically update another textbox which is linked to this.

Here my questions is when someone enters a value in the textbox an event should be fired!

(I am getting the names a,b,c, from database). i dont want to have an edit link or update link because all the values to be entered are mandatory!

i tried Grid_SelectedIndexChanged1 but this not firing.. is there something i am missing or i need to change so that the evant is fired and i can update the other textbox??

I am new to ASP.NET so please help!

Thanks in advance!

2
TextBox has an AutoPostback property set it to true and handle the TextChanged event or do it on clientside with javascript. - Tim Schmelter
Would please tell me what you mean by linking textboxes ? - Babak Fakhriloo
i tried that too.. but how to i know which rows textbox changed? - helpme
@ persian dev i have other textbox which needs to update the values i get from these feilds.. i have a text to edit with the values provided from these textbox fields! - helpme
selectedindexchanged fires when the index of the selected row changes, which hasn't occurred in this instance. if all you are doing is updating another UI element with the same text, then making a trip the server is overkill. javascript, more specifically jquery will be a much better solution. - Jason Meckley

2 Answers

2
votes

If the updates are supposed to be triggered when the text changes, you should use the OnTextChanged event of the TextBox, and set AutoPostBack to true.

EDIT

To avoid duplicating efforts here, using the above approach you can find the row index using the technique that Pankaj Garg outlined in his answer:

int rowIndex = ((GridViewRow)((TextBox)sender).NamingContainer).RowIndex;

The biggest caveat to this approach is that it's not forgiving of changes in the markup. If you were to wrap the TextBox in another control that implements INamingContainer, the example above would break. For example:

<asp:TemplateField>
    <asp:Panel ID="Panel1" runat="server"> <!-- becomes the naming container -->
        <asp:TextBox ID="TextBox1" runat="server" onchange='valueChanged(<%# Container.ItemIndex %>);' />
    </asp:Panel>
</asp:TemplateField>

That being said, you would want to notate your markup accordingly so other developers know to be careful when making changes.

EDIT

As an alternative, you could also trigger the postback in JavaScript using the onchange event of the TextBox:

<script type="text/javascript">
    valueChanged = function(rowIndex){
        __doPostBack("<%= GridView1.ClientID %>", rowIndex);
    }
</script>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" ...>
    <Columns>
        <asp:TemplateField>
            <asp:TextBox ID="TextBox1" runat="server" onchange='valueChanged(<%# Container.ItemIndex %>);' />
        </asp:TemplateField>
    </Columns>
</asp:GridView>    

In the code-behind, override the RaisePostBackEvent method, and put your update logic there:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    base.RaisePostBackEvent(source, eventArgument);

    if (source == GridView1)
    {
        int rowIndex = int.Parse(eventArgument);

        TextBox txt = GridView1.Rows[rowIndex].FindControl("TextBox1") as TextBox;
        if (txt != null)
        {
            var id = (int)GridView1.DataKeys[rowIndex]["ID"];
            var text = txt.Text.Trim();

            //update the database
        } 
    }    
}
1
votes

You can check the Current Row Index like below...

((GridViewRow)((TextBox)sender).NamingContainer).RowIndex

Create a handler for OnTextChanged event and set the AutoPostBack Property True.

protected void TextBox_TextChanged(object sender, EventArgs e)
{
     int CurrentGridIndex = ((GridViewRow)((TextBox)sender).NamingContainer).RowIndex
}