【问题标题】:updatepanel trigger cause fullpostback instead partial postbackupdatepanel 触发器导致完全回发而不是部分回发
【发布时间】:2013-06-13 15:15:25
【问题描述】:

我想要部分回发(asyncpostback)而不是完整回发。但它不起作用。动态创建的复选框,其中选中或未选中的复选框会导致 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/>";
            }
        }
    }

【问题讨论】:

    标签: c# asp.net updatepanel asynchronous-postback


    【解决方案1】:

    你能检查你的脚本管理器 EnablePartialRendering 属性吗?它必须是 EnablePartialRendering="true"

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

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

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

    【讨论】:

    • 如果控件位于更新面板中,页面加载事件是否会触发?此处选中或取消选中复选框列表控件。
    • 你救了我一天!
    • EnablePartialRendering="true" 是 ScriptManager 的默认设置。这应该已经奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2015-07-21
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2014-07-30
    • 2011-08-04
    相关资源
    最近更新 更多