asp.net中GridView綁定到數(shù)據(jù)源時,可以自動顯示數(shù)據(jù)源的各個字段。只要設定其AutoGenerateColumns為TRUE即可。但這,自動顯示有其不好的一面,因為不能自義定控制顯示的樣式。 1. DataControlField DataControlField 類用作所有數(shù)據(jù)控件字段類型的基類。數(shù)據(jù)綁定控件使用數(shù)據(jù)控件字段表示數(shù)據(jù)字段。 2. 使用BoundField GridView在顯示狀態(tài),BoundField總是直接把數(shù)據(jù)項顯示為文本;在編輯狀態(tài),BoundFiled將數(shù)據(jù)項顯示為一個單行的文本框
<asp:GridView id=”grdMovies” DataSourceID=”srcMovies” AutoGenerateColumns=”false” Runat=”server”> <Columns> <asp:BoundField DataField=”Title” HeaderText=”Movie Title” /> <asp:BoundField DataField=”Director” HeaderText=”Movie Director” /> <asp:BoundField DataField=”BoxOfficeTotals” DataFormatString=”{0:c}” HtmlEncode=”false” HeaderText=”Box Office Totals” /> </Columns> </asp:GridView>
3. CheckBoxField CheckBoxField會在相應的列內(nèi)顯示一個復選框,在沒有進入編輯模式時,其復雜框處于禁用狀態(tài)。 4. CommandField 使用CommandField可以定制GridView控件中Edit、Delete、Update、Cancel、Select等按鈕的外觀。 5. ButtonField 使用ButtonField可以在GridView中顯示一個按鈕,使用它可以完成自定義或標準的編輯命令 <asp:GridView id=”grdMovieCategories” DataSourceID=”srcMovieCategories” DataKeyNames=”Id,Position” AutoGenerateColumns=”false” OnRowCommand=”grdMovieCategories_RowCommand” Runat=”server”> <Columns> <asp:ButtonField Text=”Move Up” CommandName=”Up” /> <asp:ButtonField Text=”Move Down” CommandName=”Down” /> <asp:BoundField DataField=”Position” HeaderText=”Position” /> <asp:BoundField DataField=”Name” HeaderText=”Category Name” /> </Columns> </asp:GridView> <asp:SqlDataSource id=”srcMovieCategories” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id, Name, Position FROM MovieCategories ORDER BY Position” UpdateCommand=”UPDATE MovieCategories SET Position=@Position WHERE Id=@Id” Runat=”server”> <UpdateParameters> <asp:Parameter Name=”Id” /> <asp:Parameter Name=”Position” /> </UpdateParameters> </asp:SqlDataSource>
6. 使用HyperLinkField HyperLinkField用來鏈接到其他頁面。當創(chuàng)建兩具主從表單的時候,HyperLinkField非常有用。 <asp:GridView id=”grdMovies” DataSourceID=”srcMovies” AutoGenerateColumns=”false” Runat=”server”> <Columns> <asp:HyperLinkField HeaderText=”Movies” DataTextField=”Title” DataNavigateUrlFields=”Id” DataNavigateUrlFormatString=”FrameDetails.aspx?id={0}” Target=”FrameDetails” /> </Columns> </asp:GridView> <div class=”column”> <iframe name=”FrameDetails” id=”FrameDetails”></iframe> </div>
<asp:DetailsView id=”dtlMovie” DataSourceID=”srcMovieDetails” Runat=”server” /> <asp:SqlDataSource id=”srcMovieDetails” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Title, Director, InTheaters FROM Movies WHERE Id=@MovieId” Runat=”server”> <SelectParameters> <asp:QueryStringParameter Name=”MovieId” QueryStringField=”id” /> </SelectParameters> </asp:SqlDataSource>
7. 使用ImageField ImageFIeld用來顯示保存在服務器上的圖片,不能用ImageField來顯示保存在數(shù)據(jù)庫上的圖片。 <script runat=”server”> protected void frmPhoto_ItemInserting(object sender, FormViewInsertEventArgs e) { // Get the FileUpload control FileUpload upPhoto = (FileUpload)frmPhoto.FindControl(“upPhoto”); srcImages.InsertParameters[“FileName”].DefaultValue = upPhoto.FileName; string savePath = MapPath(“~/Photos/” + upPhoto.FileName); // Save contents to file system upPhoto.SaveAs(savePath); } </script> <asp:GridView id=”grdImages” DataSourceID=”srcImages” AutoGenerateColumns=”false” ShowHeader=”false” Runat=”server”> <Columns> <asp:ImageField DataImageUrlField=”FileName” DataAlternateTextField=”AltText” DataImageUrlFormatString=”~/Photos/{0}” ControlStyle-Width=”200px” /> </Columns> </asp:GridView> <asp:SqlDataSource id=”srcImages” ConnectionString=”<%$ ConnectionStrings:Photos %>” SelectCommand=”SELECT FileName, AltText FROM Photos” InsertCommand=”INSERT Photos (FileName, AltText) VALUES (@FileName, @AltText)” Runat=”server”> <InsertParameters> <asp:Parameter Name=”FileName” /> </InsertParameters> </asp:SqlDataSource> <asp:FormView id=”frmPhoto” DefaultMode=”Insert” DataSourceID=”srcImages” OnItemInserting=”frmPhoto_ItemInserting” Runat=”server”> <InsertItemTemplate> <asp:FileUpload id=”upPhoto” Runat=”server” /> <asp:TextBox id=”txtAltText” Text=’<%# Bind(“AltText”) %>’ Columns=”50” Runat=”server” /> <asp:Button id=”btnInsert” Text=”Add New Photo” CommandName=”Insert” Runat=”server” /> </InsertItemTemplate> </asp:FormView>
8.使用TemplateField
<asp:GridView id=”grdMovies” DataSourceID=”srcMovies” DataKeyNames=”Id” AutoGenerateColumns=”false” AutoGenerateEditButton=”true” Runat=”server”> <Columns> <asp:TemplateField HeaderText=”Title”> <!-- Title 列--> <ItemTemplate> <%# Eval(“Title”) %> </ItemTemplate> <!-- 注意,是用Eval綁定字綁,而沒有BoundField屬性了 --> <EditItemTemplate> <!-- 編輯時顯示的模板 --> <asp:TextBox id=”txtTitle” Text='<%# Bind(“Title”) %>' Runat=”server” /> <!-- 使用Bind,雙向綁定 --> <-- 編輯時對txtTitle進行驗證 --> <asp:RequiredFieldValidator id=”valTitle” ControlToValidate=”txtTitle” Text=”(required)” Runat=”server” /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText=”Category”> <!-- Category列 --> <ItemTemplate> <%# Eval(“Name”) %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList id=”ddlCategory” DataSourceID=”srcMovieCategories” DataTextField=”Name” DataValueField=”Id” SelectedValue='<%# Bind(“CategoryId”) %>' Runat=”server” /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> |
|
來自: goodwangLib > 《ASP.Net》