Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a aspx page where i am creating several dynamic controls.

I am also Re-creating all these dynamic controls in Page_PreInit phase since i need to render them when an event happens or when the page is posted back to server.

But Now the problem is, i want to retrieve the selected value from a control "DropDownList" when the DropDownlist Change event occurs. And Since i am Re-creating all Dynamic Controls in PreInit phase, i cannot get the Dropdownlist selected value in that phase.?

Please advise.

Thanks in advance.

Code Ex.

C#
protected void Page_PreInit(object sender, EventArgs e)
    {
        //recreating all dyn controls on every postback in order to access them..
        if (IsPostBack)
        {
            Custom_Org_Chart.Draw_Entire_Chart(this.Page, form1,this.GetType(),Convert.ToInt32(DropDownList1.SelectedItem.Value));
        }
    }

Here the Dynamic controls will be rendered, based on the DropDownlist Selected Item Value
but (DropDownList1.SelectedItem.Value) will throw NullRefrenceException.

Note:
1. I tried to get the value by OverRiding Pre-Render Event, but it didn't work..
2. Also tried re-creating all dynamic controls on DropDownList Change event, it threw duplicate ID's found error, since controls were creating twice..
Posted
Updated 10-Jun-14 21:15pm
v3

You need to use the Page OnInit event to create dynamic controls.

http://msdn.microsoft.com/en-us/library/cd6at422(v=vs.110).aspx[^]
 
Share this answer
 
I think what's really getting you is the if(isPostBack). It seems weird, that you'd want to recreate your dynamic controls on postback, but since a web page is stateless, it doesn't know what your control is anymore. I did a little exercise myself to prove it. Put a breakpoint on the aList_SelectedIndexChanged event and it will visit it on postback with the right value.


ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <asp:Panel ID="pnlMain" runat="server" Style="width: 100%;">
    </asp:Panel>
    </form>
</body>
</html>


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

public partial class _Default : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        CreateDropDown();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void CreateDropDown()
    {
        DropDownList aList = new DropDownList();
        for (int i = 0; i < 5; i++)
        {
            aList.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        aList.SelectedIndexChanged += new EventHandler(aList_SelectedIndexChanged);
        aList.AutoPostBack = true;
        pnlMain.Controls.Add(aList);
    }

    private void aList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList aList = (DropDownList)sender;
        int i = -1;
        if (Int32.TryParse(aList.SelectedValue, out i))
        { 
            //good to go
        }
    }
}
 
Share this answer
 
v2
Comments
Prathap S V 13-Jun-14 1:10am    
Thanks for the help, it works! but a small clarification, i didn't override the OnPreInit event instead i just used void Page_PreInit event , with 2 arguments {object sender , EventArgs e},So what might be the difference, when withOverride and WithoutOverride works the same way...
jasonHall 13-Jun-14 9:21am    
It's okay, the compiler will know that you have managed code for the Page_PreInit event, otherwise, I'm pretty sure the compiler would add it anyway. VB.NET is not so forgiving, and that's just one reason why I think C# is way better, but I digress. In any case, it works, so the compiler is certainly using your event handler when the page is initialized. One way to be certain is put a break point on the method and see if it pauses on initialization. But since your dropdown works, we know it's doing that. Glad to help!

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