locked
How to update row when click on new row in gridview? RRS feed

  • Question

  • User-405684302 posted

    Hi,,

    I have gridview which contains dropdownlist and textbox in each row, I want that when i click on new row then previous row value update with confirmation message if previous row value change, remember when i click on new row.

    With textboxes in each row its working fine but i have place dropdownlist , but now its not working.

    Can any one help me please.?

        

    <asp:GridView ID="gvchild" CssClass="gvv" DataKeyNames="AccountID"
                OnRowDeleting="gvchild_RowDeleting" runat="server"
                AutoGenerateColumns="false" class="table table-striped"
                
                Width="100%">
                <Columns>
                    <asp:BoundField DataField="AccountID" ReadOnly="true"
                        HeaderText="Account ID" />
                    <asp:TemplateField HeaderText="Account Name">
                        <ItemTemplate>
                            <asp:DropDownList runat="server" ID="ddlaccount" data-index='<%# Container.DataItemIndex %>'  >
                                   <asp:ListItem Value ="Office Expense">Office Expense</asp:ListItem>
                                     <asp:ListItem Value ="Rent">Rent</asp:ListItem>
                                     <asp:ListItem Value ="Utilities">Utilities</asp:ListItem>
                                     <asp:ListItem Value ="Insurance">Insurance</asp:ListItem>
                                     <asp:ListItem Value ="Security">Security</asp:ListItem>
                               </asp:DropDownList>
    
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Sub Account">
                        <ItemTemplate>
                            <asp:TextBox ID="txtsubaccount" runat="server" data-index='<%# Container.DataItemIndex %>'
                                Text='<%#Eval("SubAccount")%>' Width="475px" />
                        </ItemTemplate>
                    </asp:TemplateField>
              
                    <asp:CommandField ButtonType="Button" ShowDeleteButton="true"
                        HeaderText="Delete" />
    
                </Columns>
               
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click"  style="display:none"/>
            <asp:HiddenField ID="HiddenField1" runat="server" />
            <script>
                var index;
                $("#gvchild input[type=text]").change(function () {// record preous row's index
                 
                    index = $(this).data("index");
                })
                $("#gvchild input[type=text]").focus(function () { // wnen edit the next row
    
                    if (index !== undefined  && $(this).data("index") !=index) {
                                  // confirm 
                        
                        if (confirm("are you going to update the record, whose row index is  " + (index + 1))) {
                            // if return true,  pass data to server 
                            $("#HiddenField1").val(index);
                            $("[value=Update]").click(); 
                           
                        }
                            index = undefined;
                
                    }
                })
               
            </script>

    In code behind :

     protected void Button1_Click(object sender, EventArgs e)
            {
                int updateevent=0;
                int currentRowIndex2 = Convert.ToInt32(HiddenField1.Value);
                DropDownList tbacc = gvchild.Rows[currentRowIndex2].FindControl("ddlaccount") as DropDownList;
                TextBox tbsub = gvchild.Rows[currentRowIndex2].FindControl("txtsubaccount") as TextBox;
                // here you could get the previoud gridview row and  save the data in database
                int AccountID = Convert.ToInt32(gvchild.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[0].Text.ToString());
                string AccountName = tbacc.SelectedItem.Value.ToString();
                string SubAccount = tbsub.Text.ToString();
                 updateevent = db.UpdateAccountByAccountID(AccountID, AccountName, SubAccount);
                        if (updateevent>0)
                        {
                            
                           
                            loadChilItems(Session["QBClientID"].ToString(), Session["QBAccount"].ToString());
                        }
            }

    Its not showing confirmation message ,not working.

    Saturday, April 27, 2019 11:17 AM

Answers

  • User288213138 posted

    Hi  Hameed_Khan,


    According to your description, I tried to reproduce your question. I found out the problem was with juqery, and I successfully modified it.


    The code:

    <script>
                    var index;
                    $("#gvchild input,#gvchild select").change(function () {// record preous row's index             
                        index = $(this).data("index");
                    })
                      $("#gvchild  tr").click(function () { // wnen edit the next row
    
                    if (index !== undefined  && $(this).find("input[type=text]").data("index") !=index) {
                                  // confirm                   
                        if (confirm("are you going to update the record, whose row index is  " + (index + 1))) {
                            // if return true,  pass data to server 
                            $("#HiddenField1").val(index);
                            $("[value=Update]").click();                      
                        }
                            index = undefined;                    
                    }
                })
                </script>

    The Result:

    Best Regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 29, 2019 11:04 AM

All replies

  • User-685154102 posted

    Hi Hameed_Khan,

    In your <g class="gr_ gr_106 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="106" data-gr-id="106">Jquery</g> you are trying to access server control but you may not get the result as you are not using the ClientID property of the server control.

    So the following <g class="gr_ gr_311 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="311" data-gr-id="311">Jqueryexpressions</g> :

    $("#gvchild input[type=text]").change(function ()....
    $("#gvchild input[type=text]").focus(function () 

    should reference the <g class="gr_ gr_288 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="288" data-gr-id="288">gvchild</g> as :

    $("#<%=gvchild.ClientID %> input[type=text]").change(function ()
    $("#<%=gvchild.ClientID %> input[type=text]").focus(function () 

    Let me know if this resolves your problem.

     

    Sunday, April 28, 2019 7:37 AM
  • User-405684302 posted

    Hi,

    Thanks for response, control functions already calling properly but it works only with textboxes row. Not working with when i click on new row on dropdownlist .

    Sunday, April 28, 2019 9:09 AM
  • User288213138 posted

    Hi  Hameed_Khan,


    According to your description, I tried to reproduce your question. I found out the problem was with juqery, and I successfully modified it.


    The code:

    <script>
                    var index;
                    $("#gvchild input,#gvchild select").change(function () {// record preous row's index             
                        index = $(this).data("index");
                    })
                      $("#gvchild  tr").click(function () { // wnen edit the next row
    
                    if (index !== undefined  && $(this).find("input[type=text]").data("index") !=index) {
                                  // confirm                   
                        if (confirm("are you going to update the record, whose row index is  " + (index + 1))) {
                            // if return true,  pass data to server 
                            $("#HiddenField1").val(index);
                            $("[value=Update]").click();                      
                        }
                            index = undefined;                    
                    }
                })
                </script>

    The Result:

    Best Regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 29, 2019 11:04 AM