0

I have few textboxes on a page which are loaded dynamically on change of text in dropdownlist. The user enters values in these textboxes and clicks on a addnewitem button. In the code behind I want to capture the values entered in these textboxes. But I get the value as empty.

On Pageload mthd there is nothing which is referencing to createdynamiccontrols method.Then I have

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        CreateDynamicControls();

    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }

    protected override object SaveViewState()
    {
        return new Pair(base.SaveViewState(), null);
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(((Pair)savedState).First);

        EnsureChildControls();
    }

 public void CreateDynamicControls()
    { 
        Student= ddlStudentschool.SelectedItem.Text.ToString();
        studentcollege= ddlstudentcollege.SelectedValue.ToString();  
        int rowPos = 0;


        DynPanel.BorderStyle = BorderStyle.Solid;

Based on the selected of studentschool and studentcollege, the following textboxes & fieldnames will be created

        Label Mynewlabel = new Label();
        Mynewlabel.ID = "lbl" + FldLabel;
        Mynewlabel.Text = FldLabel;
        Mynewlabel.Visible = true;
        Mynewlabel.Font.Bold = true;
        DynPanel.Controls.Add(new LiteralControl("<td bgcolor=#333>"));
        DynPanel.Controls.Add(Mynewlabel);
        DynPanel.Controls.Add(new LiteralControl("</td>"));


        if (ControlType == "TextBox")
        {
            int textBoxLength;
            TextBox MynewTextBox = new TextBox();
            MynewTextBox.ID = "txt" + Fldname;

            MynewTextBox.Width = 100;
            MynewTextBox.EnableViewState = true;
            MynewTextBox.Enabled = true;
         did not paste the whole code
       }
   protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        CreateDynamicControls();
    }

  protected void btnAddItem_Click(object sender, EventArgs e)
    {
        CreateDynamicControls();

        if (DynPanel.Controls != null)
        {
            foreach (Control ctrl in DynPanel.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox _txt = DynPanel.FindControl(ctrl.ID) as TextBox; 
                    int count = DynPanel.Controls.Count;
                    // txtresult.ID = ctrl.ID;
                    txtresult.Text = ((TextBox)ctrl).Text;
                    TextBox txtPath = (TextBox)ctrl.FindControl("");
                    string result = txtresult.Text;
                }

Firstly i am getting object reference not set to an instance of an object when the createchildcontrols method is called the second time and also if I dont have the createdynamiccontrols method in additem click event, controls are no longer tehre...

Janaki
  • 47
  • 2
  • 7
  • Are you sure you're not setting `txtresult.Text` and then clearing it with subsequent iterations? – m.edmondson Mar 31 '11 at 21:48
  • @Edmondson , i am not clearing teh txtresult.Text on subsequent iterations. – Janaki Apr 01 '11 at 00:08
  • @Edmonson, my scenario is ondropdownselectedindex changed i need to load the controls dynamically based on the dropdown selection, and teh dynamic controls need to be there when I click on Add new Item button. However, I see that I called the method in both dropdowneventselection and Additembutton click event, so when I click on addnew item due to postback, my values are getting refreshed. But, if I dont call the loaddynamiccontrols method in Additembutton click event there are no controls on the page. How do I deal with this. PLease help me. – Janaki Apr 01 '11 at 15:13

2 Answers2

0

It probably has to do with how you're loading the control. On post-back you need to reload your dynamic controls in the same position as they were originally loaded. Otherwise the viewstate won't match up and the control state can't be loaded into the control tree. If that doesn't work then please post more code!

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • the controls are reloaded at the same position with the same id's on the user selected item in the dropdown list..do you want me to post the code on how the controls are loaded...its a very big one... – Janaki Mar 31 '11 at 21:51
  • @Josh I have edited my orinal code that I pasted, can you help me with that . – Janaki Apr 01 '11 at 00:27
  • You didn't show the full code - e.g. I need to see how you decide to load the dynamic controls. – Josh M. Apr 01 '11 at 00:59
  • can I get your email address please so that I could send it to you – Janaki Apr 01 '11 at 01:03
  • I suggest you attempt to reproduce this but start very simple and slowly add in your existing logic - that way you can have it working first and see where it breaks. – Josh M. Apr 01 '11 at 02:35
  • @Josh, I have pasted the code , kindly review the same and pls let me know wht am I doing wrong – Janaki Apr 01 '11 at 16:21
0

You need to re-create dynamic control on each postback, see my previous answer to a similar question here

Community
  • 1
  • 1
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • Excellent post, helpmed me understand as I am new to this whole world of dynamic controls. However, I think i am missing something...pasting my code above.kindly review it.. – Janaki Apr 01 '11 at 16:00