2017-04-07 25 views
0

我有技術上的5列在GridView如何將我隱藏的主鍵值在GridView中傳遞給存儲過程?

  1. 修改和停用按鈕
  2. 名稱
  3. 電話
  4. 電子郵件
  5. [隱藏]用戶ID 「這是我對錶的主鍵」

我想將選定行的此userid值傳遞給我的更新存儲過程,該存儲過程將設置active = 0,其中us ERID = @用戶ID。

不知道是否有辦法傳遞選定的數據鍵或只傳遞選定的行隱藏列4,索引爲5或者如果您有其他更好的方法。

protected void gvRowDeleting2(object sender, GridViewDeleteEventArgs e) 
{ 

    String strConnString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; 
    SqlConnection con = new SqlConnection(strConnString); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "usp_update_user_active"; 
    cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = userID; ******NEED HELP HERE**** 

    cmd.Connection = con; 

    try 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     buildcontractoradmingv(); 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     /*Display message saying company is deactivated*/ 
     string message = "User has been removed"; 
     string script = "window.onload = function(){ alert('"; 
     script += message; 
     script += "')};"; 
     ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true); 
     con.Close(); 
     con.Dispose(); 
    } 
} 
+0

見,如果它在['e.Values'](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewdeleteeventargs.values(V = VS。 110).aspx)或['e.Keys'](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewdeleteeventargs.keys(v = vs.110).aspx ) – stuartd

回答

1

你可以試試這個:

string UserID = YourGrid.Rows[e.RowIndex].Cells[IndexOfHiddenCol].Text; 
+1

謝謝!它工作「字符串UserID = YourGrid.Rows [e.RowIndex] .Cells [4] .Text; –

+0

沒有問題的隊友:) – bmvr

0

最簡單的方法是添加userIDDataKeyNames到GridView。在這種情況下,userID是綁定到GridView的源的數據庫列或屬性。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="userID" 

然後從刪除方法中的DataKeys中獲取值。

int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); 

或者你也可以設置爲false可見性增加LabelTemplateField

<asp:Label ID="Label1" runat="server" Text='<%# Eval("userID") %>' Visible="false"></asp:Label> 

然後後面使用FindControl尋找標籤和閱讀文本獲得代碼的值。

Label label = GridView1.Rows[e.RowIndex].FindControl("Label1") as Label; 
int userID = Convert.ToInt32(label.Text); 
+0

嗨我試過第一種方法,並得到這個消息。」索引超出範圍。 \ r \ n參數名稱:index「value [0]中的0表示的值是什麼?我將嘗試第二種方法並報告返回 –

+0

您是否添加了'DataKeyNames'到GridView? – VDWWD

+0

是的,我做了。我嘗試了兩種方法,仍然有相同的問題。這是否適用於隱藏的列?你想看更多的代碼嗎? –

相關問題