1
votes

i have an update panel with a menu in it.
i wanna to redirect to another page when the user click on the print button in grid view in user control (in my update panel)
i used these codes,but they didn't work for me:

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);

this is my code :

<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) { }
}

my menu button :

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) { }
}

Note: i test a sample code and i find the reason, in my sample code i drag user control to my update panel and the button worked, but when i call user control by these codes the button doesn't work(in case one the user control register to main page but in case two there isn't any register code in main page)

and for this problem how can i redirect to other page?

1
When in the page lifecycle do you invoke these? Did you try RegisterClientScriptBlock()?Frédéric Hamidi
i dont understand what u mean? can u explain it to me plz?H.Ghassami
What is the exception you are getting?Alex Filipovici
just my page doesn't redirect to any page.didn't show me any exeptionH.Ghassami

1 Answers

0
votes

I will make a couple of daring assumptions for the "lack of exception", but let's give it a try:

  1. You have JIT debugging disabled for script in Visual Studio
  2. You have disabled debugging in Internet Explorer

Referring to the questions real topic, you are probably binding the data source to your GridView in the Page_Load event (check this answer).

Inside your user control (or wherever you are binding the data to the GridView), you have to check for !Page.IsPostback before binding the GridView to your data source:

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