current code: Response.Redirect("DeviceInformation.aspx?arg=" + e.CommandArgument);

which redirects to a page with a data grid. The argument is passed into a stored proc on a database and the infor is returned in a grid.

What i want, is the same thing, but, open it in a new page.

So something like:

Response.OpenNewDamnPage("DeviceInformation.aspx?arg=" + e.CommandArgument);

but unfortunatly that does not exsist.

Thanks in advance for any help!

Dani AI

Generated

Good call trying to launch a new page from the command handler. The blank result you are seeing is very likely due to the value you pass in containing a # (fragment). Anything after # is not sent to the server, so your target page only receives a truncated arg and your SqlDataSource/GridView binds to nothing. Encode the argument before building the URL, and register the script cleanly (safer than Response.Write and works with UpdatePanel).

using System.Web;

protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "AssetName")
    {
        var arg = HttpUtility.UrlEncode(e.CommandArgument.ToString());
        var url = ResolveUrl("~/UsageInformation.aspx?arg=" + arg);

        ScriptManager.RegisterStartupScript(
            this, GetType(), "openUsage",
            "window.open('" + url + "', '_blank');",
            true);
    }
}

Alternatives that avoid script injection:

  • Use an ASP.NET HyperLink with Target="_blank" and an encoded NavigateUrl: <asp:HyperLink ... Target="_blank" NavigateUrl='<%# "~/UsageInformation.aspx?arg=" + HttpUtility.UrlEncode(Eval("Arg").ToString()) %>' />
  • Or for a LinkButton, set OnClientClick = "window.open('...'); return false;" so the click opens a tab and cancels the postback. Note: browsers generally allow window.open only in a direct user gesture.

On the target page (UsageInformation.aspx), read the query once and bind in Page_Load. Request.QueryString is already decoded, so you can use it directly. As hinted, ensure the GridView actually binds after you read the parameter.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var arg = Request.QueryString["arg"];  // validate/null-check
        // If using SqlDataSource, ensure a QueryStringParameter named "arg" exists
        GridView1.DataBind();
    }
}

This combination (encode on the way out, bind once on load) should fix both the new-window requirement and the empty grid described.

Recommended Answers

All 9 Replies

Hey
Try This

protected void button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script type='text/javascript'>window.open('NewPageUrlWithArgument.aspx','_blank');</script>");
    }

This sure will work

If it solves your problem mark the thread as solved.

Use this exactly for your case:

protected void button1_Click(object sender, EventArgs e)
    {
      Response.Write("<script type='text/javascript'>window.open('DeviceInformation.aspx?arg=" + e.CommandArgument + "','_blank');</script>");
    }

Dont forget to enclose your code in code tags like this:

[code=C#] Response.OpenNewDamnPage("DeviceInformation.aspx?arg=" + e.CommandArgument);

[/code]

this will display as

Response.OpenNewDamnPage("DeviceInformation.aspx?arg=" + e.CommandArgument);

Use this exactly for your case:

protected void button1_Click(object sender, EventArgs e)
    {
      Response.Write("<script type='text/javascript'>window.open('DeviceInformation.aspx?arg=" + e.CommandArgument + "','_blank');</script>");
    }

I used the following:

if (e.CommandName == "AssetName")
        {
            Response.Write("<script type='text/javascript'>window.open('UsageInformation.aspx?arg=" + e.CommandArgument + "','_blank');</script>");
         }

I get a new blank page with the correct url!

but if i use:

if (e.CommandName == "AssetName")
        {
            Response.Redirect("UsageInformation.aspx?arg=" + e.CommandArgument);
        }

I get the page reloaded with the data.

So both ways i get the correct url:

http://localhost:4513/WebSite2/UsageInformation.aspx?arg=XFM_UG:#306:100099462

but unless i call the responce.redirect it will load blank. For example even if I copy that link into my browser it loads a blank page.

Very very close... any ideas?

Thanks for your help!

I have some new information tha might help.

If i put a control such as a label as long with the gridview the label loads when I used the following:

http://localhost:4513/WebSite2/UsageInformation.aspx?arg=XFM_UG:#306:100099462

But the grid which is set up to a sql data source that uses the qurrey string to get data, does not load. So i wonder if the remaining problem is with the datagrid?

well, your problem was to load the page in new window.

Which you did successfully.

Did you use the page_load event of new page. Try to get the value from the query string in the page_load event and then load the gridview in the Page_Load event.

This should probably work . I will sure look over this problem.

Your problem of opening new page from the code behind was solved. So you should mark this thread as solved and start new thread.

well, your problem was to load the page in new window.

Which you did successfully.

Did you use the page_load event of new page. Try to get the value from the query string in the page_load event and then load the gridview in the Page_Load event.

This should probably work . I will sure look over this problem.

Your problem of opening new page from the code behind was solved. So you should mark this thread as solved and start new thread.

You quite right, you did solve the question of this thred :)

Thanks so much! I will mark that you solved it.

I did start a new thred for the remaining question of why it did not load. It is here: http://www.daniweb.com/forums/thread207422.html

Thanks so much for your help!

very useful information. Thank you so much

**how to print the diameter of an array in c#

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.