HOW TO:允許使用者編輯 DataList Web 伺服器控制項中的項目

更新:2007 年 11 月

您可以讓使用者編輯 DataList Web 伺服器控制項中的個別項目。當個別項目設定為編輯模式時,能夠變更的值通常都會顯示在文字方塊或其他控制項中,以便讓使用者自行變更。

若要允許使用者編輯 DataList 控制項中的項目

  1. DataList 控制項的 DataKeyField 屬性設定為包含主索引鍵的資料欄位名稱。

  2. 建立 ItemTemplate (以及 AlternatingItemTemplate,如果您有使用的話),然後加入 Button Web 伺服器控制項。將按鈕的 CommandName 屬性設定為 edit。

    注意事項:

    您可以在呼叫 Button Web 伺服器控制項的任一步驟中,使用 LinkButtonImageButton 控制項。

  3. 建立 DataList 控制項的 EditItemTemplate,其中包含下列項目:

    • 使用者可變更的所有值之控制項。例如,包含所有字元和數值資料的 TextBox 控制項。使用宣告式 Eval 方法,指定每個控制項所繫結的欄位,如下列範例所示:

      安全性注意事項:

      這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀

      <asp:TextBox ID="TextBox1" 
      
        Text='<%# Eval("ProductName") %>' />
      
    • Text 屬性設定為 "Update" 以及 CommandName 屬性設定為 update (區分大小寫) 的 Button 控制項。

    • Text 屬性設定為 "Cancel" 以及 CommandName 屬性設定為 cancel 的 Button 控制項。

      [更新] 按鈕可以讓使用者指定完成編輯並且儲存任何變更。[取消] 按鈕可以讓使用者結束編輯而不儲存變更。

  4. 撰寫執行下列工作的程式碼:

    • 處理 DataList 控制項的 EditCommand 事件,此事件會將 DataList 控制項的 EditItemIndex 屬性設定為項目的索引值,將其置於編輯模式。使用者所按項目的索引,可透過 Item 物件的 ItemIndex 屬性取得。然後呼叫控制項的 DataBind 方法。

    • 處理 DataList 控制項的 CancelCommand 事件,此事件會將 DataList 控制項的 EditItemIndex 屬性設為 -1,然後呼叫控制項的 DataBind 方法。

    • 處理 DataList 控制項的 UpdateCommand 事件。在程式碼中,從目前項目的控制項擷取數值,然後將其傳遞到資料來源控制項進行更新作業。使用的實際程式碼,視您使用的資料來源控制項類型而定。

範例

下列程式碼範例會示範,使用 DataList 控制項和 SqlDataSource 控制項的 ASP.NET 網頁,以顯示 Northwind 資料庫中 Categories 資料表的資訊。使用者能夠編輯項目。

安全性注意事項:

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀

<%@ Page Language="VB" %>

<script >
Protected Sub DataList1_EditCommand(ByVal source As Object, _
        ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
    DataList1.EditItemIndex = e.Item.ItemIndex
    DataList1.DataBind()
End Sub

Protected Sub DataList1_CancelCommand(ByVal source As Object, _
        ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
    DataList1.EditItemIndex = -1
    DataList1.DataBind()
End Sub

Protected Sub DataList1_UpdateCommand(ByVal source As Object, _
        ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
    Dim categoryID As String = _
        DataList1.DataKeys(e.Item.ItemIndex).ToString()
    Dim categoryName As TextBox = _
        CType(e.Item.FindControl("textCategoryName"), TextBox)
    Dim description As TextBox = _
        CType(e.Item.FindControl("textDescription"), TextBox)

    SqlDataSource1.UpdateParameters("original_CategoryID"). _
        DefaultValue = categoryID
    SqlDataSource1.UpdateParameters("categoryName"). _
        DefaultValue = categoryName.Text
    SqlDataSource1.UpdateParameters("Description"). _
        DefaultValue = description.Text
    SqlDataSource1.Update()
    DataList1.EditItemIndex = -1
    DataList1.DataBind()
End Sub
</script>

<html>
<head ></head>
<body>
  <form id="form1" >
    <div>
        <br />
        <asp:DataList  
            DataKeyField="CategoryID" 
            DataSourceID="SqlDataSource1" ID="DataList1"
            OnEditCommand="DataList1_EditCommand" 
            OnCancelCommand="DataList1_CancelCommand" 
            OnUpdateCommand="DataList1_UpdateCommand">
            <EditItemTemplate>
                ID: <asp:Label ID="Label1"  
                         Text='<%# Eval("CategoryID") %>'>
                    </asp:Label>
                <br />
                Name: <asp:TextBox ID="textCategoryName"  
                         Text='<%# Eval("CategoryName") %>'>
                      </asp:TextBox>
                <br />
                Description: <asp:TextBox ID="textDescription" 
                                  
                        Text='<%# Eval("Description") %>'>
                     </asp:TextBox>
                <br />
                <asp:LinkButton ID="LinkButton1"  
                    CommandName="update" >
                    Save
                </asp:LinkButton>
                &nbsp;
                <asp:LinkButton ID="LinkButton2" >
                    CommandName="cancel" 
                    Cancel
                </asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                CategoryID:
                <asp:Label ID="CategoryIDLabel"  
                    Text='<%# Eval("CategoryID") %>'>
                </asp:Label>
                <br />
                CategoryName:
                <asp:Label ID="CategoryNameLabel" 
                     Text='<%# Eval("CategoryName") %>'>
                </asp:Label>
                <br />
                Description:
                <asp:Label ID="DescriptionLabel"  
                    Text='<%# Eval("Description") %>'>
                </asp:Label>
                <br />
                <asp:LinkButton  ID="LinkButton1" 
                    CommandName="edit" >
                    Edit
                </asp:LinkButton><br />
            </ItemTemplate>
        </asp:DataList>

        <asp:SqlDataSource ID="SqlDataSource1"  
              ConnectionString=
                  "<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName], 
                 [Description] FROM [Categories]"
            UpdateCommand="UPDATE [Categories] SET [CategoryName] = 
                 @CategoryName, [Description] = @Description 
                 WHERE [CategoryID] = @original_CategoryID">
            <UpdateParameters>
              <asp:Parameter Name="CategoryName" Type="String" />
              <asp:Parameter Name="Description" Type="String" />
              <asp:Parameter Name="original_CategoryID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
  </form>
</body>
</html>
<%@ Page Language="C#" %>
<script >
protected void DataList1_EditCommand(object source, 
    DataListCommandEventArgs e)
{
    DataList1.EditItemIndex = e.Item.ItemIndex;
    DataList1.DataBind();
}

protected void DataList1_CancelCommand(object source, 
    DataListCommandEventArgs e)
{
    DataList1.EditItemIndex = -1;
    DataList1.DataBind();
}

protected void DataList1_UpdateCommand(object source, 
    DataListCommandEventArgs e)
{
    String categoryID = 
         DataList1.DataKeys[e.Item.ItemIndex].ToString();
    String categoryName = 
         ((TextBox)e.Item.FindControl("textCategoryName")).Text;
    String description = 
         ((TextBox) e.Item.FindControl("textDescription")).Text;

    SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue 
        = categoryID;
    SqlDataSource1.UpdateParameters["categoryName"].DefaultValue 
        = categoryName;
    SqlDataSource1.UpdateParameters["Description"].DefaultValue 
        = description;
    SqlDataSource1.Update();

    DataList1.EditItemIndex = -1;
    DataList1.DataBind();
}
</script>
<html>
<head ></head>
<body>
  <form id="form1" >
    <div>
        <br />
        <asp:DataList  
            DataKeyField="CategoryID" 
            DataSourceID="SqlDataSource1" ID="DataList1"
            OnEditCommand="DataList1_EditCommand" 
            OnCancelCommand="DataList1_CancelCommand" 
            OnUpdateCommand="DataList1_UpdateCommand">
            <EditItemTemplate>
                ID: <asp:Label ID="Label1"  
                         Text='<%# Eval("CategoryID") %>'>
                    </asp:Label>
                <br />
                Name: <asp:TextBox ID="textCategoryName"  
                         Text='<%# Eval("CategoryName") %>'>
                      </asp:TextBox>
                <br />
                Description: <asp:TextBox ID="textDescription" 
                                  
                        Text='<%# Eval("Description") %>'>
                     </asp:TextBox>
                <br />
                <asp:LinkButton ID="LinkButton1"  
                    CommandName="update" >
                    Save
                </asp:LinkButton>
                &nbsp;
                <asp:LinkButton ID="LinkButton2" >
                    CommandName="cancel" 
                    Cancel
                </asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                CategoryID:
                <asp:Label ID="CategoryIDLabel"  
                    Text='<%# Eval("CategoryID") %>'>
                </asp:Label>
                <br />
                CategoryName:
                <asp:Label ID="CategoryNameLabel" 
                     Text='<%# Eval("CategoryName") %>'>
                </asp:Label>
                <br />
                Description:
                <asp:Label ID="DescriptionLabel"  
                    Text='<%# Eval("Description") %>'>
                </asp:Label>
                <br />
                <asp:LinkButton  ID="LinkButton1" 
                    CommandName="edit" >
                    Edit
                </asp:LinkButton><br />
            </ItemTemplate>
        </asp:DataList>

        <asp:SqlDataSource ID="SqlDataSource1"  
              ConnectionString=
                 "<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName], 
                 [Description] FROM [Categories]"
            UpdateCommand="UPDATE [Categories] SET [CategoryName] = 
                 @CategoryName, [Description] = @Description 
                 WHERE [CategoryID] = @original_CategoryID">
            <UpdateParameters>
               <asp:Parameter Name="CategoryName" Type="String" />
              <asp:Parameter Name="Description" Type="String" />
              <asp:Parameter Name="original_CategoryID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
  </form>
</body>
</html>

若要更新資料,您需要欲更新記錄的主索引鍵。您可以從包含索引鍵陣列的 DataKeyField 屬性取得這個值。

若要取得項目中特定控制項的值,請使用 Item 事件引數物件的 FindControl 方法。

您在 SqlDataSource1.UpdateParameters 字典中設定的值,必須符合 UpdateParameters 項目中設定的名稱。

編譯程式碼

程式碼需要名為 NorthwindConnectionString 的連接字串。您所連接的資料庫假設擁有名為 Categories 的資料表,並使用 CategoryID、CategoryName 和 Description 欄位。

頁面用來連接至資料庫的帳戶,必須擁有更新 Category 資料表的權限。

穩固程式設計

範例中的程式碼,不會執行下列您通常在實際執行環境中所進行工作:

  • 程式碼並未包含錯誤檢查,以確定 FindControl 方法傳回有效的控制項。如需更穩定的程式碼,請確定 FindControl 方法傳回的值不是 Null 參考 (在 Visual Basic 是 Nothing)。

  • 程式碼不會檢查更新是否成功。

安全性

使用者在 Web Form 網頁中輸入的內容可能會包含惡意的用戶端指令碼。根據預設,Web Form 網頁會驗證該使用者的輸入內容中沒有包含指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀

請參閱

概念

使用 SqlDataSource 控制項修改資料

使用參數和 SqlDataSource 控制項

參考

DataList Web 伺服器控制項概觀