Question

i'm having issues retreiving the values out of a dynamically created dropdownlist. all controls are created in the Page_Init section. the listitems are added at that time as well from an array of listitems. (the controls are named the same so should be accessable to the viewstate for appropriate setting.)

here is the function that attempts to retrieve the values:

protected void Eng98AssignmentComplete_Click(object sender, EventArgs e)
{
    String myID = "0";
    Page page = Page;
    Control postbackControlInstance = null;
    // handle the Button control postbacks
    for (int i = 0; i < page.Request.Form.Keys.Count; i++)
    {
        postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
        //Response.Write(page.Request.Form.Keys[i].ToString());
        if (postbackControlInstance is System.Web.UI.WebControls.Button)
        {
            myID = Convert.ToString(
                postbackControlInstance.ID.Replace("button_", ""));
        }
    }
    String txtholder = "ctl00$ContentPlaceHolder$Eng098Instructors_" + myID;
    Response.Write("MYID: " + myID + "<br/>");
    DropDownList ddInstructorCheck = (DropDownList)Page.FindControl(txtholder);
    Response.Write("Instructor Selected: "
      + ddInstructorCheck.SelectedValue + "<br/>");
}

here is the output I get, no matter which instructor was selected.....

MYID: 1_1
Instructor Selected: 0
ctl00$ContentPlaceHolder$Eng098Instructors_1_1

the name of the control is correct (verified via view source)....

ideas?

Was it helpful?

Solution 8

I had 2 catches.... here's what they were.

1.  I didn't clear the table I was adding to before re-creating the controls.

apparently my attention to detail was off yesterday, i'm pretty sure the ctlXX frontrunner of the control was some different number upon postback due to how I was recreating the controls.

2.  I was assigning the same list to all the dropdownlist controls.  

once I called the lookup upon each creation a dropdownlist control, all works well.

anyway for what it's worth....

OTHER TIPS

You're going to a lot of work to build this fancy string:

ctl00$ContentPlaceHolder$Eng098Instructors_1_1

That is the client ID of your control, not the server id. This code is running on the server side, and so you need the server id. To get that control using the server id, you need to do this:

ContentPlaceHolder.FindControl("Eng08Instructors_1_1");

Notice I didn't look in the page, because your content place holder created a new naming container.

Also, the way your loop is set up the myID variable will always end up holding the last button in the Keys collection. Why even bother with the loop?


Based on your comments, a better way to find the id of the dropdownlist is like this:

string id = ((Control)sender).ID.Replace("button_", "Eng098Instructors_");

why not just save the control in an instance in your class so that you don't have to use FindControl?

Do you also re-create the controls during the postback? Dynamically generated/added controls must be re-created with every request, they are not automatically re-created.

Why don't you cast the sender? This should be the button that caused the postback:

string myId = "0";
Button btn = sender as Button;
if (btn != null)
    myId = btn.ID
...

You need to perform something like this because the UniqueID property is the key in Request.Form.

        List<Button> buttons = new List<Button>();
        List<DropDownList> dropdowns = new List<DropDownList>();
        foreach (Control c in Controls)
        {
            Button b = (c as Button);
            if (b != null)
            {
                buttons.Add(b);
            }

            DropDownList d = (c as DropDownList);
            if (d != null)
            {
                dropdowns.Add(d);
            }
        }

        foreach (String key in Request.Form.Keys)
        {
            foreach (Button b in buttons)
            {
                if (b.UniqueID == key)
                {
                    String id = b.ID.Replace("button_", "");
                    String unique_id = "ctl00$ContentPlaceHolder$Eng098Instructors_" + id;
                    Response.Write("MYID: " + id + "<br/>");

                    foreach (DropDownList d in dropdowns)
                    {
                        if (d.UniqueID == unique_id)
                        {
                            Response.Write("Instructor Selected: " + d.SelectedValue + "<br/>");
                            break;
                        }
                    } 
                }
            }   
        }

I'm not sure why you are generating the control in code (you can still add items dynamically if you do), but the code that generates the controls would probably be a huge help here. I'm guessing you are not setting the list item value, and instead just setting the list item text. Try seeing what you get from the SelectedText field and post your control creation function.

EDIT: In response to your comment on @Martin's post, you said "yes I recreate the controls in the Page_Init function each time the page is created (initial or postback)". Are you also setting the selected value when you create them?

You can also use controls on the page even if your data comes from a database, the controls themselves don't have to be dynamically generated.

How about this?

((Button)sender).Parent.FindControl(myid)

Edit:I misunderstood your question. But i think you should follow page lifecycle. it is common issue for dynamically created controls.

I did some research and here is some info about Dynamically Created Controls may help you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top