[英]Asp.Net : Redirect to another page from usercontrol in update panel
我有一个带有菜单的更新面板。
当用户在用户控件(在我的更新面板中)的网格视图中单击“打印”按钮时,我想重定向到另一个页面
我使用了这些代码,但是它们对我不起作用:
1 : Response.Redirect("PrintTicket.aspx");
2 : Response.Write("<script>window.open('PrintTicket.aspx','_parent');</script>");
3 : Page.Response.Redirect("PrintTicket.aspx", true);
4 : Page.ClientScript.RegisterStartupScript(typeof(Page), "ClientScript", "<script language='javascript' type='text/javascript'>window.location.replace('http://vinaticket.ir /PrintTicket.aspx');</script> ", false);
5 : ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect","window.location='" + Request.ApplicationPath + "PrintTicket.aspx';", true);
6 : Page.ClientScript.RegisterStartupScript(typeof(Page), "ClientScript", "<script language='javascript' type='text/javascript'> postRefId('" + Session["TicketID"].ToString() + "');</script> ", false);
7 : ScriptManager.RegisterStartupScript(this, this.GetType(), "redir", "document.location = 'PrintTicket.aspx'", true);
这是我的代码:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
----menu .... with exit button
<asp:PlaceHolder ID="PlaceHolder1" runat="server" ViewStateMode="Enabled">
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="exit" />
</Triggers>
</asp:UpdatePanel>
//code behind to loade user control
private string LastLoadedControl
{
get
{
return ViewState["LastLoaded"] as string;
}
set
{
ViewState["LastLoaded"] = value;
}
}
private void LoadUserControl()
{
try
{
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
catch (Exception ex) { }
}
我的菜单按钮:
protected void your_ticket_Click(object sender, EventArgs e)
{
try
{
Session["pg"] = "Z_ViewTicket.ascx";
setPath();
}
catch (Exception ex) { }
}
and set path method():
protected void setPath()
{
try
{
string controlPath = string.Empty;
controlPath = BASE_PATH + Session["pg"].ToString();
LastLoadedControl = controlPath;
LoadUserControl();
}
catch (Exception ex) { }
}
注意:我测试了一个示例代码,并找到了原因,在示例代码中,我将用户控件拖到了更新面板上,并且该按钮起作用了,但是当我通过这些代码调用用户控件时,该按钮不起作用了(如果一个用户控制注册到主页,但如果是两个,则在主页中没有任何注册码)
对于这个问题,我如何重定向到其他页面?
对于“缺少例外”,我将做出一些大胆的假设,但让我们尝试一下:
提到问题的真实主题,您可能在Page_Load
事件中将数据源绑定到GridView
(选中此答案 )。
在用户控件内部(或将数据绑定到GridView的任何位置),必须在将GridView绑定到数据源之前检查!Page.IsPostback
:
if (!IsPostBack)
{
gridView.DataSource = new[] { new { Id = 1, Value = "test" } };
gridView.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.