简体   繁体   中英

How to call an event handler from one control to the another control where the second control is inside the first control?

i have a calender control like this

    <asp:Calendar ID="CldrDemo" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"

        OnSelectionChanged="CldrDemo_SelectionChanged" OnDayRender="CldrDemo_DayRender">

    </asp:Calendar>

OnDayRender event i have code like this

 protected void CldrDemo_DayRender(object sender, DayRenderEventArgs e)
    {if (e.Day.Date == Convert.ToDateTime("11/30/2010"))//comparing date
        {
            DropDownList ddlBlist = new DropDownList();//creating instance of ddl
            ddlBlist.AutoPostBack = true;
            ddlBlist.Items.Add("Ashrith");//adding values to the ddl
            ddlBlist.Items.Add("Nayeem");//adding values to the ddl
            ddlBlist.SelectedIndexChanged += new EventHandler(ddlBlist_SelectedIndexChanged);//want to call this
            string name = ddlBlist.SelectedItem.Text;
            e.Cell.Controls.Add(ddlBlist);//adding dropdownlist to the cell
            e.Cell.BorderColor = System.Drawing.Color.Black;
            e.Cell.BorderWidth = 1;
            e.Cell.BackColor = System.Drawing.Color.LightGray;

        }

i want to call the event handler for the dropdownlist - selectedIndexchanged and i have added it also like this

protected void ddlBlist_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

but this is not getting fire when i am changing the item of the dropdownlist. Please help

try this

ddlBlist.SelectedIndexChanged += new EventHandler("ddlBlist_SelectedIndexChanged");

try putting your calendar control in a Ajax update panel
and put this line before adding items in your combo box:

ddlBlist.SelectedIndexChanged += new EventHandler(ddlBlist_SelectedIndexChanged);
ddlBlist.Items.Add("Ashrith");//adding values to the ddl 
ddlBlist.Items.Add("Nayeem");//adding values to the ddl 

I believe in order to get this to work you need to have re-added your drop-down list to the controls collection before the SelectedIndexChanged event would normally be fired.

What's happening is, you're adding your control dynamically at render time, but when a post-back happens the control doesn't actually exist any more, or at least it won't until your render method gets called again. And so the event will not fire.

In my experience with adding controls dynamically like this, in order to be able to handle any events they raise you need to be able to re-create your dynamic control tree before the page's Load event occurs. If you can do this, you will probably find that your event will fire as normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM