开发者

Unable to get the value entered by user on button click event , value is entered in a dynamically loaded textbox

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...


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!


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜