将值从datagrid传递到另一个页面

问题描述:

我正在尝试将值从gridView传递到另一页的文本框中,并且出现以下错误

Hi Am trying to pass values from gridView to a textbox in another page and i get the following error

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 10:     protected void Page_Load(object sender, EventArgs e)
Line 11:     {
Line 12:         this.txtPatientName.Text = Request.QueryString["name"].ToString();
Line 13:     }
Line 14: }




以下是我第一页的代码




Below is the code from my first page

foreach (GridViewRow row in grdAvailableStuff.Rows)
       {
           string name = row.Cells[1].Text;
           string time = row.Cells[2].Text;
           Response.Redirect("AddPatients.aspx?name=" + name.ToString());
       }



接下来是我的第二页



Then below is my second page

protected void Page_Load(object sender, EventArgs e)
    {
        this.txtPatientName.Text = Request.QueryString["name"].ToString();
    }


在下面尝试此代码
Hi,
Try this code below
if (Request.QueryString["name"]!=null && !string.IsNullOrEmpty(Request.QueryString["name"].ToString()))
{
    this.txtPatientName.Text = Request.QueryString["name"].ToString();
}



在第一页上,为什么每个gridview行都将名称值重定向到另一页?在这种情况下,它将无法工作,基于某些事件/业务逻辑会发生重定向.考虑一下,希望这将解决您的对象引用错误.



And on first page why each gridview row is redirecting name value to other page? In this case it wont work, redirection happen based on some event/business logics. Give this a thought.Hope this will solve your Object reference error.


以datakey作为名称,并在gridview中使用gridview Delete命令,并根据您的需要更改delete命令的文本名称. >
Take datakey as name and take gridview deleting command in gridview and change text name of delete command as per you.
protected void EmployeeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string Name= EmployeeGridView.DataKeys[e.RowIndex].Value.ToString();
    Response.Redirect("AddPatients.aspx?EmployeeId=" + Name);
}






然后在下一页的加载事件中
做某事






then on load event of next page
do something

protected void Page_Load(object sender, EventArgs e)
    {  string Name= Request.QueryString["Name"];
        this.txtPatientName.Text = Name;
    }


希望 [ ^ ]也可能会为您提供帮助.
Hope this[^] also might help you.