0

我是新手開發者。我正在創建一個頁面+一個用戶控件。 aspx頁面包含一個由aspx頁面上的sql數據源填充的下拉列表。 ascx頁面包含一個由ascx頁面上的另一個sql數據源所填充的gridview。ASP.net UserControl參數傳遞

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獲取數據。我錯過了什麼?

+0

如何/你在哪裏填充ASPX的PublicCountry屬性? – Mantorok 2012-01-09 12:26:06

+0

這是我試圖在這裏做的。 _Protected子DropDownList1_SelectedIndexChanged(BYVAL發件人爲對象,BYVALË作爲System.EventArgs)把手DropDownList1.SelectedIndexChanged UCtest1.PublicCountry = DropDownList1.SelectedValue 結束Sub_ 我知道這是錯誤的,但如果我把.. _UCtest1.PublicCountry =「墨西哥「_頁面加載事件它工作正常。那麼我如何通過dropdownlist動態實現呢? – lawphotog 2012-01-09 14:47:31

回答

1

定義一個名爲CountryId財產在你的ascx控件,當你從下拉列表中選擇的國家是CountryDropdown_SelectedIndexChanged()事件設置您的控件屬性是這樣的

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

,然後在控件的屬性的set訪問綁定你的GridView通過傳遞ID到你的綁定方法 像

Private _CountryId As Integer = 0 
Public Property CountryId() As Integer 
    Get 
     Return _CountryId 
    End Get 
    Set 
     _CountryId = value 
     bindGridView(_CountryId) 
    End Set 
End Property 

希望這有助於如果不和有疑問或查詢,請隨時在共同發表您的疑問mments。快樂編碼。

+0

THanks Devjosh爲您的回覆,....... databindd(priCountry) Private Sub databindd(ByVal cccountry As String) SqlDataSource1.SelectParameters.Add(「Country」,cccountry) End Sub ...... ......我得到這個錯誤。沒有給出一個或多個必需參數的值。我錯過了什麼? – lawphotog 2012-01-10 10:46:55

+0

您的綁定方法正在接收priCountry作爲參數,並且您正在爲您選擇的參數SqlDataSource1.SelectParameters.Add(「Country」,cccountry)添加一個不同的值,因此請在此處檢查並確保參數的名稱和數量爲在你的存儲過程/查詢中也一樣。 @dany請不要忘記標記任何帖子作爲答案/ upvote如果它爲你工作,這將幫助我們的其他同齡人去適當的!謝謝 – Devjosh 2012-01-10 11:17:55

+0

好吧,我設置參數的默認值,一切正常現在好了。謝謝你的幫助。 :D – lawphotog 2012-01-10 11:22:29

1

可能是因爲在設置值(在SelectedIndexChanged中)後,您需要強制用戶控件自行刷新,在用戶控件準備好數據後,可能會觸發SelectedIndexChanged事件。

您可以實現此購買,在您的用戶控件上創建一個公共方法,並在您設置下拉列表中的值後調用它。

+0

我試過去了,但最終得到了那些錯誤,例如'沒有給出一個或多個必需參數的值',一個簡單的例子會非常有用。謝謝。 – lawphotog 2012-01-10 10:36:38