c# - 在按钮单击时将 int 传递到不同的页面
问题描述
Hi I have a page that has a load of check boxes, when one is selected the user then clicks the button to go to a new page. 我需要这个新页面来包含从上一页中选择的记录的 ID。
我不知道如何将名为 FileID 的 ID 的 int 值获取到名为 EditFile.aspx 的下一页。
我这个函数的所有代码目前都在一个 buttonclick 事件中:
protected void btnEditSelectedFile_Click(object sender, EventArgs e)
{
int intSelectedFileCount = 0;
foreach (GridDataItem item in fileRadGrid.MasterTableView.Items)
{
int FileID = int.Parse(fileRadGrid.MasterTableView.DataKeyValues[item.DataSetIndex - (fileRadGrid.CurrentPageIndex * fileRadGrid.PageSize)]["FileID"].ToString()); //Gets File ID of Selected field
CheckBox chk = (CheckBox)item["AllNone"].Controls[0];
if (chk.Checked)
{
intSelectedFileCount++;
}
}
if (intSelectedFileCount == 1)
{
Response.Redirect("EditFile.aspx", false);
}
else
{
lblNeedSingleFile.Visible = true;
}
}
任何有关如何在 EditFile 页面中访问“FileID”的帮助将不胜感激!
解决方案
在 asp.net 中的页面之间共享数据有两种方法:
1) 使用 URL 查询字符串:当您重定向时,您更改以下行
Response.Redirect("EditFile.aspx?FileId=" + FileID.ToString(), false);
在 EditFile.aspx 中你可以在 Page_Load()
int FileId = int.Parse(Request.QueryString["FileId"]);
2)使用会话状态:设置会话字段例如:
Session["FileId"] = FileID;
并将其从 EditFile.aspx 中检索为
int FileId = (int)Session["FileId"];
推荐阅读
- python - 对于包含多个字符串的大型 .CSV 列是否有任何时间拆分功能?
- swiftui - 如何在 SwiftUI 中通过 ViewBuilder 访问 TupleView?
- python - instancemethod'对象没有属性'return_value'
- windows - 什么是“ms-cxh-full://0.0.135.7”?
- javascript - 样式化 HTML5 日期
- android - 对通过 startActivity 创建的 AppCompatActivity 对象的引用
- mysql - SQL:两列的唯一索引,其中一的值不是默认值
- python - discord.py bot 使用 Pillow - ValueError:图像不匹配
- react-native - 访问世博会资产
- flutter - 从另一个小部件访问 Controller.isEmpty