0

我需要使用更新面板中的按钮重定向页面。但是当我尝试这个时它对我不起作用。谁可以帮我这个事??/。请在下面找到我的代码。

   <body>
     <form id="form1" runat="server">
          <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
            <asp:Label ID="Label2" runat="server" Text="Label" Width="345px"></asp:Label><br />
                    <br />    
                    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>     
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>        
                </ContentTemplate>
                <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                 <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

                </Triggers>
            </asp:UpdatePanel>
              <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="500">
                    </asp:Timer>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" Width="344px" OnClick="Button1_Click" /><br />
            <asp:Button ID="Button2" runat="server" Text="Button" />
            <br />
            <br />
            &nbsp;</div>
    </form>
              </body>

我的代码背后的代码

public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }
        protected void Button1_Click(object sender, EventArgs e)
        {


        }
        public void imgBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/HotelResult.aspx");

        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Timer1.Enabled = false;
            Label1.Text = DateTime.Now.ToString();
            Label2.Text = DateTime.Now.ToString();
            Label3.Text = DateTime.Now.ToString();
            ImageButton testbtn = new ImageButton();
            testbtn.ID = "testbtnid";
            testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
            testbtn.ImageUrl = "images/book-btn.png";
            PlaceHolder1.Controls.Add(testbtn);
        }



    }
4

2 回答 2

0

这里的问题是在计时器滴答后才ImageButton添加到页面中。如果您希望为此类动态添加的控件调用服务器端处理程序,则必须在每次回发时将 添加到页面中。以下是使用键视图状态的方法:

protected void Page_Load(object sender, EventArgs ea)
{
    bool? addHotelResultButton = ViewState["AddHotelResultButton"] as bool?;
    if (addHotelResultButton.HasValue && addHotelResultButton.Value)
    {
        AddHotelResultButton();
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    ViewState["AddHotelResultButton"] = true;
    AddHotelResultButton();
}

protected void AddHotelResultButton()
{
    Timer1.Enabled = false;
    Label1.Text = DateTime.Now.ToString();
    Label2.Text = DateTime.Now.ToString();
    Label3.Text = DateTime.Now.ToString();
    ImageButton testbtn = new ImageButton();
    testbtn.ID = "testbtnid";
    testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
    testbtn.ImageUrl = "images/heroAccent.png";
    PlaceHolder1.Controls.Add(testbtn);
}
于 2013-08-09T07:54:52.043 回答
-2

我会这样做:

  • 将您的按钮放入您的 updatePanel 并禁用它

  • 将其作为触发器放在<triggers>标签内

  • 当滴答事件到来时,启用您的按钮

于 2013-08-09T07:45:46.717 回答