Answered by:
Dropdownlist avoid postback in c#

Question
-
User-2004582644 posted
Hi,
I have a `dropdownlist control` (ddl) on gridview.
There are these values in the ddl control "A" and "B" and "C".
I need to avoid postback of the page when selected value in `dropdownlist control` (ddl) is changed.
And this working.
But this code not working correctly, because it's no longer possible to edit the gridview line.
The `CommandName="Edit"` in gridview has stopped working after inserted of `UpdatePanel`.
Can you help me?
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="gvProducts" EventName="SelectedIndexChanged" /> </Triggers> <ContentTemplate> <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="no data" EnableViewState="true" runat="server" DataKeyNames="ID" HorizontalAlign="Center"> <AlternatingRowStyle CssClass="altrows" /> <Columns> <asp:TemplateField HeaderText="EDIT" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="/Images/Edit.gif" OnClientClick="return confirm('Confirm ?');" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="List" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"> <asp:ListItem Text="A" Value="A" /> <asp:ListItem Text="B" Value="B" /> <asp:ListItem Text="C" Value="C" /> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </asp:GridView> </ContentTemplate> </asp:UpdatePanel>
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e) { string sID = gvProducts.DataKeys[e.NewEditIndex].Value.ToString(); string queryString = "sDetails.aspx?sID=" + sID.ToString().ToUpper(); string newWin = "var Mleft = (screen.width/2)-(1200/2);var Mtop = (screen.height/2)-(700/2);window.open('" + queryString + "','_blank','height=700,width=1200,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\';');"; ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true); }
protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer); string sIDdataKey = this.gvProducts.DataKeys[gvr.RowIndex].Value.ToString(); DropDownList duty = (DropDownList)gvr.FindControl("ddl"); sql = String.Format(@" UPDATE `tbl` "); sql += String.Format(" SET `Letter`= ? "); sql += String.Format(" WHERE ID= ?; "); using (OdbcConnection cn = new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) { using (OdbcCommand command = new OdbcCommand(sql, cn)) { try { command.Connection.Open(); command.Parameters.AddWithValue("param1", duty.SelectedItem.Value.ToString()); command.Parameters.AddWithValue("param2", sIDdataKey.ToString()); command.ExecuteNonQuery(); BindData(); } catch (Exception ex) { throw ex; } finally { command.Connection.Close(); } } } }
Saturday, April 18, 2020 9:47 PM
Answers
-
User314352500 posted
Hope this can help you.
<asp:GridView ID="GridView1" AutoGenerateColumns="False" EmptyDataText="no data" EnableViewState="true" runat="server" DataKeyNames="ID" HorizontalAlign="Center"> <AlternatingRowStyle CssClass="altrows" /> <Columns> <asp:TemplateField HeaderText="EDIT" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="/Images/Edit.gif" OnClientClick="return confirm('Confirm ?');" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="List" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="gvProducts" EventName="SelectedIndexChanged" /> </Triggers> <ContentTemplate> <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"> <asp:ListItem Text="A" Value="A" /> <asp:ListItem Text="B" Value="B" /> <asp:ListItem Text="C" Value="C" /> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> </ItemTemplate> </asp:TemplateField>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 19, 2020 9:34 AM
All replies
-
User-1330468790 posted
Hi Mark Sunderland,
But this code not working correctly, because it's no longer possible to edit the gridview line.
The `CommandName="Edit"` in gridview has stopped working after inserted of `UpdatePanel`.
This problem is located in the code behind where you register startup script using below code
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e) { string sID = gvProducts.DataKeys[e.NewEditIndex].Value.ToString(); string queryString = "sDetails.aspx?sID=" + sID.ToString().ToUpper(); string newWin = "var Mleft = (screen.width/2)-(1200/2);var Mtop = (screen.height/2)-(700/2);window.open('" + queryString + "','_blank','height=700,width=1200,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\';');"; ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true); }
However, since you put the controls into the UpdatePanel control, the client-script-related part should be changed correspondingly.
You should no longer use "ClientScript" to register scripts. Instead, you should use "ScriptManager".
Solution:
Change the code as below
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e) { string sID = gvProducts.DataKeys[e.NewEditIndex].Value.ToString(); string queryString = "sDetails.aspx?sID=" + sID.ToString().ToUpper(); string newWin = "var Mleft = (screen.width/2)-(1200/2);var Mtop = (screen.height/2)-(700/2);window.open('" + queryString + "','_blank','height=700,width=1200,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\';');"; ScriptManager.RegisterStartupScript(this,this.GetType(), "pop", newWin, true); }
You may want to read the official document about Script registering for UpdatePanel.
Click this link to read more about
- When you should use RegisterStartupScript method of ScriptManager
- and when you should use RegisterStartupScript method of the ClientScriptManager class
PS: In the "Remarks" section under the "RegisterStartupScript(Page, Type, String, String, Boolean)" subtitle
Hope this can help you.
Best regards,
Sean
Sunday, April 19, 2020 6:32 AM -
User-2004582644 posted
Hello Sean, thank you for reply.
I have tried your suggestion without success because the `CommandName="Edit"` in gridview not open page for edit row of gridview
Can you help me ?
Sunday, April 19, 2020 8:24 AM -
User314352500 posted
Hope this can help you.
<asp:GridView ID="GridView1" AutoGenerateColumns="False" EmptyDataText="no data" EnableViewState="true" runat="server" DataKeyNames="ID" HorizontalAlign="Center"> <AlternatingRowStyle CssClass="altrows" /> <Columns> <asp:TemplateField HeaderText="EDIT" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="/Images/Edit.gif" OnClientClick="return confirm('Confirm ?');" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="List" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="gvProducts" EventName="SelectedIndexChanged" /> </Triggers> <ContentTemplate> <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"> <asp:ListItem Text="A" Value="A" /> <asp:ListItem Text="B" Value="B" /> <asp:ListItem Text="C" Value="C" /> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> </ItemTemplate> </asp:TemplateField>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 19, 2020 9:34 AM