2011-05-02 42 views
0

我有一個頁面和2個用戶控件,第一個用戶控件有下拉列表,第二個用戶控件有另一個下拉列表,當我們選擇第一個用戶控件的下拉列表時,應該填充另一個下拉列表中的第二個下拉列表用戶控件....我們如何才能實現呢?請explaing在預先扣留asp.net中的usercontrol

感謝...

+0

請閱讀:http://tinyurl.com/so-hints – Oded 2011-05-02 07:34:02

回答

0

我會暴露在公衆頂級水平,爲孩子DropDownList的OnSelectedItemChanged事件與實際的DropDownList用戶控制。

這將允許您捕獲Page中的OnSelectedItemChanged事件並設置第二個用戶控件的值。

讓我知道你是否想要一些示例代碼。

好了,第一用戶控制

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SampleUserControl.ascx.cs"  Inherits="WebApplication1.UserControls.SampleUserControl" %> 
<asp:DropDownList runat="server" ID="DdlTest" AutoPostBack="true"> 
    <asp:ListItem Text="Sampe 1" /> 
    <asp:ListItem Text="Sampe 2" /> 
</asp:DropDownList> 

現在落後

public partial class SampleUserControl : System.Web.UI.UserControl 
{ 
    public DropDownList InternalDropDownList 
    { 
     get { return DdlTest; } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

好文件,讓我們進入實際的.aspx

<form id="form1" runat="server"> 
<div> 
    <uc1:SampleUserControl ID="SampleUserControl1" runat="server" /> 
    <uc1:SampleUserControl ID="SampleUserControl2" runat="server" /> 
</div> 
</form> 

和後面的代碼那

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SampleUserControl1.InternalDropDownList.SelectedIndexChanged += InternalDropDownList_SelectedIndexChanged; 

    } 

    void InternalDropDownList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     SampleUserControl2.InternalDropDownList.SelectedValue = SampleUserControl1.InternalDropDownList.SelectedValue; 
    } 
} 
+0

是的,我想知道詳細的代碼 – 2011-05-02 08:08:11