【问题标题】:ASP.net UserControl Parameter PassingASP.net UserControl 参数传递
【发布时间】: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


【解决方案1】:

在您的 ascx 控件中定义一个名为 CountryId 的属性,当您从 CountryDropdown_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

如果没有,希望这会有所帮助,如有疑问或疑问,请随时在 cmets 中发布您的疑问。快乐编码。

【讨论】:

  • 感谢 Devjosh 的回复,...... databindd(priCountry) Private Sub databindd(ByVal cccountry As String) SqlDataSource1.SelectParameters.Add("Country", cccountry) End Sub .. .......... 我收到此错误。没有为一个或多个必需参数指定值。我错过了什么?
  • 您的绑定方法正在接收 priCountry 作为参数,并且您正在向您的选择参数 SqlDataSource1.SelectParameters.Add("Country", cccountry) 添加不同的值,因此请在此处检查并确保存储过程/查询中的参数名称和数量相同。 @dany 请不要忘记将任何帖子标记为答案/赞成,如果它对您有用,它将帮助我们的其他同行寻找合适的人!谢谢
  • 好吧,我设置了参数的默认值,现在一切正常。谢谢你的帮助。 :D
  • 那是我的荣幸。享受编码:)
【解决方案2】:

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

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

【讨论】:

  • 我尝试了它,但最终得到了诸如No value given for one or more required parameters 之类的错误,一个简单的示例将非常有用。谢谢。
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多