Skip to main content
 首页 » 编程设计

c#之在 gridview 中,updatepanel 不工作

2024年02月13日14emanlee

html代码

<asp:GridView ID="gridprodlist" runat="server" AutoGenerateColumns="False" EmptyDataText="No record" Width="100%"> 
<Columns> 
<asp:TemplateField HeaderText="Edit&lt;br/&gt;Delete&lt;br/&gt;Disable"> 
<ItemTemplate> 
<table cellpadding="0" cellspacing="0" border="0"> 
     <tr> 
        <td valign="top"> 
         <asp:UpdatePanel ID="UpdatePanel100" runat="server"> 
            <ContentTemplate> 
        <asp:CheckBox ID="chkdis" runat="server" AutoPostBack="True" OnCheckedChanged="chkdis_CheckedChanged" ValidationGroup='<%# Eval("pid") %>' Text="Disable" /> 
            </ContentTemplate> 
          </asp:UpdatePanel> 
         </td> 
    </tr> 
</table> 
</ItemTemplate> 
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 

.cs代码

protected void chkdis_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox chkdis = (CheckBox)sender; 
    int id = Convert.ToInt32(((CheckBox)sender).ValidationGroup); 
 
    if (chkdis.Checked == true) 
    { 
 
    } 
    else 
    { 
 
    } 
 
} 

我已经将 updatepanel 放在 gridview 中,但仍然在单击我的复选框时我的页面得到刷新。 我应该怎么办? 我不希望我的页面刷新

请您参考如下方法:

尝试从代码隐藏中将此 CheckBox 注册为异步回发控件:

protected void Page_Init(Object sender, EventArgs e) 
{ 
     gridprodlist.DataBound += GridprodList_DataBound; 
}   
 
private void GridprodList_DataBound(Object sender, EventArgs e) 
{ 
    ScriptManager sm = ScriptManager.GetCurrent(Page); 
    foreach(GridViewRow row in gridprodlist.Rows) 
    { 
        CheckBox chkdis = (CheckBox) row.FindControl("chkdis"); 
        sm.RegisterAsyncPostBackControl(chkdis); 
    } 
} 

its working for first time only but on second time again my page gets refresh

然后尝试使用 RowCreated,它在每一行的每次回发时触发:

ScriptManager sm = ScriptManager.GetCurrent(Page); 
 
private void GridprodList_RowCreated(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
        CheckBox chkdis = (CheckBox) e.Row.FindControl("chkdis"); 
        sm.RegisterAsyncPostBackControl(chkdis); 
    } 
}