开发者

About saving data into database from dynamically created controls in C# asp.net

I Have created dynamic controls depending on no which i select in dropdownlist. For that i wrote code in SelectedIndexChanged event of dropdownlist as follows:

int N = Convert.ToInt32(ddlpassenger.SelectedValue);
Table t = new Table();
 for (int i = 0; i < N; i++)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            TextBox name = new TextBox();
            name.ID = "txtname" + i;
            cell.Controls.Add(name);
            row.Cells.Add(cell);
            t.Rows.Add(row);
            PName_placeholder.Controls.Add(t);
        }

I have to save data inside database. For that i have written code for getting values of that created control in button_click event as follows:

string pname = "";
        foreach (TableRow tr in t.Rows)
        {
            foreach (TableCell tc in tr.Cells)
            {
                foreach (Control c in tc.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.ID.StartsWith("txtname"))
                        {
                            TextBox txt = (TextBox)t.FindControl(c.ID);

                            pname = txt.Text;
                            Session["pname1"] = pname;
                        }
                    }
                }
            }
        }

But i am not getting value here its getting as null, so what should i do? My Edited code is as follows:

protected void btnsave_Click(object sender, EventArgs e)
{
Table t11 = (Table)PName_placeholder.FindControl("tblname");
        string pname = "";
        foreach (TableRow tr in t11.Rows)
        {
            foreach (TableCell tc in tr.Cells)
            {
                foreach (Control c in tc.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.ID.StartsWith("txtname"))
                        {
                            TextBox txt = (TextBox)t11.FindControl(c.ID);

                            pname = txt.Text;
                            Session["pname1"] = pname;
                        }
                    }
                }
            }
        }
}

protected void ddlpassenger_SelectedIndexChanged(object sender, EventArgs e)
    {
    int N = Convert.ToInt32(ddlpassenger.SelectedValue);
    Table t = new Table();
        t.ID = "tblname";

    for (int i = 0; i < N; i++)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            TextBox name = new TextBox();
            name.ID = "txtname" + i;
            cell.Controls.Add(name);
            row.Cells.Add(ce开发者_高级运维ll);
            t.Rows.Add(row);
            PName_placeholder.Controls.Add(t);
    }
    }

Asp.net c# Thank you.


You need to recreate the dynamic controls in the Page_Init method when its posted back.

protected void Page_Init(object sender, EventArgs e)
{
   if (IsPostBack)
  {
     //you dynamic control creation
  }
}

Then ASP.NET will populate them with values.


Where in the code is the null value happening? What exception are you seeing?

In your button_click event for the save, where is t defined? Is it passed in to the handler or is it a class level variable?

Sharing some more code will help get you an answer - the full methods of the selected index changed event handler and button click would be a good start.

Edited to Add

t is only in scope for the SelectdIndexChanged event handler. It's not in scope in the button click event handler, which is probably why you're seeing a problem with nulls.

Unless you're persisting that table somehow (outside the scope of the event handler) I don't see how you're going to be able to accomplish what you're doing.

Can you give a little more information regarding the controls you're creating dynamically? It's not very clear (to me at least) what role the table plays in this. Can you have more than one dynamic control at a time? What's the DropDownList used for, etc?

Second Edit In your SelectedIndexChanged event handler, assign an ID to the table your create, for example:

t.ID = "PassengerTable";

this will make it easier to get the table when the button is clicked.

protected void btn_Click(object sender, EventArgs e)
{

    Table t = (Table)PName_placeholder.Controls.FindControls("PassengerTable");

    string pname = "";

    foreach (TableRow tr in t.Rows)
    {
        foreach (TableCell tc in tr.Cells)
        {
            foreach (Control c in tc.Controls)
            {
              if (c is TextBox && c.ID.StartsWith("txtname"))
              {
                      pname = ((TextBox)t.FindControl(c.ID)).Text;

                      Session["pname1"] = pname;
              }
         }
     }
}

That should get you the value in each TextBox - but please note you'll need to do more than just stick the TextBox.Text value in Session - otherwise if you have more than one TextBox, you'll wind up with the last TextBox's value only.

Also note the other poster's answer about rendering the dynamic controls during pre-init on postback - if there's a possibility that a postback will occur between the time the user creates the dynamic controls and the time they click the button, you may want to handle the pre-init so they don't lose what they already had.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜