2012-04-03 58 views
0

我在.aspx頁面上有一個帶有ID值的隱藏列的GridView。當用戶點擊某一行時,我需要C#代碼後面的ID列值,以便查詢數據庫並使用與此ID對應的數據填充另一個視圖。我的問題是,我可以將該ID值傳遞給該行的onClick事件中的javascript函數,但是如何從實際查詢SQL Server數據庫的位置獲取它的代碼?如何從一個gridView發送一個id值到數據庫?

+0

請告訴我們,什麼樣的技術堆棧的使用,也表明現有的代碼。 – 2012-04-03 17:45:24

+0

我不確定要顯示什麼代碼,它只是一個簡單的網格視圖。我編輯了添加一些技術的答案。 – neuDev33 2012-04-03 17:57:41

回答

0

這是我如何解決它 -

得到了與所選擇的行索引 - e.Row.Attributes [ 「的onclick」] = ClientScript.GetPostBackClientHyperlink(this.GridView2, 「選擇$」 + e.Row.RowIndex);

在網格的SelectedIndexChanged事件 - GridView2.SelectedRowStyle.BackColor = System.Drawing.Color.LightGray; string id = GridView2.SelectedRow.Cells [0] .Text;

其中,細胞[0]是我隱藏的列其中包含了ID值

1

可以使用數據網格控制的指定包含ID

<asp:gridview 
id="grvTest" 
autogeneratecolumns="true" 
datakeynames="ID" 
runat="server"> 

列DataKeyNames屬性//獲取所選擇的行

void grvTest_SelectedIndexChanged(object sender, EventArgs e) 

{

的值
// Determine the index of the selected row. 
int index = CustomersGridView.SelectedIndex; 


//Display the primary key value of the selected row. 
Message.Text = "The key value of the selected row is " + 
    grvTest.DataKeys[index].Value.ToString(); 

}

+0

是...這就是它 – beauXjames 2012-04-03 22:17:19

相關問題