【问题标题】:ASP.NET Multiple trigger doesn't work properlyASP.NET 多个触发器无法正常工作
【发布时间】:2013-10-28 09:11:41
【问题描述】:

我正在使用更新面板和一些触发器来实现一个页面。

下面是我想要实现的。

  1. Timer(timer1) 每隔 10 秒更新一次 UpdatePanel(update_content)。

  2. 如果用户处理 RadioButtonList(rbl_axis), ListBox(list_point), 立即更新 UpdatePanel。

  3. 所有更新均由异步进行。

这是我的代码。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server" type="text/C#">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            div_title.InnerText= "Hello";

            ScriptManager1.RegisterAsyncPostBackControl(rbl_axis);
            ScriptManager1.RegisterAsyncPostBackControl(list_point);
            printTime("Page_Load");

        }   
    }

    protected void printTime(string message)
    {
        div_content.InnerHtml += message +": "+ DateTime.Now.ToLongTimeString() + "<br />";
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        printTime("<font color='red'>Timer</font>");
    }

    protected void rbl_axis_SelectedIndexChanged(object sender, EventArgs e)
    {
        printTime("Axis("+rbl_axis.SelectedValue+")");
    }

    protected void list_point_SelectedIndexChanged(object sender, EventArgs e)
    {
        printTime(list_point.SelectedValue);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <form id="form1" runat="server">

            <asp:RadioButtonList ID="rbl_axis" runat="server" RepeatDirection="Horizontal" 
                OnSelectedIndexChanged="rbl_axis_SelectedIndexChanged">
                <asp:ListItem Text="X" Value="X" Selected="True"></asp:ListItem>
                <asp:ListItem Text="Y" Value="Y" ></asp:ListItem>
                <asp:ListItem Text="Z" Value="Z" ></asp:ListItem>
            </asp:RadioButtonList>

            <asp:ListBox ID="list_point" runat="server" SelectionMode="Multiple" Width="100px" Height="100px"
                OnSelectedIndexChanged="list_point_SelectedIndexChanged">
                <asp:ListItem Text="Point1" Value="Point1"></asp:ListItem>
                <asp:ListItem Text="Point2" Value="Point2"></asp:ListItem>
                <asp:ListItem Text="Point3" Value="Point3"></asp:ListItem>
                <asp:ListItem Text="Point4" Value="Point4"></asp:ListItem>
                <asp:ListItem Text="Point5" Value="Point5"></asp:ListItem>
            </asp:ListBox>

            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="10000"></asp:Timer>

            <asp:UpdatePanel ID="update_content" runat="server">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                </Triggers>

                <ContentTemplate>
                    <div id="div_title" runat="server"></div>
                    <div id="div_content" runat="server"></div>
                </ContentTemplate>

            </asp:UpdatePanel>
        </form>
    </div>
</body>
</html>

它不能正常工作。

仅通过计时器更新面板更新。

如果我处理列表框和单选按钮控件,更新面板不会更新。

并且在下一个 Timer Tick 时,应用列表框和单选按钮控件的修改。

我怎样才能实现我想要的。

你能给我一些建议吗?

提前谢谢你。

【问题讨论】:

    标签: asp.net asynchronous timer


    【解决方案1】:

    您的 RadioButtonList 和 ListBox 都必须将 AutoPostBack 属性设置为 true 才能触发回发。

    您还需要将这些回发事件注册到 UpdatePanel,这与您对 Timer Click 事件所做的方式非常相似。

    例如,您的代码可能更像这样:

    <asp:RadioButtonList ID="rbl_axis" runat="server" RepeatDirection="Horizontal"
         OnSelectedIndexChanged="rbl_axis_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem Text="X" Value="X" Selected="True"></asp:ListItem>
        <asp:ListItem Text="Y" Value="Y" ></asp:ListItem>
        <asp:ListItem Text="Z" Value="Z" ></asp:ListItem>
    </asp:RadioButtonList>
    <asp:ListBox ID="list_point" runat="server" SelectionMode="Multiple" Width="100px"
        Height="100px" OnSelectedIndexChanged="list_point_SelectedIndexChanged"
        AutoPostBack="true">
        <asp:ListItem Text="Point1" Value="Point1"></asp:ListItem>
        <asp:ListItem Text="Point2" Value="Point2"></asp:ListItem>
        <asp:ListItem Text="Point3" Value="Point3"></asp:ListItem>
        <asp:ListItem Text="Point4" Value="Point4"></asp:ListItem>
        <asp:ListItem Text="Point5" Value="Point5"></asp:ListItem>
    </asp:ListBox>
    <asp:UpdatePanel ID="update_content" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            <asp:AsyncPostBackTrigger ControlID="rbl_axis" EventName="SelectedIndexChanged" />
            <asp:AsyncPostBackTrigger ControlID="list_point" EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <div id="div_title" runat="server"></div>
            <div id="div_content" runat="server"></div>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    【讨论】:

    • 非常感谢您的帮助。它运作良好。再次感谢您
    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 2015-12-02
    • 2012-01-04
    • 2019-02-24
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多