ASP.net UserControl参数传递

时间:2012-01-09 12:07:06

标签: asp.net gridview user-controls drop-down-menu parameter-passing

我是新手开发者。我正在创建一个页面+一个用户控件。 aspx页面包含一个下拉列表,该列表由aspx页面上的sql数据源填充。 ascx页面包含一个gridview,它由ascx页面上的另一个sql数据源填充。

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获取数据。我错过了什么?

2 个答案:

答案 0 :(得分:1)

可能是在设置值(在SelectedIndexChanged中)后需要强制用户控件刷新自身,在用户控件准备好数据后,可能会触发SelectedIndexChanged事件。

您可以实现此购买,在您的用户控件上创建一个公共方法,并在您从下拉列表中设置值后调用它。

答案 1 :(得分:1)

在ascx控件中定义一个名为CountryId的属性,当您从下拉列表中选择CountryDropdown_SelectedIndexChanged()时,将事件设置为您喜欢的控件属性

    Private Sub New(Sender As [Object], e As EventArgs)
    YourControl.CountryId = Integer.Parse(CountryDropDown.SelectedVale)
    End Sub

然后在你的控件的Property的set访问器中通过将id传递给你的绑定方法来绑定你的gridview  

之类的东西
    Private _CountryId As Integer = 0
Public Property CountryId() As Integer
    Get
        Return _CountryId
    End Get
    Set
        _CountryId = value
        bindGridView(_CountryId)
    End Set
End Property

希望这有帮助,如果没有,并有疑问或查询随时在评论中发布您的查询。快乐的编码。

相关问题