使用 SqlDataSource 更新 GridView 中的資料時,如何取到使用者於 TemplateField 中控制項的屬性值

本文將介紹使用 SqlDataSource 更新 GridView 中的資料時,該如何取到使用者於 TemplateField 中控制項的屬性值。

當您使用下列 T-SQL 更新資料時,SqlDataSource 會幫您自動建立 UpdateParameter(如下圖以及下面的程式碼第37-40列)。

 

 

   1:  UPDATE Region 
   2:  SET RegionDescription = @RegionDesc 
   3:  WHERE (RegionID = @RegionID)

 

 

image

 

程式碼如下:

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SqlDataSourceDemo.aspx.cs" Inherits="WebApplication1.SqlDataSourceDemo" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  13:              DataKeyNames="RegionID" DataSourceID="SqlDataSource1">
  14:              <Columns>
  15:                  <asp:BoundField DataField="RegionID" HeaderText="RegionID" ReadOnly="True" 
  16:                      SortExpression="RegionID" />
  17:                  <asp:TemplateField HeaderText="RegionDescription" 
  18:                      SortExpression="RegionDescription">
  19:                      <ItemTemplate>
  20:                          <asp:Label ID="Label1" runat="server" Text='<%# Bind("RegionDescription") %>'></asp:Label>
  21:                      </ItemTemplate>
  22:                      <EditItemTemplate>
  23:                          <asp:TextBox ID="TextBox1" runat="server" 
  24:                              Text='<%# Bind("RegionDescription") %>'></asp:TextBox>
  25:                      </EditItemTemplate>
  26:                  </asp:TemplateField>
  27:                  <asp:CommandField ShowEditButton="True" />
  28:              </Columns>
  29:              <EmptyDataTemplate>
  30:                  No Data.
  31:              </EmptyDataTemplate>
  32:          </asp:GridView>
  33:          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  34:              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
  35:              SelectCommand="SELECT RegionID, RegionDescription FROM Region" 
  36:              UpdateCommand="UPDATE Region SET RegionDescription = @RegionDesc WHERE (RegionID = @RegionID)">
  37:              <UpdateParameters>
  38:                  <asp:Parameter Name="RegionDesc" />
  39:                  <asp:ControlParameter ControlID="GridView1" Name="RegionID" PropertyName="SelectedValue" />
  40:              </UpdateParameters>
  41:          </asp:SqlDataSource>    
  42:      </div>
  43:      </form>
  44:  </body>
  45:  </html>

 

 

您可以指定 UpdateParameter 的來源可以是繼承 System.Web.UI.WebControls.Parameter 的類別(例如ControlParameter、SessionParameter等),以 ControlParameter 為例,您可以設定 UpdateParameter 的值是來自於某一控制項的某個屬性,例如上圖 RegionID 參數的值是來自GridView1.SelectedValue。

假設 RegionDesc 參數是要來自於 GridView 控制項中的 TemplateField 裡的 TextBox1.Text(上述程式碼第23-24列),你會發現選不到 GridView 中的控制項。

 

image

 

若自己手動把下列程式碼:


<asp:Parameter Name="RegionDesc" />

 

改為:


<asp:ControlParameter ControlID="TextBox1" Name="RegionDesc" PropertyName="Text" />

 

執行更新時會收到找不到控制項的錯誤訊息(如下圖):

 

image

 

解決方法如下:

  • 以本文的程式碼為例,刪除 UpdateParameter 中的 RegionDesc 參數。
  • 將 UpdateCommand 中的 RegionDesc 參數名稱改為與欄位名稱相同(下列程式碼的第4列改為第9列的樣子)。

       1:  <%--<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       2:  ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
       3:  SelectCommand="SELECT RegionID, RegionDescription FROM Region" 
       4:  UpdateCommand="UPDATE Region SET RegionDescription = @RegionDesc WHERE (RegionID = @RegionID)">--%>
       5:   
       6:  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       7:  ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
       8:  SelectCommand="SELECT RegionID, RegionDescription FROM Region" 
       9:  UpdateCommand="UPDATE Region SET RegionDescription = @RegionDescription WHERE (RegionID = @RegionID)">

 

  • 所要修改的欄位,對應到的控制項屬性是使用支援讀/寫的資料繫結運算式 Bind

       1:   <asp:TextBox ID="TextBox1" runat="server" 
       2:                              Text='<%# Bind("RegionDescription") %>'></asp:TextBox>

 

修改後的完整程式碼如下:

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SqlDataSourceDemo.aspx.cs" Inherits="WebApplication1.SqlDataSourceDemo" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  13:              DataKeyNames="RegionID" DataSourceID="SqlDataSource1">
  14:              <Columns>
  15:                  <asp:BoundField DataField="RegionID" HeaderText="RegionID" ReadOnly="True" 
  16:                      SortExpression="RegionID" />
  17:                  <asp:TemplateField HeaderText="RegionDescription" 
  18:                      SortExpression="RegionDescription">
  19:                      <ItemTemplate>
  20:                          <asp:Label ID="Label1" runat="server" Text='<%# Bind("RegionDescription") %>'></asp:Label>
  21:                      </ItemTemplate>
  22:                      <EditItemTemplate>
  23:                          <asp:TextBox ID="TextBox1" runat="server" 
  24:                              Text='<%# Bind("RegionDescription") %>'></asp:TextBox>
  25:                      </EditItemTemplate>
  26:                  </asp:TemplateField>
  27:                  <asp:CommandField ShowEditButton="True" />
  28:              </Columns>
  29:              <EmptyDataTemplate>
  30:                  No Data.
  31:              </EmptyDataTemplate>
  32:          </asp:GridView>
  33:          <%--<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  34:              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
  35:              SelectCommand="SELECT RegionID, RegionDescription FROM Region" 
  36:              UpdateCommand="UPDATE Region SET RegionDescription = @RegionDesc WHERE (RegionID = @RegionID)">--%>
  37:             <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  38:              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
  39:              SelectCommand="SELECT RegionID, RegionDescription FROM Region" 
  40:              UpdateCommand="UPDATE Region SET RegionDescription = @RegionDescription WHERE (RegionID = @RegionID)">
  41:              <UpdateParameters>
  42:                  <%--<asp:ControlParameter ControlID="TextBox1" Name="RegionDesc" PropertyName="Text" />--%>
  43:                  <asp:ControlParameter ControlID="GridView1" Name="RegionID" PropertyName="SelectedValue" />
  44:              </UpdateParameters>
  45:          </asp:SqlDataSource>    
  46:      </div>
  47:      </form>
  48:  </body>
  49:  </html>

 

 

【參考資料】