免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
ASP.NET 2.0中實(shí)現(xiàn)模板中的數(shù)據(jù)綁定

  模板化的數(shù)據(jù)綁定控件為我們在頁面上顯示數(shù)據(jù)提供了根本的靈活性。你可能還記得ASP.NET v1.x中的幾個模板化控件(例如DataList和Repeater控件)。ASP.NET 2.0仍然支持這些控件,但在模板中綁定數(shù)據(jù)的語法已經(jīng)被簡化和改善了。本文將討論在數(shù)據(jù)綁定控件模板中綁定數(shù)據(jù)的多種方法。

  數(shù)據(jù)綁定表達(dá)式

  ASP.NET 2.0改善了模板中的數(shù)據(jù)綁定操作,把v1.x中的數(shù)據(jù)綁定語法DataBinder.Eval(Container.DataItem, fieldname)簡化為Eval(fieldname)。Eval方法與DataBinder.Eval一樣可以接受一個可選的格式化字符串參數(shù)??s短的Eval語法與DataBinder.Eval的不同點(diǎn)在于,Eval會根據(jù)最近的容器對象(例如DataListItem)的DataItem屬性來自動地解析字段,而DataBinder.Eval需要使用參數(shù)來指定容器。由于這個原因,Eval只能在數(shù)據(jù)綁定控件的模板中使用,而不能用于Page(頁面)層。當(dāng)然,ASP.NET 2.0頁面中仍然支持DataBinder.Eval,你可以在不支持簡化的Eval語法的環(huán)境中使用它。

  下面的例子演示了如何使用新的簡化的Eval數(shù)據(jù)綁定語法綁定到DataList數(shù)據(jù)項模板(ItemTemplate)中的Image、Label和HyperLink控件。

<asp:DataList ID="DataList1" RepeatColumns="5" Width="600" runat="server" DataSourceID="ObjectDataSource1">
 <ItemTemplate>
 ?。糰sp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=‘<%# Eval("PhotoID", "PhotoFormViewPlain.aspx?ID={0}") %>‘>
  <asp:Image ID="Image1" Runat="server" ImageUrl=‘<%# Eval("FileName", "images/thumbs/{0}") %>‘ /></asp:HyperLink>
 ?。糰sp:Label ID="CaptionLabel" runat="server" Text=‘<%# Eval("Caption") %>‘ />
 </ItemTemplate>
</asp:DataList><br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataComponentTableAdapters.PhotosTableAdapter" SelectMethod="GetPhotosForAlbum">

  數(shù)據(jù)綁定也可以作為控件的主題定義(theme definition)的一部分,這樣我們就可以通過改變主題來隨意地改變模板化控件的布局和外觀。但是Theme(主題)模板中只能使用Eval(或者后面討論的Bind)。綁定到任意的用戶代碼是被禁止的。

  FormView控件

  DataList控件在來自數(shù)據(jù)源的數(shù)據(jù)項中進(jìn)行迭代操作,并為每個數(shù)據(jù)項輸出ItemTemplate(數(shù)據(jù)項模板)。這對于顯示數(shù)據(jù)項列表是有用的,但是通常情況下,你希望在一個窗體中實(shí)現(xiàn)單條數(shù)據(jù)項的綁定操作。為了實(shí)現(xiàn)這個目的,ASP.NET 2.0引入了FormView控件,它能夠在任意的模板中每次顯示一個數(shù)據(jù)項。DetailsView和FormView之間的主要差異在于,DetailsView擁有內(nèi)建的表格顯示方式,而FormView需要使用用戶自定義的顯示模板。在其它方面FormView和DetailsView對象模型是非常相似的。下面的例子顯示了一個綁定到ObjectDataSource的FormView控件。該FormView的ItemTemplate屬性包含數(shù)據(jù)綁定的Image、Label和HyperLink控件,與前面的DataList示例類似。

<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1">
?。糏temTemplate>
 ?。糰sp:Label ID="CaptionLabel" runat="server" Text=‘<%# Eval("Caption") %>‘ Font-Size="32pt" /><br />
  <asp:Image ID="Image1" runat="server" ImageUrl=‘<%# Eval("FileName", "images/{0}") %>‘ />
 ?。糰sp:HyperLink ID="HyperLink1" Text="Back to Album" NavigateUrl=‘<%# Eval("AlbumID", "PhotosDataList.aspx?ID={0}") %>‘ runat="server" />
 </ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataComponentTableAdapters.PhotosTableAdapter" SelectMethod="GetPhoto">
?。糞electParameters>
<asp:QueryStringParameter Name="PhotoID" DefaultValue="9" QueryStringField="ID" />
</SelectParameters>
</asp:ObjectDataSource>

  FormView與DetailsView類似,也跟蹤當(dāng)前顯示的數(shù)據(jù)項,但是當(dāng)數(shù)據(jù)源返回列表的時候,我們也可以選擇支持多個數(shù)據(jù)項的分頁操作。下面的例子顯示了一個帶有分頁功能的FormView。

<asp:FormView ID="FormView1" Runat="server" DataSourceID="SqlDataSource1"
HeaderText="Books for Author" AllowPaging="True">
 <ItemTemplate>
 ?。糰sp:Image ID="Image1" ImageUrl=‘<%# Eval("title_id","~/Images/{0}.gif") %>‘ Runat="server" />
 ?。糰sp:Label ID="Label1" Font-Size="1.2em" Font-Bold="true" Text=‘<%# Eval("title") %>‘ runat="server" />
 ?。糰sp:Label ID="Label2" Text=‘<%# Eval("price","{0:c}") %>‘ runat="server" />
?。?ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT dbo.authors.au_id, dbo.titles.title_id, dbo.titles.title, dbo.titles.type, dbo.titles.price, dbo.titles.notes FROM dbo.authors INNER JOIN dbo.titleauthor ON dbo.authors.au_id = dbo.titleauthor.au_id INNER JOIN dbo.titles ON dbo.titleauthor.title_id = dbo.titles.title_id WHERE (dbo.authors.au_id = @au_id)"
ConnectionString="<%$ ConnectionStrings:Pubs %>">
<SelectParameters>
<asp:QueryStringParameter Name="au_id" DefaultValue="213-46-8915" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>

 

  雙向數(shù)據(jù)綁定

  FormView可以通過相關(guān)的數(shù)據(jù)源控件支持自動地更新、插入和刪除操作(與DetailsView類似)。如果要定義編輯或插入的UI,那么除了定義數(shù)據(jù)項模板(ItemTemplate)之外,你還要定義EditItemTemplate或InsertItemTemplate模板。在這個模板中,你可以把輸入控件(例如文本框、檢查框或下拉列表)綁定到數(shù)據(jù)源的字段。這些模板中的數(shù)據(jù)綁定使用了"雙向"數(shù)據(jù)綁定語法,允許FormView從模板的輸入控件中提取值并傳遞給數(shù)據(jù)源。這些數(shù)據(jù)綁定操作用新的Bind(fieldname)語法代替了Eval。

  請注意:使用Bind語法的數(shù)據(jù)綁定控件必須設(shè)置好ID屬性。

  GridView或DetailsView執(zhí)行更新或插入操作的時候(這些控件的Columns或Fields都會定義BoundFields,綁定字段),GridView或 DetailsView負(fù)責(zé)建立編輯或插入模式中的輸入UI,因此它能夠自動地提取這些值并把它們傳遞給數(shù)據(jù)源。由于模板包含了任意的用戶自定義UI控件,雙向數(shù)據(jù)綁定語法就是必要的,以確保模板化控件(例如FormView)在應(yīng)對更新、插入或刪除操作的時候,知道應(yīng)該從模板中提取那些控件的值。你仍然可以在EditItemTemplate中使用Eval語句進(jìn)行數(shù)據(jù)綁定,來給數(shù)據(jù)源傳遞值。請注意,F(xiàn)ormView與DetailsView和GridView一樣支持DataKeyNames屬性,它保存了傳遞給更新/刪除操作的主鍵字典的原始值,即使這些值沒有顯示出來。

  FormView支持DefaultMode屬性,它可以指定默認(rèn)顯示的模板,但在默認(rèn)情況下FormView處于只讀模式并顯示ItemTemplate模板。為了把UI從只讀模式轉(zhuǎn)換為編輯或插入模式,你可以給模板添加一個按鈕控件,把該按鈕的CommandName屬性設(shè)置為Edit或New。在EditItemTemplate模板中,你可以增加按鈕,把CommandName設(shè)置為Update或Cancel以提交或終止更新操作。類似的,你可以增加按鈕,把CommandName設(shè)置為Insert或Cancel來提交或終止插入操作。

  下面的例子演示了定義了ItemTemplate和EditItemTemplate模板的FormView。其中的ItemTemplate模板包含了使用Eval(雙向)綁定的控件,而EditItemTemplate模板則包含了使用Bind語句進(jìn)行雙向綁定的文本框控件。主鍵字段(PhotoID)是使用DataKeyNames屬性存放在viewstate中的。該FormView包含了用于在模板之間進(jìn)行切換的按鈕。

<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="PhotoID">
<EditItemTemplate>
?。糱>Enter a New Caption:</b>
?。糰sp:TextBox Text=‘<%# Bind("Caption") %>‘ runat="server" ID="CaptionTextBox" /> <asp:Button ID="Button1" runat="server" Text="Update" CommandName="Update" />
?。糰sp:Button ID="Button2" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<ItemTemplate>
?。糰sp:Label ID="CaptionLabel" runat="server" Text=‘<%# Eval("Caption") %>‘ Font-Size="32pt" /><br />
?。糰sp:Image ID="Image1" runat="server" ImageUrl=‘<%# Eval("FileName", "images/{0}") %>‘ /> <br />
?。糰sp:Button ID="Button3" runat="server" Text="Edit Caption..." CommandName="Edit" /> <asp:HyperLink ID="HyperLink1" Text="Back to Album" NavigateUrl=‘<%# Eval("AlbumID", "PhotosDataList.aspx?ID={0}") %>‘ runat="server" />
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataComponentTableAdapters.PhotosTableAdapter" SelectMethod="GetPhoto" UpdateMethod="UpdateCaption" OldValuesParameterFormatString="original_{0}">
?。糢pdateParameters>
 ?。糰sp:Parameter Name="Caption" />
 ?。糰sp:Parameter Name="Original_PhotoID" />
?。?UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="PhotoID" DefaultValue="9" QueryStringField="ID" />
</SelectParameters>
</asp:ObjectDataSource>


  GridView和DetailsView還支持模板化UI,它是通過給Columns或Fields集合增加TemplateField來實(shí)現(xiàn)的。TemplateField支持使用ItemTemplate、EditItemTemplate和InsertItemTemplate(DetailsView才有)為控件的不同顯示模式中的字段指定UI。與上面的FormView示例類似,EditItemTemplate或InsertItemTemplate中的雙向數(shù)據(jù)綁定也允許GridView或DetailsView從這些模板的控件中提取值。TemplateField最常見的用途是給EditItemTemplate增加驗(yàn)證器控件,用于公開地驗(yàn)證GridView或DetailsView操作。下面的例子演示了這種技術(shù)。
 


……
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataKeyNames="AlbumID">
?。糃olumns>
 ?。糰sp:CommandField ShowEditButton="True" />
 ?。糰sp:BoundField ReadOnly="True" HeaderText="AlbumID" DataField="AlbumID" SortExpression="AlbumID" />
 ?。糰sp:TemplateField HeaderText="AlbumName" SortExpression="AlbumName" ItemStyle-Wrap="false">
  ?。糏temTemplate>
   ?。糰sp:Label ID="Label1" runat="server" Text=‘<%# Eval("AlbumName") %>‘></asp:Label>
  ?。?ItemTemplate>
  ?。糆ditItemTemplate>
   ?。糰sp:TextBox ID="TextBox1" runat="server" Text=‘<%# Bind("AlbumName") %>‘></asp:TextBox>
   ?。糰sp:RequiredFieldValidator ControlToValidate="TextBox1" ErrorMessage="AlbumName cannot be empty" ID="RequiredFieldValidator1" Display="Dynamic" runat="server">*</asp:RequiredFieldValidator>
  ?。?EditItemTemplate>
 ?。?asp:TemplateField>
  ……
 ?。?asp:GridView><br />
  <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
 ?。糰sp:ObjectDataSource ID="ObjectDataSource1" runat="server" ConvertNullToDBNull="true"
TypeName="DataComponentTableAdapters.AlbumsTableAdapter" SelectMethod="GetAlbumsByOwner" UpdateMethod="Update" OldValuesParameterFormatString="original_{0}">
  ……
</asp:ObjectDataSource>

  TemplateField的另外一種用途是定制給GridView或DetailsView列/字段輸入值的控件。例如,你可以在TemplateField的EditItemTemplate中放置一個DropDownList,允許用戶從預(yù)定義的值列表中選擇。下面的例子演示了這種技術(shù)。請注意,示例中的下拉列表綁定到了自己的數(shù)據(jù)源控件,以動態(tài)地獲取列表值。

<asp:TemplateField HeaderText="Owner" SortExpression="Owner">
 <ItemTemplate>
 ?。糰sp:Label ID="Label2" runat="server" Text=‘<%# Eval("Owner") %>‘></asp:Label>
 </ItemTemplate>
?。糆ditItemTemplate>
  <asp:DropDownList DataSourceID="ObjectDataSource2" DataTextField="Owner" DataValueField="Owner" ID="DropDownList2" runat="server" SelectedValue=‘<%# Bind("Owner") %>‘>
 ?。?asp:DropDownList>
 </EditItemTemplate>
 <ItemStyle Wrap="False" />
</asp:TemplateField>

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP.NET 3.5中的ListView控件和DataPager控件-其它相關(guān)問題-華夏...
ListView控件學(xué)習(xí)系列1-了解ListView控件
一天精通asp.net:專為有其他語言基礎(chǔ)的人 (轉(zhuǎn))
FormView控件
帶分類的產(chǎn)品側(cè)欄前臺
asp.net中Bind和Eval的有什么區(qū)別_北極狼
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服