response.redirect使用计时器勾选在更新面板中不起作用

时间:2013-08-09 07:38:42

标签: c# asp.net .net ajax

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

   <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);
        }



    }

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:-2)

我会这样做:

  • 将您的按钮放在updatePanel中并将其禁用

  • 将其作为<triggers>标记

  • 中的触发器
  • 当tick事件发生时,启用按钮