簡體   English   中英

將SQL數據庫中的選定GridView值分配給變量

[英]Assigning a selected GridView value from SQL database to variable

我正在嘗試將GridView控件中的值分配給會話變量。 我有一個SQL數據庫,其中包含一個名為Computer_Cases的表和一個名為FormFactor的列。 我的GridView稱為GridView1,而SqlDataSource是:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" 
    SelectCommand="SELECT * FROM [Computer_Cases]"></asp:SqlDataSource>

因此,顯然GridView只是顯示了case表中的所有內容。 我在GridView控件上啟用了選擇功能,但是我不確定當有人選擇一行時如何實際將值保存到變量中(例如Session["Form_Factor_Selection_Variable"] )。 我正在嘗試為GridView控件上的所選行存儲FormFactor列的值。 然后,該值將在另一頁上使用,以顯示其外形尺寸與最初選擇的主板匹配的所有主板(不同的表)。

您必須利用GridViewSelectedIndexChanged事件。 這是一個例子。

void YourGridView_SelectedIndexChanged(Object sender, GridViewSelectEventArgs e)
  {
    GridViewRow row = YourGridView.SelectedRow;

    Session["Form_Factor_Selection_Variable"] = row.Cells[1].Text; // use the proper index for the cell that you want to save in session variable
  }

並且,請確保在GridView標記代碼中添加以下屬性

onselectedindexchanged="YourGridView_SelectedIndexChanged"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM