开发者

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

开发者 https://www.devze.com 2023-01-24 07:12 出处:网络
i have a calender control like this <asp:Calendar ID=\"CldrDemo\" runat=\"server\" BackColor=\"#FFFFCC\" BorderColor=\"#FFCC66\"

i have a calender control like this

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

        OnSelectionChanged="CldrDemo_Se开发者_开发技巧lectionChanged" 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号