2013-07-24 57 views
-3

我想通過會話的幫助按下按鈕時,從網格視圖傳遞一個值到另一個頁面。如何實現它?如何通過會話將值從gridview傳遞到另一個頁面?

C#代碼:

保護無效的Page_Load(對象發件人,EventArgs的)

{ 
     if (!IsPostBack) 
     { 
      BindEmployeeDetails(); 
     } 
    } 
    protected void BindEmployeeDetails() 
    { 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Select uniqueref,fldCustomerName,fldCustomerAddress,fldCustomerCity,fldOrderValue,fldINRrEUR from asm", con); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     con.Close(); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      GridView1.DataSource = ds; 
      GridView1.DataBind(); 
     } 
     else 
     { 
      ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); 
      GridView1.DataSource = ds; 
      GridView1.DataBind(); 
      int columncount = GridView1.Rows[0].Cells.Count; 
      GridView1.Rows[0].Cells.Clear(); 
      GridView1.Rows[0].Cells.Add(new TableCell()); 
      GridView1.Rows[0].Cells[0].ColumnSpan = columncount; 
      GridView1.Rows[0].Cells[0].Text = "No Records Found"; 
     } 

    } 



    protected void Add_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("add.aspx"); 
    } 

    protected void Edit_Click(object sender, EventArgs e) 
    { 

     Response.Redirect("edit.aspx"); 
    } 
+3

你到目前爲止嘗試過什麼嗎?一些代碼示例將會很棒.. –

+0

@SonerGönül在每一行中,第一列是主鍵列,當我按下編輯按鈕時,它將重定向到另一個頁面即編輯頁面。在那裏我編輯相應的列與該編輯頁面中的會話變量中存在的主鍵的幫助。 – ramits

+0

@ramits,你需要證明你已經開始了,問題是可以回答的。 **請顯示您的一些代碼**。 – Liam

回答

1

設定值成Session,是這樣的:從Session

// We will say that `GridView1.PrimaryKey` is an int 
Session["YourGridValue"] = GridView1.Value; 

讀取值,這樣:

// Check first to make sure our value is in Session 
    if(null != Session["YourGridValue"]) 
    { 
     int sessionValue = (int)Session["YourGridValue"]; 
    } 

注意:Session中的值存儲爲Object,但可以是字符串,int,List等。因此在檢索值時必須將其轉換爲正確的類型。

+0

在我的網格視圖中,第一列是主鍵列。我在網格視圖的每一行都有一個編輯按鈕。如果我按編輯按鈕,它將重定向到另一個頁面,即編輯頁面。現在我的問題是,當重定向到編輯頁面時,我想將相應列的主鍵傳遞給會話變量,並在該會話變量的幫助下編輯另一個頁面中的特定網格視圖列。如何實現這一目標? – ramits

+0

在一個名爲'Edit'的GridView命令事件中,重定向到編輯頁面的邏輯在哪裏?或者是該字段是編輯頁面的鏈接? –

+0

+1 .. – Liam

相關問題