我尝试将所选的ID号从源页面发布到新的目标页面(弹出窗口),并在该页面上显示更多详细信息。
我已经搜索了不同的解决方案但看起来有点过时了。 以下是我的问题:
发布它的最佳方式是什么(会话?cookie?)或Page.PreviousPage.FindControl
?或将控件添加到<ItemTemplate>
?还是Javascript?
我不想使用查询字符串,因为我不希望用户手动修改它。
我应该在目标页面上写什么来获取该值?
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string testID= GridView1.SelectedRow.Cells[3].Text;
// eg. Session["ID"] = testID;
//eg. open new pop up window ("target.aspx");
}
很多thx
答案 0 :(得分:0)
你试过Cross Page Postback吗?听起来这就是你要找的东西。
答案 1 :(得分:0)
虽然您可以使用将所选值存储在会话变量甚至跨页回发中,但我首选的方法和整体标准方法是使用简单的查询字符串值。
1)抓取值并使用所需的ID重定向。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var testID = gridView1.SelectedRow.Cells[3].Text;
Response.Redirect("target.aspx?Id=" + Server.UrlEncode(testID));
}
2)在“target.aspx”页面上,加载网址中的值(“http://localhost/target.aspx?Id=125”)
protected void Page_Load()
{
if (!IsPostBack)
{
var querystringValue = Request.QueryString["Id"];
if (!string.IsNullOrEmpty(querystringValue))
{
// do something.
}
}
}
<强> Passing variables between pages using QueryString. 强>
编辑虽然我不会做这样的事情(我希望尽可能保持我的网站无状态),同样的原则适用于会话变量。
protected void Page_Load()
{
if (!IsPostBack)
{
var sessionValue = Session["Id"];
if (!string.IsNullOrEmpty(sessionValue))
{
// do something.
}
}
}
话虽如此,我不明白查询字符串有什么问题。当然,用户可以修改它并插入虚假值,但这就是服务器端验证到位的地方。嘿,如果谷歌使用查询字符串,它们就不会那么糟糕。