Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to bind my two drop down in such a way that on selected index of one dropdown second fill with its relative values. I need this activity on user control page because my form is on .ascx page only but the problem is when selected index calls it reload the page again and my controls set on its initial values and second drop down is not filling its value.. what to do..???

please help me and tell me how to control postback here

thanks in advance
Posted
Updated 11-Jun-14 1:34am
v4

1 solution

Please, consider using updatepanel envolving both controls on the page that uses them, and handling the selectindexchange event on that same page (which will now not postback anymore).


Update:

this is a basic example which makes the second drop down has the first dropdown's value when the latter is selected.

Page:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dummyweb.WebForm1" %>

<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="uc1" TagName="WebUserControl2" %>



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                    <uc1:WebUserControl1 runat="server" ID="WebUserControl1" OnSelectIndexChanged="WebUserControl1_SelectIndexChanged" />
                    <uc1:WebUserControl2 runat="server" ID="WebUserControl2" />

                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>



PAGE.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void WebUserControl1_SelectIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            WebUserControl2.DropDown.SelectedValue = ddl.SelectedValue;
        }
    }
}




UserControl1


XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="dummyweb.WebUserControl1" %>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="item3" Value="3"></asp:ListItem>
    <asp:ListItem Text="item4" Value="4"></asp:ListItem>
</asp:DropDownList>



UserControl1.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public event EventHandler SelectIndexChanged;

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged(sender, e);
        }

    }
}



UserControl2

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="dummyweb.WebUserControl2" %>
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="item3" Value="3"></asp:ListItem>
    <asp:ListItem Text="item4" Value="4"></asp:ListItem>
</asp:DropDownList>



UserControl2.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dummyweb
{
    public partial class WebUserControl2 : System.Web.UI.UserControl
    {
        public DropDownList DropDown { get { return DropDownList1; } }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900