【问题标题】:Asp.Net : Redirect to another page from usercontrol in update panelAsp.Net:从更新面板中的用户控件重定向到另一个页面
【发布时间】:2013-01-20 08:08:37
【问题描述】:

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

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)
    {
        尝试
        {
            会话["pg"] = "Z_ViewTicket.ascx";
            设置路径();
        }
        捕捉(异常前){}
    }
    and set path method():

protected void setPath()
{
    try
    {
        string controlPath = string.Empty;
        controlPath = BASE_PATH + Session["pg"].ToString();
        LastLoadedControl = controlPath;
        LoadUserControl();
    }
    catch (Exception ex) { }
}

注意:我测试了一个示例代码,我找到了原因,在我的示例代码中,我将用户控件拖到我的更新面板并且按钮有效,但是 当我通过这些代码调用用户控件时,按钮不起作用(在 情况一,用户控件注册到主页,但情况二有 主页上没有注册码)

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

【问题讨论】:

  • 在页面生命周期中什么时候调用这些?你试过RegisterClientScriptBlock()吗?
  • 我不明白你的意思?可以给我解释一下吗?
  • 你遇到了什么异常?
  • 只是我的页面没有重定向到任何页面。没有显示任何异常

标签: c# javascript asp.net


【解决方案1】:

我将对“没有例外”做出几个大胆的假设,但让我们试一试:

  1. 你有JIT debugging disabled for script in Visual Studio
  2. 你有disabled debugging in Internet Explorer

参考问题真实主题,您可能在Page_Load 事件中将数据源绑定到您的GridView(检查此answer)。

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

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

【讨论】:

  • 我只是在 Firefox 而不是 IE 中测试,我在另一个项目中制作了一个示例,它可以工作,但在我的项目中不起作用。 :((
猜你喜欢
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
相关资源
最近更新 更多