|
|
|
|
Response.Write(Page.IsValid) Dim queryStr As String = HttpUtility.UrlEncode(txtSearch.Text) Response.Redirect("http://www.google.com/search?q=" & queryStr) End Sub |
|
<uc1:Google title="Google Search" runat="server" ID="Google1" /> |
|
CloseVerb.ImageUrl="Images/CloseVerb.gif" EditVerb.ImageUrl="Images/EditVerb.gif" MinimizeVerb.ImageUrl="Images/MinimizeVerb.gif" RestoreVerb.ImageUrl="Images/RestoreVerb.gif" |
<asp:RadioButtonList ID="rblMode" runat="server" AutoPostBack="True"> <asp:ListItem>Browse Display Mode</asp:ListItem> <asp:ListItem>Design Display Mode</asp:ListItem> </asp:RadioButtonList> |
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles rblMode.SelectedIndexChanged Select Case rblMode.SelectedIndex Case 0 : WebPartManager1.DisplayMode =WebPartManager.BrowseDisplayMode Case 1 : WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode End Select End Sub |
|
Field | Value |
Id | 928e121a-4042-4fb4-9520-21210b9b37c1 |
PathId | 7c3b5dc0-04d0-48a2-bbb2-2b70286f22fe |
UserId | 9bff14df-024f-4bda-9a0a-b4a19ab9e387 |
PageSettings | <Binary data> |
LastUpdatedDate | 10/06/2005 4:44:05 AM |
|
|
<asp:RadioButtonList ID="rblMode" runat="server" AutoPostBack="True"> <asp:ListItem>Browse Display Mode</asp:ListItem> <asp:ListItem>Design Display Mode</asp:ListItem> <asp:ListItem>Catalog Display Mode</asp:ListItem> </asp:RadioButtonList> |
Protected Sub rblMode_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles rblMode.SelectedIndexChanged Select Case rblMode.SelectedIndex Case 0 : WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode Case 1 : WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode Case 2 : WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode End Select End Sub |
|
|
<ZoneTemplate> <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server"> <WebPartsTemplate> <uc1:Google title="Google Search" ID="Google2" runat="server" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> |
|
此外,在运行期间,还可以动态地修改webpart控件的外观等属性,如下:
1) 往窗体中添加一个editor zone的区域控件,往其中再拖放一个appearanceEdiotrPart控件,该控件可以在运行时,让用户动态改变各webpart控件的属性。
2) 我们再修改radiobutton选择框的代码如下,则加一个编辑模式:
<asp:RadioButtonList ID="rblMode" runat="server" AutoPostBack="True"> <asp:ListItem>Browse Display Mode</asp:ListItem> <asp:ListItem>Design Display Mode</asp:ListItem> <asp:ListItem>Catalog Display Mode</asp:ListItem> <asp:ListItem>Edit Display Mode</asp:ListItem> </asp:RadioButtonList> |
3) 修改code-behind代码如下:
Protected Sub rblMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles rblMode.SelectedIndexChanged Select Case rblMode.SelectedIndex Case 0 : WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode Case 1 : WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode Case 2 : WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode Case 3 : WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode End Select End Sub |
4) 运行程序,选择edit display mode模式,这时,会发现每个控件的右上角,会多了一个"edit"的按钮,点该按钮,弹出如下图的窗体,用户可以修改每个控件的外观等属性。
|
最后,我们看下,webpart控件之间还可以进行相互之间的通信,下面的例子中,要实现的是,在一个日历控件中点选某一个日期,会在已经做好的googlesearch的webpart控件的文本框中显示其日期,达到通信的目的,下面介绍其实现步骤:
1、为了使两个webpart控件之间进行通信,必须先声明一个公共的接口。往工程项目里增加一个叫ISelectedDate.vb的类文件,放在app_code目录下,写入如下代码:
Imports Microsoft.VisualBasic Public Interface ISelectedDate ReadOnly Property SelectedDate( ) As Date End Interface |
这里,我们返回一个只读的日期属性selectedDate.
2、再创建一个日历控件CalendarUC.ascx,其中拖拉一个普通的日历控件即可。然后写入如下代码:
Partial Class CalendarUC_ascx Inherits System.Web.UI.UserControl Implements ISelectedDate Public ReadOnly Property SelectedDate( ) As Date Implements ISelectedDate.SelectedDate Get Return Calendar1.SelectedDate.Date End Get End Property <ConnectionProvider("SelectedDate", "SelectedDate")> _ Public Function GetSelectedDate( ) As ISelectedDate Return Me End Function End Class |
上面的代码,首先实现了已经声明了的IselectedDate接口,要留意的是<ConnectionProvider("SelectedDate", "SelectedDate")>中的写法。由于在这个例子中,日历控件要为其他的控件提供信息,因此,该日历控件是一个provider(提供者),而另外的接收信息的控件,是consumer(消费者)。而两者为了要通信,必须要提供一个通信接入点,就象一个电插头,要找到合适的电插板一样。因此,<ConnectionProvider("SelectedDate", "SelectedDate")>中的第一个参数,定义了两者的接口点,第二个参数,则是要传递给consumer的参数,本例是selectedDate。
3、接下来,我们在已经做好的google.ascx控件的代码中,编写如下代码:
Private _selectedDate As ISelectedDate <ConnectionConsumer("SelectedDate", "SelectedDate")> _ Sub setSearchText(ByVal SearchText As ISelectedDate) Me._selectedDate = SearchText End Sub Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender If _selectedDate IsNot Nothing Then txtSearch.Text = _selectedDate.SelectedDate.ToShortDateString End If End Sub |
可以看到 <ConnectionConsumer("SelectedDate", "SelectedDate")>的定义必须和provider中的定义一样。
4、再修改如下代码,将两个控件的命名变得通俗易懂
<ZoneTemplate> <uc1:Google title="Google Search" runat="server" ID="Google1" /> <uc3:CalendarUC title="Calendar Web Part" runat="server" ID="CalendarUC1" /> </ZoneTemplate> |
5、最后,为了使两者能互相通信,必须在default.aspx页中修改如下代码:
<asp:WebPartManager ID="WebPartManager1" runat="server"> <StaticConnections> <asp:WebPartConnection ID="Connection" ProviderID="CalendarUC1" ProviderConnectionPointID="SelectedDate" ConsumerID="Google1" ConsumerConnectionPointID="SelectedDate" /> </StaticConnections> </asp:WebPartManager> |
6、在页面代码中,增加一个radiobutton,用作显示connection模式,并写入如下代码:
Case 4 : WebPartManager1.DisplayMode = WebPartManager.ConnectDisplayMode |
7、运行程序,选择connect displaymode模式。再选择GOOGLE SEARCH的那个webpart控件,点右上角的"conenct"按钮,此时,会显示如下图所示,提示你要选择从那个控件中得到信息,这里选择日历控件,按确定。那么,当点选日历控件的某个日期值的时候,GOOGLE SEARCH的那个文本框里,就会显示相应的日期了。
|
小结:
本文主要介绍了在asp.net 2.0中,如何使用基本的webpart系列控件,以达到改变页面布局以及如何使页面的各webpart控件相互之间通信。