【发布时间】:2012-01-09 12:07:06
【问题描述】:
我是一名新手开发人员。我正在创建一个页面 + 一个用户控件。 aspx 页面包含一个下拉列表,该下拉列表由 aspx 页面上的 sql 数据源填充。 ascx 页面包含一个由 ascx 页面上的另一个 sql 数据源填充的 gridview。
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 属性?
-
这就是我在这里尝试做的。 Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged UCtest1.PublicCountry = DropDownList1.SelectedValue End Sub 我知道这是错误的,但如果我把.. UCtest1 .PublicCountry = "Mexico" 在页面加载事件中它工作正常。那么如何使用下拉列表动态实现呢?
标签: asp.net gridview user-controls drop-down-menu parameter-passing