簡體   English   中英

Asp.Net:在更新面板中從用戶控件重定向到另一個頁面

[英]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) { } }

注意:我測試了一個示例代碼,並找到了原因,在示例代碼中,我將用戶控件拖到了更新面板上,並且該按鈕起作用了,但是當我通過這些代碼調用用戶控件時,該按鈕不起作用了(如果一個用戶控制注冊到主頁,但如果是兩個,則在主頁中沒有任何注冊碼)

對於這個問題,我如何重定向到其他頁面?

對於“缺少例外”,我將做出一些大膽的假設,但讓我們嘗試一下:

  1. 您已為腳本在Visual Studio中禁用了JIT調試
  2. 您已禁用Internet Explorer中的調試

提到問題的真實主題,您可能在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM