ASP.net 用户控件参数传递
我是一名新手开发人员。我正在创建一个页面+一个用户控件。 aspx 页面包含一个下拉列表,该列表由 aspx 页面上的 sql 数据源填充。 ascx 页面包含一个 gridview,该网格视图由 ascx 页面上的另一个 sql 数据源填充。
aspx 上的下拉列表有一个国家/地区列表,网格视图(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 获取数据。我缺少什么?
I am a newbie developer. I am creating a page + a user control. The aspx page contains a dropdownlist which is populated by a sql datasource that is on aspx page. The ascx page contains a gridview which is popluated by another sql datasource that is on ascx page.
The dropdownlist on aspx has a list of countries and the gridview (on ascx) should display data depending on the selected country.
My ascx page as follow.
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>
My aspx page
<%@ 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
It works fine if I just pass static values such as
<uc1:UCtest ID="UCtest1" runat="server" PublicCountry="Mexico"/>
So I think I properly linked the User Control. But when I run the page, I only get blank page which mean ascx doesn't get data from aspx. what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 ascx 控件中定义一个名为 CountryId 的属性,当您从下拉列表中选择国家/地区时,即 CountryDropdown_SelectedIndexChanged() 事件设置您的控件属性,如下所示
然后在控件的属性设置访问器中通过将该 id 传递给您的绑定方法来绑定您的 gridview
希望这会有所
帮助,如果没有的话,如有疑问或疑问,请随时在评论中发表您的疑问。快乐编码。
Define a property called CountryId in your ascx control and when you select the country from dropdown that is CountryDropdown_SelectedIndexChanged() Event set your controls property something like this
And then in your control's Property's set accessor bind your gridview by passing that id to your binding method
something like
Hope this helps if not and have doubts or queries feel free to post your queries in comments. Happy Coding.
可能是在设置值(在 SelectedIndexChanged 中)后,您需要强制用户控件刷新自身,在用户控件准备好其数据后,可能会触发 SelectedIndexChanged 事件。
您可以在用户控件上创建一个公共方法并在从下拉列表中设置值后调用它来实现此购买。
It could be that after you set the value (in SelectedIndexChanged) you need to force the user control to refresh itself, the SelectedIndexChanged event will probably be fired after your user control has prepared its data.
You could achieve this buy creating a public method on your user control and calling it after you set the value from he dropdown.