ASP.net用户控件参数传递

ASP.net用户控件参数传递,asp.net,gridview,user-controls,drop-down-menu,parameter-passing,Asp.net,Gridview,User Controls,Drop Down Menu,Parameter Passing,我是一个新手开发人员。我正在创建一个页面+一个用户控件。aspx页面包含一个dropdownlist,该列表由aspx页面上的sql数据源填充。ascx页面包含一个gridview,该gridview由ascx页面上的另一个sql数据源发布 aspx上的下拉列表有一个国家列表,gridview(在aspx上)应根据所选国家显示数据 我的ascx页面如下 Partial Class UCtest Inherits System.Web.UI.UserControl Pri

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

aspx上的下拉列表有一个国家列表,gridview(在aspx上)应根据所选国家显示数据

我的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>
部分类UCtest
继承System.Web.UI.UserControl
私有国家作为字符串
公共属性PublicCountry()作为字符串
得到
返回原籍国
结束
设置(ByVal值作为字符串)
priCountry=值
端集
端属性
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
SqlDataSource1.SelectParameters.Add(“国家”,priCountry)
端接头
末级
我的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

受保护的子DropDownList1\u SelectedIndexChanged(ByVal sender作为对象,ByVal e作为System.EventArgs)处理DropDownList1.SelectedIndexChanged
UCtest1.PublicCountry=DropDownList 1.SelectedValue
端接头
如果我只传递静态值,比如

    <uc1:UCtest ID="UCtest1" runat="server" PublicCountry="Mexico"/>


因此,我认为我正确地链接了用户控件。但是当我运行页面时,我只得到一个空白页面,这意味着ascx不会从aspx获取数据。我错过了什么

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


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

在ascx控件中定义一个名为CountryId的属性,并在您从下拉列表中选择CountryDropdown的国家时\u SelectedIndexChanged()事件设置控件属性如下

    Private Sub New(Sender As [Object], e As EventArgs)
    YourControl.CountryId = Integer.Parse(CountryDropDown.SelectedVale)
    End Sub
然后在控件属性的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

希望这对你有所帮助,如果你有疑问或疑问,请随时在评论中发表你的疑问。快乐编码

您如何/在何处从ASPX填充PublicCountry属性?这就是我在这里尝试做的。受保护的子DropDownList1\u SelectedIndexChanged(ByVal sender作为对象,ByVal e作为System.EventArgs)处理DropDownList1.SelectedIndexChanged UCtest1.PublicCountry=DropDownList1.SelectedValue End Sub我知道这是错误的,但如果我把。。UCtest1.PublicCountry=“墨西哥”在页面加载事件中,它工作正常。那么,如何使用dropdownlist动态实现呢?我尝试过这样做,但最终得到了一些错误,例如,
没有为一个或多个必需的参数提供值
,一个简单的示例将非常有用。谢谢。谢谢Devjosh的回复。。。。。。。databind(priCountry)私有子databind(ByVal cccountry作为字符串)SqlDataSource1.SelectParameters.Add(“Country”,cccountry)End Sub。。。。。。。。。。。。我得到了这个错误。没有为一个或多个必需参数提供值。我缺少什么?您的绑定方法正在接收priCountry作为参数,并且您正在向选择参数SqlDataSource1添加不同的值。SelectParameters.Add(“Country”,cccountry)如下所示,因此请检查此处,并确保存储过程/查询中的参数名称和数量相同@dany请不要忘记将任何帖子标记为“答案/投票”,如果它对您有效,它将帮助我们的其他同行选择合适的帖子!谢谢,我为参数设置了默认值,现在一切正常。谢谢你的帮助这是我的荣幸。享受编码:)