Answered by:
How to update row when click on new row in gridview?

Question
-
User-405684302 posted
I have a gridview which shows all the values in textboxes when gridview load.
asp.net gridview
I have a gridview which shows all the values in textboxes when gridview load.
<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:TextBox ID="txtaccountname" runat="server"
Text='<%#Eval("AccountName")%>' Width ="475px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Sub Account">
<ItemTemplate>
<asp:TextBox ID="txtsubaccount" runat="server"
Text='<%#Eval("SubAccount")%>' Width ="475px" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType ="Button" ShowDeleteButton="true"
HeaderText="Delete"/>
</Columns>
</asp:GridView>
I want that when user enter values in row textboxes and when click on other row then ask user popup you want to update this row yes or no . If user click on yes then row updated in the database otherwise not. Now how can i get when user click on new row and update previous one with confirmation message box??Wednesday, April 24, 2019 3:04 AM
Answers
-
User-893317190 posted
Hi Hameed_Khan ,
I'm sorry , I only checked AccountName and forget to check SubAccount.
If you want to check both AccountName and SubAccount checkbox , you could try the code below.
<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:TextBox ID="txtaccountname" runat="server" data-index='<%# Container.DataItemIndex %>' Text='<%#Eval("AccountName")%>' Width="475px" /> </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>
Code behind , if you want to get AccountName and SubAccount checkbox , since you could get gridviewrow in code behind , you could use findControl to find them.
protected void Button1_Click(object sender, EventArgs e) { // here you could get the previoud gridview row and save the data GridViewRow row = gvchild.Rows[Convert.ToInt32(HiddenField1.Value)]; TextBox account =row.FindControl("txtaccountname") as TextBox; TextBox subaccount = row.FindControl("txtsubaccount") as TextBox; Response.Write(account.Text + ":" + subaccount.Text); }
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2019 1:24 AM
All replies
-
User-893317190 posted
Hi Hameed_Khan ,
You could use jquery to register change event of your textbox to record previous row index.
And then you could register focus event of textbox , when user inputs the next box, you could pop up confirmation message.
If the user needs to update , you could send previous row index to server and let server to update the row.
Below is my code.
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> </head> <body> <form id="form1" runat="server"> <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>
<!--store the row index in textbox--> <asp:TextBox ID="txtaccountname" runat="server" data-index='<%# Container.DataItemIndex %>' Text='<%#Eval("AccountName")%>' Width="475px" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Sub Account"> <ItemTemplate> <asp:TextBox ID="txtsubaccount" runat="server" 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) { // 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> </form>Code behind.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable table = new DataTable(); table.Columns.Add(new DataColumn("AccountID", typeof(int))); table.Columns.Add(new DataColumn("AccountName", typeof(string))); table.Columns.Add(new DataColumn("SubAccount", typeof(string))); table.Rows.Add(1, "", "sub1"); table.Rows.Add(2, "", "sub2"); table.Rows.Add(3, "", "sub3"); gvchild.DataSource = table; gvchild.DataBind(); } } protected void gvchild_RowDeleting(object sender, GridViewDeleteEventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { // here you could get the previoud gridview row and save the data in database Response.Write(gvchild.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[0].Text); }
The result.
Best regards,
Ackerly Xu
Wednesday, April 24, 2019 8:03 AM -
User-405684302 posted
Hi,
Thanks for response. I have tried, but your code not workingWednesday, April 24, 2019 1:38 PM -
User-405684302 posted
Now i am getting Response.Write(gvchild.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[0].Text);
Empty value ,i want to get updated values in textnox of Account Name and Subaccount?Wednesday, April 24, 2019 2:53 PM -
User-893317190 posted
Hi Hameed_Khan ,
I'm sorry , I only checked AccountName and forget to check SubAccount.
If you want to check both AccountName and SubAccount checkbox , you could try the code below.
<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:TextBox ID="txtaccountname" runat="server" data-index='<%# Container.DataItemIndex %>' Text='<%#Eval("AccountName")%>' Width="475px" /> </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>
Code behind , if you want to get AccountName and SubAccount checkbox , since you could get gridviewrow in code behind , you could use findControl to find them.
protected void Button1_Click(object sender, EventArgs e) { // here you could get the previoud gridview row and save the data GridViewRow row = gvchild.Rows[Convert.ToInt32(HiddenField1.Value)]; TextBox account =row.FindControl("txtaccountname") as TextBox; TextBox subaccount = row.FindControl("txtsubaccount") as TextBox; Response.Write(account.Text + ":" + subaccount.Text); }
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2019 1:24 AM -
User-405684302 posted
Thank You sooo muchhhhhhhh !
You great. My issue is resolvedSaturday, April 27, 2019 8:27 AM