I have the following code :
protected void gv_inbox_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "sign")
{
Session["TransYear"] = int.Parse(((HiddenField)gv_inbox.Rows[index].Cells[1].FindControl("HDN_TransYear")).Value);
Session["MailNumber"] = int.Parse(((HiddenField)gv_inbox.Rows[index].Cells[1].FindControl("HDN_MailNumber")).Value);
Response.Redirect("Signature.aspx", false);
//Response.Write("<script>");
//Response.Write("window.open('Signature.aspx','_blank')");
//Response.Write("</script>");
}
}
I want to open the page in a new tab or window . The commented code do that but when refresh
the original page cause errors .how to open Signature.aspx
in a new window or tab in the correct way in the row command event of my gridview .
What you want to do is use the ScriptManager
to make the JavaScript call.
The JavaScript snippet you had defined works, however, you need the ScriptManager
to take care of executing it for you.
String js = "window.open('Signature.aspx', '_blank');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Open Signature.aspx", js, true);
The issue with using Response.Write
is that during PostBack
, the browser will not execute script tags. On the other hand, when using ScriptManager.RegisterClientScriptBlock
, the ASP.NET ScriptManager
will execute the code automatically.
UPDATE - 2013/02/14
As suggested in Vladislav's answer, use ClientScript
vs. ScriptManager
; the difference is ClientScript
is for synchronous postbacks only (no asp:UpdatePanel
), and ScriptManager
is for asynchronous postbacks (using asp:UpdatePanel
).
Additionally, per the author's comment below - enclosing the asp:GridView
in an asp:UpdatePanel
will eliminate the browser message confirming a refresh. When refreshing a page after a synchronous postback (no asp:UpdatePanel
), the browser will resend the last command, causing the server to execute the same process once more.
Finally, when using an asp:UpdatePanel
in conjunction with ScriptManager.RegisterClientScriptBlock
, make sure you update the first and second parameters to be the asp:UpdatePanel
object.
ScriptManager.RegisterClientScriptBlock(updatePanel1, updatePanel1.GetType(), "Open Signature.aspx", js, true);
First of all, the exercise here is how to pass simple values from one page to another.
In your case those are only Keys/Values so you can pass them via GET.
You need to render this every row, instead of the command button.
<a href="Signature.aspx?TransYear=value1&MailNumber=value2" target="_blank">
Sign
</a>
In this example, value1 and value2 are the values that you were writing into HDN_TransYear and HDN_MailNumber respectively.
In Signature.aspx you had to do something like this if you wanted to use the session variables:
Session["TransYear"]
Session["MailNumber"]
Now it should be changed to this:
Request.QueryString["TransYear"]
Request.QueryString["MailNumber"]
That will normally make what you asked for. But...
If you say that this method is insecure, that anyone could play with the querystring variables by changing the parameter values, then...
Compute a signature for each row, put this signature as a third param in your querystring and validate the values and the signature in the other side.
How to compute the signature, well, it is up to you. That's your secret sauce. There are lots of hashing functions and salting algorithms.
As a very simple example:
string signature = CRC32(HDN_TransYear + "stuff that you only know" + HDN_MailNumber)
Then your link should look something like this:
<a href="Signature.aspx?TransYear=2012&MailNumber=1&sig=8d708768" target="_blank">
Sign
</a>
In Signature.aspx you use the values from the querystring and the "stuff you only know" to calculate the CRC32 signature again and validate against the signature passed as parameter in the querystring.
No dramas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With