【问题标题】:UpdatePanel RadioButtonList AsyncPostback EventHandler LostUpdatePanel RadioButtonList 异步回发事件处理程序丢失
【发布时间】:2017-11-26 20:38:47
【问题描述】:

过去几个小时我一直在寻找,但找不到解决问题的方法。标题很罗嗦,但我认为它准确地描述了我的情况。请参阅下面的代码。我尝试在转发器的 ItemDataBound 事件上添加 SelectedIndexChanged 函数,但似乎回发事件会导致页面忘记处理程序。使用 Triggers.Add 添加 AsyncPostbackTrigger 也是如此。

有效的解决方案是在回发时重新添加 EventHandler,但这对我来说不是一个可行的解决方案。我可以编写一个自定义函数来循环通过我的转发器 DataSource 独立于实际的 DataBinding,然后在适用时专门重新绑定 EventHandler,但这似乎是一种非常暴力的方法。有没有更好的方法来实现我的目标?

前端:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="smMain" runat="server" />
        <div>
            <asp:UpdatePanel ID="updTest" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Repeater ID="rptTest" runat="server" OnItemDataBound="rptTest_ItemDataBound">
                        <ItemTemplate>
                            <asp:RadioButtonList ID="rblTest" runat="server" RepeatDirection="Horizontal" ClientIDMode="AutoID" />
                        </ItemTemplate>
                    </asp:Repeater>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

后端:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Test");

            DataRow dr = dt.NewRow();

            dr["Test"] = "FirstRow";

            dt.Rows.Add(dr);

            dr = dt.NewRow();

            dr["Test"] = "SecondRow";

            dt.Rows.Add(dr);

            rptTest.DataSource = dt;
            rptTest.DataBind();
        }
        else
        {
            // This works, but it's very, very ugly. Also, I have no way of being able to test for which function I actually want to call!
            ((RadioButtonList)FindControl(Request.Params["__EVENTTARGET"])).SelectedIndexChanged += testFunction;
        }
    }

    protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("rblTest");

        DataTable dt = new DataTable();

        dt.Columns.Add("ButtonName");
        dt.Columns.Add("ButtonID");

        DataRow dr = dt.NewRow();

        dr["ButtonName"] = "Yes";
        dr["ButtonID"] = "1";

        dt.Rows.Add(dr);

        dr = dt.NewRow();

        dr["ButtonName"] = "No";
        dr["ButtonID"] = "2";

        dt.Rows.Add(dr);

        rbl.DataTextField = "ButtonName";
        rbl.DataValueField = "ButtonID";
        rbl.DataSource = dt;
        rbl.DataBind();

        rbl.AutoPostBack = true;
        //This doesn't work. Even though it's set here, it doesn't "remember" the setting on postback
        //rbl.SelectedIndexChanged += testFunction;
        //This doesn't work either. Again, it doesn't "remember" the setting on postback
        //updTest.Triggers.Add(new AsyncPostBackTrigger() { ControlID = rbl.UniqueID });
    }

    private void testFunction(object sender, EventArgs e)
    {

    }
}

【问题讨论】:

    标签: c# updatepanel postback repeater eventhandler


    【解决方案1】:

    找到问题的解决方案。本质上,我需要将 EventHandler 绑定到 ItemCreated 事件,而不是 ItemDataBound 事件。每次回发都会触发 ItemCreated 事件,而不仅仅是第一次,因此可以正确地重新绑定 EventHandler。

    前端:

    <asp:Repeater ID="rptTest" runat="server" OnItemCreated="rptTest_ItemCreated" OnItemDataBound="rptTest_ItemDataBound">
    

    后端:

    protected void rptTest_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("rblTest");
    
        rbl.SelectedIndexChanged += testFunction;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2022-09-26
      相关资源
      最近更新 更多