usercontrol in asp.net
i have one page a开发者_开发技巧nd 2 user control in it and first user control have dropdownlist and second user control have another dropdown list , when we select dropdownlist of first user control than should be filled another dropdown list of second user control.... how can we achieve it ...please explaing in detain
thanks in advance...
I would expose the child DropDownList's OnSelectedItemChanged
event AND the actual DropDownList at the top public level for the user control.
This would allow you to catch the OnSelectedItemChanged
event in the Page
and set the value of the second user control.
Let me know if you want some sample code.
Ok, so first the user control
<%@ 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>
now the file behind that
public partial class SampleUserControl : System.Web.UI.UserControl
{
public DropDownList InternalDropDownList
{
get { return DdlTest; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
okay, lets go to the actual .aspx
<form id="form1" runat="server">
<div>
<uc1:SampleUserControl ID="SampleUserControl1" runat="server" />
<uc1:SampleUserControl ID="SampleUserControl2" runat="server" />
</div>
</form>
and the code behind that
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;
}
}
精彩评论