我是新手開發者。我正在創建一個頁面+一個用戶控件。 aspx頁面包含一個由aspx頁面上的sql數據源填充的下拉列表。 ascx頁面包含一個由ascx頁面上的另一個sql數據源所填充的gridview。ASP.net UserControl參數傳遞
aspx上的下拉列表有一個國家列表,並且gridview(在ascx上)應顯示取決於所選國家/地區的數據。
我ascx頁面如下。
Partial Class UCtest
Inherits System.Web.UI.UserControl
Private priCountry As String
Public Property PublicCountry() As String
Get
Return priCountry
End Get
Set(ByVal value As String)
priCountry = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SqlDataSource1.SelectParameters.Add("Country", priCountry)
End Sub
End Class
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="UCtest.ascx.vb" Inherits="UCtest" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [CompanyName], [Country] FROM [Customers] WHERE ([Country] = ?)">
<SelectParameters>
</SelectParameters>
</asp:SqlDataSource>
我的aspx頁面
<%@ Register src="UCtest.ascx" tagname="UCtest" tagprefix="uc1" %>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT DISTINCT [Country] FROM [Customers]"></asp:SqlDataSource>
<uc1:UCtest ID="UCtest1" runat="server" />
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
UCtest1.PublicCountry = DropDownList1.SelectedValue
End Sub
,如果我只是通過靜態值,如
<uc1:UCtest ID="UCtest1" runat="server" PublicCountry="Mexico"/>
它正常工作,所以我認爲我正確鏈接的用戶控制。但是當我運行這個頁面時,我只能得到空白頁面,這意味着ascx不能從aspx獲取數據。我錯過了什麼?
如何/你在哪裏填充ASPX的PublicCountry屬性? – Mantorok 2012-01-09 12:26:06
這是我試圖在這裏做的。 _Protected子DropDownList1_SelectedIndexChanged(BYVAL發件人爲對象,BYVALË作爲System.EventArgs)把手DropDownList1.SelectedIndexChanged UCtest1.PublicCountry = DropDownList1.SelectedValue 結束Sub_ 我知道這是錯誤的,但如果我把.. _UCtest1.PublicCountry =「墨西哥「_頁面加載事件它工作正常。那麼我如何通過dropdownlist動態實現呢? – lawphotog 2012-01-09 14:47:31