C# 如何在gridview的rowcommand事件中在新选项卡中打开页面?
我有以下代码:C# 如何在gridview的rowcommand事件中在新选项卡中打开页面?,c#,javascript,jquery,asp.net,gridview,C#,Javascript,Jquery,Asp.net,Gridview,我有以下代码: 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[i
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>");
}
}
protectedvoid gv_inbox_row命令(对象发送方,GridViewCommandEventArgs e)
{
int index=Convert.ToInt32(e.CommandArgument);
如果(例如,CommandName==“符号”)
{
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);
重定向(“Signature.aspx”,false);
//回答。写(“”);
//响应。写入(“window.open('Signature.aspx','u blank');
//回答。写(“”);
}
}
我想在新选项卡或窗口中打开页面。注释的代码会这样做,但当
刷新
原始页面时会导致错误。如何在my gridview的row命令事件中以正确的方式在新窗口或选项卡中打开签名.aspx
。您要做的是使用脚本管理器
进行JavaScript调用
您定义的JavaScript代码段可以工作,但是,您需要ScriptManager
来为您执行它
String js = "window.open('Signature.aspx', '_blank');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Open Signature.aspx", js, true);
使用Response.Write
的问题是,在PostBack
期间,浏览器不会执行脚本标记。另一方面,当使用ScriptManager.RegisterClient ScriptBlock
时,ASP.NETScriptManager
将自动执行代码
更新-2013/02/14
按照中的建议,使用ClientScript
与ScriptManager
;区别在于ClientScript
仅用于同步回发(无asp:UpdatePanel
),而ScriptManager
用于异步回发(使用asp:UpdatePanel
)
此外,根据作者下面的评论-在asp:UpdatePanel
中包含asp:GridView
将消除确认刷新的浏览器消息。同步回发(noasp:UpdatePanel
)后刷新页面时,浏览器将重新发送最后一个命令,使服务器再次执行相同的过程
最后,当将asp:UpdatePanel
与ScriptManager.RegisterClientScriptBlock
结合使用时,请确保将第一个和第二个参数更新为asp:UpdatePanel
对象
ScriptManager.RegisterClientScriptBlock(updatePanel1, updatePanel1.GetType(), "Open Signature.aspx", js, true);
我宁愿使用
ClientScript.RegisterStartupScript(this.GetType(),“opensignature.aspx”,js,true)代码>
您不需要刷新页面。
脚本块将在页面从回发中恢复后激发
还有一个建议:
-在您的情况下,是否可以使用DataKeys来检索TransYear和MailNumber的值,而不是使用HiddenFields我们已经成功地使用了以下代码若干年了。这几种方法都是从一个更大的通用模块中提取出来的,但我想我已经包括了所有内容
public class Utilities
{
/// <summary>
/// This will return the first parent of the control that is an update panel
/// </summary>
/// <param name="source">The source control</param>
/// <returns>The first parent found for the control. Null otherwise</returns>
public static UpdatePanel GetFirstParentUpdatePanel(Control source)
{
Page currentPage = source.Page;
UpdatePanel updatePanel = null;
Type updatePanelType = typeof(UpdatePanel);
object test = source.Parent;
while (test != null && test != currentPage)
{
// is this an update panel
if (test.GetType().FullName == updatePanelType.FullName)
{
// we've found the containing UpdatePanel
updatePanel = (UpdatePanel)test;
}
// check the next parent
test = ((Control)test).Parent;
} // next test
return updatePanel;
}
/// <summary>
/// This will open the specified url in a new window by injecting a small script in
/// the current page that is run when the page is sent to the client's browser.
/// This method accounts for the presence of update panels and script managers on the
/// page that the control resides in.
/// </summary>
/// <param name="source">The control that the call is being made from</param>
/// <param name="url">The URL to bring up in a new window</param>
public static void RedirectToNewWindow(Control source, string url)
{
// create the script to register
string scriptKey = "_NewWindow";
string script = "window.open('" + url + "');";
RegisterControlScript(source, scriptKey, script);
}
/// <summary>
/// This will register a script for a specific control accounting for the control's UpdatePanel
/// and whether or not there is a script manager on the page
/// </summary>
/// <param name="source">The control that will be affected by the script</param>
/// <param name="scriptKey">A unique key for the script</param>
/// <param name="script">The script that will affect the control</param>
public static void RegisterControlScript(Control source, string scriptKey, string script)
{
// get the control's page
Page currentPage = source.Page;
// does the control reside in an UpdatePanel
UpdatePanel updatePanel = GetFirstParentUpdatePanel(source);
// did we find the UpdatePanel
if (updatePanel == null)
{
// register the script on the page (works outside the control being in an update panel)
currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), scriptKey,
script, true /*addScriptTags*/);
}
else
{
// register the script with the UpdatePanel and ScriptManger
ScriptManager.RegisterClientScriptBlock(source, source.GetType(), scriptKey,
script, true /*addScriptTags*/);
} // did we find the UpdatePanel
}
}
这不是我提取的这些helper方法中最简洁的代码,但你已经明白了。上述代码将在c#2.0及以上版本上运行,并且无论UpdatePanel
嵌套,或者即使根本没有使用UpdatePanel
s,这些代码都将运行为什么要在打开新窗口之前设置会话
如果在打开新窗口后设置会话是可以的,我认为您可以在Signature.aspx
的代码中进行设置,考虑到您已经像@David指出的那样将所需参数作为查询字符串传递。首先,这里的练习是如何将简单值从一个页面传递到另一个页面
在您的情况下,这些只是键/值,因此您可以通过GET传递它们
您需要每行渲染一次,而不是使用命令按钮
<a href="Signature.aspx?TransYear=value1&MailNumber=value2" target="_blank">
Sign
</a>
现在应改为:
Request.QueryString["TransYear"]
Request.QueryString["MailNumber"]
这通常会满足你的要求。但是
如果您说此方法不安全,任何人都可以通过更改参数值来使用querystring变量,那么
为每一行计算一个签名,将该签名作为第三个参数放在querystring中,并在另一端验证值和签名
如何计算签名,这取决于你。那是你的秘方。有很多散列函数和salt算法
作为一个非常简单的例子:
string signature = CRC32(HDN_TransYear + "stuff that you only know" + HDN_MailNumber)
那么您的链接应该如下所示:
<a href="Signature.aspx?TransYear=2012&MailNumber=1&sig=8d708768" target="_blank">
Sign
</a>
在Signature.aspx中,您使用querystring中的值和“您只知道的东西”再次计算CRC32签名,并根据querystring中作为参数传递的签名进行验证
没有戏剧。为什么我们要使用回发功能在不同的URL窗口中打开链接
在创建控件链接时,我们可以将signature.aspx所需的值作为Querystring嵌入到链接控件中,只需简单的浏览器单击即可
我希望这是简单和干净的 如果您想在新选项卡中打开某些内容,那么Response.Redirect()
可能不是一个好办法。这将重定向当前浏览器选项卡中的当前响应。在新选项卡中打开某些内容的最简单方法是完全避免使用服务器端代码,只需创建一个带有目标的\u blank
链接即可。或者,如果需要一个按钮,JavaScript也可以在一个新选项卡中打开一些内容。看起来您试图在引导用户之前设置一些值,但我想链接可以只在查询字符串中包含这些值,Signature.aspx
页可以从那里使用它们。@David有正确的方法。特别是因为会话变量在部署到web场时增加了复杂性。事实上,我甚至没有想到这一点。(我已经有一段时间没有对我的web应用程序着迷于REST了,所以我不必为
string signature = CRC32(HDN_TransYear + "stuff that you only know" + HDN_MailNumber)
<a href="Signature.aspx?TransYear=2012&MailNumber=1&sig=8d708768" target="_blank">
Sign
</a>