Updatepanel触发器导致完整回发而不是部分回发

本文关键字:触发器 Updatepanel | 更新日期: 2023-09-27 18:18:14

我想要部分回发(asyncpostback)而不是fullpostback。但这行不通。动态创建的复选框,其中选中或未选中的复选框会导致fullpostback但它应该是asyncpostback。这是我的代码.....

<asp:CheckBoxList ID="chkList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="chkList_SelectedIndexChanged"
                ClientIDMode="AutoID">
            </asp:CheckBoxList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Label ID="lblMessage" runat="server" Visible="false"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="chkList" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
c#代码:

private static readonly string constring = ConfigurationManager.ConnectionStrings["ConnectionStrRead"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(constring);
            SqlCommand com = new SqlCommand("Select * from Category");
            com.Connection = con;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);
            int dtRows = dt.Rows.Count;
            List<string> itemList = new List<string>();
            for (int i = 0; i < dtRows; i++)
            {
                //itemList = new List<string>();
                string item = dt.Rows[i]["CategoryName"].ToString() + "(" + dt.Rows[i]["CreateUser"].ToString() + ")";
                itemList.Add(item);
            }
            chkList.DataSource = itemList.ToArray();
            chkList.DataBind();
            con.Close();
        }
    }
 protected void chkList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMessage.Visible = true;
        lblMessage.Text = string.Empty;
        foreach (ListItem item in chkList.Items)
        {
            if (item.Selected)
            {
                lblMessage.Text += item.Text + "<br/>";
            }
        }
    }

Updatepanel触发器导致完整回发而不是部分回发

你可以检查你的scriptmanager EnablePartialRendering属性吗?必须是EnablePartialRendering="true"

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnableViewState="False" EnablePartialRendering="true" EnableScriptGlobalization="true" > </asp:ScriptManager>

如果问题不是关于你可以尝试添加AsyncPostBackTrigger在代码后面

 ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(chkList);