2012-08-06 80 views
0

我有一个网格视图。 我想从网格视图中选取一行并将该行中的列值传递到新页面,同时将该值存储在会话中。从网格视图传递值到新页面ASP.NET C#?

到目前为止,我知道如何将它作为字符串从啧啧存储,但不知道如何保持int格式的值不将其转换为字符串。

这里是我在我的C#代码中......就像我说过的那样将行写出来作为字符串类型我希望它保持为int。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Select") 
     { 
      Int16 num = Convert.ToInt16(e.CommandArgument); 
      TextBox2.Text = GridView1.Rows[num].Cells[0].Text; 

     } 
    } 

将它放入会话将很容易。我发现它是:

Session["customerID"] = GridView1.Rows[num].Cells[0].Text; 

所以现在真正的问题是如何保持int格式的值。

谢谢!

回答

1

您不能直接将它保存在int类型中.Session将该值存储在对象类型中。所以你必须把它作为int。

int customerID = (int) Session["customerID"]; 
     // or 
int customerID = Convert.ToInt32(Session["customerID"].ToString()); 
    // or 
int customerID = int.Parse(Session["customerID"].ToString()); 
1

你可以通过做这样的事情得到的值返回:

var myInt = (int)Session["customerID"] 

会话变量可以存储任何对象,而不仅仅是字符串。您可以在技术上将整个gridview对象存储在会话变量中。