首页 > 解决方案 > 在按钮单击时将 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”的帮助将不胜感激!

标签: c#asp.netradgrid

解决方案


在 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"];

推荐阅读