C# Asp.Net:从更新面板中的usercontrol重定向到另一个页面

C# Asp.Net:从更新面板中的usercontrol重定向到另一个页面,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个带有菜单的更新面板。 当用户单击用户控件(在我的更新面板中)网格视图中的打印按钮时,我想重定向到另一个页面 我使用了这些代码,但它们不适用于我: 1 : Response.Redirect("PrintTicket.aspx"); 2 : Response.Write("<script>window.open('PrintTicket.aspx','_parent');</script>"); 3 : Page.Response.Redirect("PrintTi

我有一个带有菜单的更新面板。
当用户单击用户控件(在我的更新面板中)网格视图中的打印按钮时,我想重定向到另一个页面
我使用了这些代码,但它们不适用于我:

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);
1:Response.Redirect(“printtick.aspx”);
2:Response.Write(“window.open('PrintTicket.aspx','u parent');”;
3:Page.Response.Redirect(“PrintTicket.aspx”,true);
4:Page.ClientScript.RegisterStartupScript(typeof(Page),“ClientScript”,“window.location.replace('http://vinaticket.ir    /PrintTicket.aspx');”,false);
5:ScriptManager.RegisterStartupScript(this,this.GetType(),“重定向”,“window.location=”,+Request.ApplicationPath+“PrintTicket.aspx';”,true);
6:Page.ClientScript.RegisterStartupScript(typeof(Page),“ClientScript”,“postRefId”(““+”Session[“TicketID”])。ToString()+”;”,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) { }
}

----菜单。。。。带退出按钮
//加载用户控件的代码隐藏
私有字符串LastLoadedControl
{
得到
{
以字符串形式返回ViewState[“LastLoaded”];
}
设置
{
ViewState[“LastLoaded”]=值;
}
}
私有void LoadUserControl()
{
尝试
{
字符串控制路径=LastLoadedControl;
如果(!string.IsNullOrEmpty(controlPath))
{
占位符1.Controls.Clear();
UserControl uc=(UserControl)LoadControl(controlPath);
占位符1.控件.添加(uc);
}
}  
捕获(例外情况除外){}
}
我的菜单按钮:
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) { } }
受保护的作废您的\u票证\u单击(对象发送者,事件参数e) { 尝试 { 会话[“pg”]=“Z_ViewTicket.ascx”; setPath(); } 捕获(例外情况除外){} }

并设置路径方法():

受保护的void setPath() { 尝试 { string controlPath=string.Empty; controlPath=BASE_PATH+Session[“pg”].ToString(); LastLoadedControl=controlPath; LoadUserControl(); } 捕获(例外情况除外){} }
注意:我测试了一个示例代码并找到了原因,在示例代码中,我将用户控件拖到了更新面板上,按钮工作了,但是 当我通过这些代码调用用户控件时,按钮不起作用(在 案例一是将用户控件注册到主页,案例二是注册到主页 主页中没有任何注册码)


对于这个问题,我如何重定向到其他页面?

我将对“缺少异常”做出一些大胆的假设,但让我们试一试:

  • 你有
  • 你有
  • 参考问题真实主题,您可能正在将数据源绑定到
    Page\u Load
    事件中的
    GridView
    (选中此项)

    在用户控件内部(或将数据绑定到GridView的任何位置),必须检查
    !Page.IsPostback
    将GridView绑定到数据源之前:

    if (!IsPostBack)
    {
        gridView.DataSource = new[] { new { Id = 1, Value = "test" } };
        gridView.DataBind();
    }
    

    在页面生命周期中,何时调用这些?你试过了吗?我不明白你的意思?你能给我解释一下吗?你得到的例外是什么?只是我的页面没有重定向到任何页面。没有显示任何例外我只是在Firefox中测试不是IE,我在另一个项目中制作了一个示例,它是有效的,但在我的项目中不起作用((