response.redirect在使用计时器刻度的更新面板中不工作
本文关键字:更新 工作 redirect 计时器 response | 更新日期: 2023-09-27 18:26:54
我需要使用更新面板中的按钮重定向页面。但当我尝试这个时,它对我不起作用。有人能帮我吗。请在下面找到我的代码。
<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 />
</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);
}
}
这里的问题是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);
}
我会这样做:
-
把你的按钮放在你的更新面板内并禁用它
-
将其作为触发器放入
<triggers>
标签中 -
当勾选事件到来时,启用您的按钮