开发者

How to return DropDownList selections dynamically in ASP.NET?

This is probably a simple question but I am developing a web app in C# with DropDownList. Currently it is working for just one DropDownList. But now that I modified the code so that number of DropDownLists that should appear is dynamic, it gives me error;

"The name 'ddl' does not exist in the current context."

The reason for this error is that there a multiple instances of 'ddl' = number of counters. So how do I instead return more than one 'ddl'? Like what return type should this method have instead? And how do I return these values?

Reason I need it dynamic is I need to create one DropDownList for each column in whatever Adventureworks table they select.

      private DropDownList CreateDropDownLists()
    {
        for (int counter = 0; counter < NumberOfControls; counter++)
        {
            DropDownList ddl = new DropDownList();
            SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
            ddl.ID = "DropDownListID" + (counter + 1).ToString();

            int NumControls = targettable.Length;
            DataTable dt = new DataTable();
            dt.Load(dr2);

            ddl.DataValueField = "COLUMN_NAME";
            ddl.DataTextField = "COLUMN_NAME";
            ddl.DataSource = dt;
            ddl.ID = "DropDownListID 1";
            ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
            ddl.DataBind();

            ddl.AutoPostBack = true;
            ddl.EnableViewState = true; //Preserves View State info on Postbacks
            //ddlList.Style["position"] = "absolute";
            //ddl.Style["top"] = 80 + "px";
            //ddl.Style["left"] = 0 + "px";
    开发者_StackOverflow社区        dr2.Close();
        }
        return ddl;
    }


Looks like you are trying to return the drop down at the end of the function, but its declared inside the scope of the for loop. Try creating an array of drop downs at the beginning of the function (before the for loop), and set the array inside the loops then return it. You'll also need to change your function declaration to support an array of drop downs.

private DropDownList[] CreateDropDownLists()
{
    DropDownList[] dropDowns = new DropDownList[NumberOfControls];

    for (int counter = 0; counter < NumberOfControls; counter++)
    {
        DropDownList ddl = new DropDownList();
        SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
        ddl.ID = "DropDownListID" + (counter + 1).ToString();

        int NumControls = targettable.Length;
        DataTable dt = new DataTable();
        dt.Load(dr2);

        ddl.DataValueField = "COLUMN_NAME";
        ddl.DataTextField = "COLUMN_NAME";
        ddl.DataSource = dt;
        ddl.ID = "DropDownListID 1";
        ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        ddl.DataBind();

        ddl.AutoPostBack = true;
        ddl.EnableViewState = true; //Preserves View State info on Postbacks
        //ddlList.Style["position"] = "absolute";
        //ddl.Style["top"] = 80 + "px";
        //ddl.Style["left"] = 0 + "px";
        dr2.Close();

        dropDowns[counter] = ddl;
    }
    return dropDowns;
}


Why not just return a list of DropDownList?

private List<DropDownList> CreateDropDownLists()
{
    List<DropDownList> listDDL = new List<DropDownList>();
    for (int counter = 0; counter < NumberOfControls; counter++)
    {
        DropDownList ddl = new DropDownList();

        SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
        ddl.ID = "DropDownListID" + (counter + 1).ToString();

        int NumControls = targettable.Length;
        DataTable dt = new DataTable();
        dt.Load(dr2);

        ddl.DataValueField = "COLUMN_NAME";
        ddl.DataTextField = "COLUMN_NAME";
        ddl.DataSource = dt;
        ddl.ID = "DropDownListID 1";
        ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        ddl.DataBind();

        ddl.AutoPostBack = true;
        ddl.EnableViewState = true; //Preserves View State info on Postbacks
        //ddlList.Style["position"] = "absolute";
        //ddl.Style["top"] = 80 + "px";
        //ddl.Style["left"] = 0 + "px";
        dr2.Close();

        listDDL.Add(ddl);
    }
    return listDDL;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜