GridView學習-資料編輯
1: <form id="form1" runat="server">
2: <div>
3: <asp:GridView ID="grdView" runat="server" DataSourceID="sqlConn"
4: DataKeyNames="Id"
5: AutoGenerateEditButton="true"
6: AutoGenerateDeleteButton="true"
7: />
8:
9: <asp:SqlDataSource ID="sqlConn" runat="server" ConnectionString='<%$ConnectionStrings:con %>'
10: SelectCommand = "SELECT Id, Title, Director From Movies"
11: UpdateCommand = "Update Movies Set Title = @Title, Director = @Director Where Id=@Id"
12: DeleteCommand = "Delete Movies Where Id=@Id" />
13: </div>
14: </form>
※可以使用<%@ Page%>指令的MaintainScrollPosition屬性,可以使頁面無論在什麼時候都定位到同樣的位置。
例如:點選GridView控制項的”Edit”後,頁面會自動滾動到被編輯的那一行(IE6+、FireFox1+、Opera8+)。
※GridView控制項不支援”新增”,需使用DetailsView或FormView控制項
.在使用GridView控制項編輯和刪除資料時,需要透過DataKeyNames屬性,得知資料表的主鍵列。
-處理衝突-
GridView同時保留每筆資料列的原始值和修改後的新值。
1: <form id="form1" runat="server">
2: <div>
3: <asp:Label ID="lblMessage" runat="server" EnableViewState="false" />
4: <asp:GridView ID="grdView" runat="server" DataSourceID="sqlConn"
5: DataKeyNames="Id"
6: AutoGenerateEditButton="true"
7: />
8:
9: <asp:SqlDataSource ID="sqlConn" runat="server" ConnectionString='<%$ConnectionStrings:con %>'
10: ConflictDetection="CompareAllValues"
11: OldValuesParameterFormatString="original_{0}"
12: SelectCommand = "SELECT Id, Title, Director From Movies"
13: UpdateCommand = "Update Movies Set Title = @Title, Director = @Director Where Id=@Id"
14: OnUpdated="sqlConn_Updated"/>
15: </div>
16: </form>
1: //事件處理 SqlDataSource OnUpdated
2: protected void sqlConn_Updated(object sender, SqlDataSourceStatusEventArgs e)
3: {
4: if (e.AffectedRows==0)
5: {
6: lblMessage.Text = "Could not update record";
7: }
8: }
※SqlDataSource控件項的ConflicatDetection屬性和OldValuesParameterFormatString屬性都會被賦值。
這兩個屬性為每一筆資料列保留此列的原始值與修改後的值。
->ConflicatDetection屬性
-CompareAllValues
-OverwriteChanges
預設為OverwriteChanges,這會導致SqlDataSource直接使用新值覆蓋到資料列。
若為CompareAllValues,SqlDataSource控制項會為每一筆保留原始值和修改後的值。
-->例如:SqlDataSource控制項的UpdateCommand屬性值如下:
Update Movies Set Title=@Title, Director=@Director
Where Id=@original_Id And Title=@original_Director參數表示這些列的原始值。
如果Title或Director的值在資料庫中被其它人修改過,則不會再更新記錄(如果有人先修改了記錄,則本次修改就會被忽略)。
1: <form id="form1" runat="server">
2: <div>
3: <asp:TextBox ID="txtTitle" runat="server" />
4: <asp:Button ID="btnSubmit" runat="server" Text="Search" OnClick="btnSubmit_Click" />
5: <hr />
6: <asp:GridView ID="grdView" runat="server" DataSourceID="srcMovies"
7: EmptyDataText="No Matching Movies"
8: Visible="false" /> //一開始就必須設為隱藏
9: //資料來源
10: <asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString='<%$ ConnectionStrings:conn %>'
11: SelectCommand="Select Id, Title, Director From Movies
12: Where Title LIKE @Title+'%'">
13: //查詢語法參數設定
14: <SelectParameters>
15: <asp:ControlParameter
16: Name="Title" //查詢語句的參數名
17: ControlID="txtTitle" //控制項ID
18: PropertyName="Text" /> //控制項屬性
19: </SelectParameters>//(參數(Title)參考控制項(txtTitle)的屬性(Text))
20: </asp:SqlDataSource>
21: </div>
22: </form>
1: //事件處理
2: <script runat="server">
3: protected void btnSubmit_Click(object sender, EventArgs e)
4: {
5: grdView.Visible = true;
6: }
7: </script>
.顯示空資料-使用樣版(EmptyDataTemplate)
(無資料時, 提示新增)
1: <form id="form1" runat="server">
2: <div>
3: <asp:TextBox ID="txtTitle" runat="server" />
4: <asp:Button ID="btnSubmit" runat="server" Text="Search" OnClick="btnSubmit_Click" />
5: <hr />
6: <asp:GridView ID="grdView" runat="server" DataSourceID="srcMovies" Visible="false">
7: <EmptyDataTemplate>
8: <p>
9: No matching movies were found.</p>
10: <asp:DetailsView ID="dtlMovie" runat="server" DataSourceID="srcMovies"
11: DefaultMode="Insert" //用來新增
12: AutoGenerateInsertButton="true" //自動產生"新增"、"取消"按鈕
13: AutoGenerateRows="false"
14: OnItemInserted="dtlMovie_ItemInserted"> //資料新增後,引發事件
15: <Fields>
16: <asp:BoundField HeaderText="Title" DataField="Title" />
17: <asp:BoundField HeaderText="Director" DataField="Director" />
18: <asp:BoundField HeaderText="CategoryId" DataField="CategoryId" />
19: </Fields>
20: </asp:DetailsView>
21: </EmptyDataTemplate>
22: </asp:GridView>
23: <asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString='<%$ ConnectionStrings:conn %>'
24: SelectCommand="Select Id, Title, Director From Movies Where Title LIKE @Title+'%'"
25: InsertCommand="Insert Movies(Title,Director,CategoryId) Values(@Title,@Director,@CategoryId)">
26: <SelectParameters>
27: <asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
28: </SelectParameters>
29: </asp:SqlDataSource>
30: </div>
31: </form>
1: //事件處理
2: <script runat="server">
3: protected void btnSubmit_Click(object sender, EventArgs e)
4: {
5: grdView.Visible = true;
6: }
7: protected void dtlMovie_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
8: {
9: txtTitle.Text = (string)e.Values["Title"];
10: grdView.DataBind();
11: }
12: </script>