Click here to Skip to main content
15,883,975 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I use linq to sql,i dynamicallu create textobes and radiobuttons as per value of database,

Here is the code for that,
C#
var r = from a in db.EMR_EVENTs
                        where a.EventID == (int)Session["eventid"]


                        select new
                        {
                            No_Of_Extra_Invitee=a.Allowed_guest_per_invitee
                        };

                var ev1 = r.First();
                int? count = ev1.No_Of_Extra_Invitee;
           
              
                int? rowCount = ev1.No_Of_Extra_Invitee;
                Session["rc"] = rowCount;
      
        for (int i = 0; i < rowCount; i++)
        {

            TextBox txtfirstname = new TextBox();
            TextBox txtlastname = new TextBox();
            TextBox txtemail = new TextBox();

            RadioButton chkmale = new RadioButton();
            RadioButton chkfemale = new RadioButton();


            txtfirstname.ID = "txtfirstname" + i.ToString();
            txtlastname.ID = "txtlastname" + i.ToString();
            txtemail.ID = "txtemail" + i.ToString();
            txtfirstname.CssClass = "form-control";
            txtlastname.CssClass = "form-control";
            txtemail.CssClass = "form-control";
            txtemail.Text = "E-Mail Address";
            txtfirstname.Text = "First Name";
            txtlastname.Text = "Last Name";

            
            chkmale.ID = "chkmale" + i.ToString();
            chkfemale.ID = "chkfemale" + i.ToString();


            chkmale.Text = "Male";
            chkfemale.Text = "Female";

            pnl.Controls.Add(txtfirstname);
            pnl.Controls.Add(txtlastname);
            pnl.Controls.Add(txtemail);
            pnl.Controls.Add(chkmale);
            pnl.Controls.Add(chkfemale);
        

        }

if i got No_Of_Extra_Invitee=2 then i got 6 textbox and 4 radiobuttons

Now i want to saved this control's value(text),though i give it default text,but i want to save it's runtime value so in submit button i implement like this,


C#
protected void Submit(object sender, EventArgs e)
        {
            int rccount = Convert.ToInt32(Session["rc"]);

            ExtraInviteeBL eb = new ExtraInviteeBL();
            for (int i = 0; i <rccount; i++)
            {
                //logic for add value of textbox's and radiobutton's in database

            }



but now i don't know how insert value of this textbox and radiobuttns can anyone give me suggestion for that
Thanks in advance..!
Posted
Updated 19-Dec-13 19:16pm
v2

first you declare like

C#
TextBox txtfirstname;
TextBox txtlastname;
TextBox txtemail;

RadioButton chkmale ;
RadioButton chkfemale ;
;

then try your with simple update

C#
 ................
................................
for (int i = 0; i < rowCount; i++)
        {
 
            txtfirstname = new TextBox();
            txtlastname = new TextBox();
            txtemail = new TextBox();
 
            chkmale = new RadioButton();
            chkfemale = new RadioButton();
.............
.............



Updated

i am using this code

for textbox

C#
DataTable dtSpci = new DataTable();
dtSpci.Columns.Add();
foreach (Control controls in pnl_details.Controls)
{
    TextBox txtTemp = controls as TextBox;
    if (txtTemp is TextBox)
    {
        dtSpci.Rows.Add(txtTemp.Text);
    }
}


for radio button

change the above code,replace textbox in to radiobutton

Note
here save the textbox name into datatable ...

Insert into Table

C#
inser into table_name values (dtSpci.Rows[1].ItemArray.GetValue(0).ToString(),dtSpci.Rows[2].ItemArray.GetValue(0).ToString() etc...


try this code, hope its helps a bit...
 
Share this answer
 
v4
Comments
ketan italiya 20-Dec-13 1:49am    
but this is not solution yar,i think so this is how to create new control ,i want to save it's value.
like that if i have this 6 textbox,
txtfirstname1.text=ravi
txtfirstname1.text=vijay
.
.
.
.
as per all control's value.
An@nd Rajan10 20-Dec-13 2:17am    
as per your comment , update my solution please see...
ketan italiya 20-Dec-13 2:14am    
hey,if is possible with this event of textbox,

protected void txtfirst_changed(object sender, EventArgs e)
{

}
An@nd Rajan10 20-Dec-13 2:21am    
see my updated solution and comment please ?
ketan italiya 20-Dec-13 2:23am    
yeh,Thanks for your solution bro.
i check it
i got solution of this but i'm still confuse on it that how i save value in database,
like suppose i have this type of solution

C#
TextBox ti = (TextBox)pnl.FindControl("txtfirstname" + i.ToString());
 eb.First_Name = Session["txtfirstname" + i.ToString()].ToString();



suppose i have this table,

id | firstname
1 ktn
2 ravi
3 vijay

here ktn,ravi and vijay are my txtfirstname textbox's value which is user enter at runtime.
then how i save it in like this,

i'm confuse about it,any solution is most welcome.
 
Share this answer
 
Hey do this but now i face the main problem,here is the my code,

for create control i use this method:

C#
private void generateDynamicControls()
       {

          int  rowCount=Convert.ToInt32(Session["rc"]);

           for (int i = 0; i < rowCount; i++)
           {

               TextBox txtfirstname = new TextBox();
               TextBox txtlastname = new TextBox();
               TextBox txtemail = new TextBox();

               RadioButton chkmale = new RadioButton();
               RadioButton chkfemale = new RadioButton();


               txtfirstname.ID = "txtfirstname" + i.ToString();
               txtlastname.ID = "txtlastname" + i.ToString();
               txtemail.ID = "txtemail" + i.ToString();
               txtfirstname.CssClass = "form-control";
               txtlastname.CssClass = "form-control";
               txtemail.CssClass = "form-control";
               txtemail.Text = "E-Mail Address";
               txtfirstname.Text = "First Name";
               txtlastname.Text = "Last Name";


               chkmale.ID = "chkmale" + i.ToString();
               chkfemale.ID = "chkfemale" + i.ToString();


               chkmale.Text = "Male";
               chkfemale.Text = "Female";

               pnll.Controls.Add(txtfirstname);
               pnll.Controls.Add(txtlastname);
               pnll.Controls.Add(txtemail);
               pnll.Controls.Add(chkmale);
               pnll.Controls.Add(chkfemale);

           }

       }


for save it's value i use this method,

C#
protected void Submit(object sender, EventArgs e)
       {

 int rccount = Convert.ToInt32(Session["rc"]);
            string strValue1 = string.Empty;
            string strValue2 = string.Empty;
            string strValue3 = string.Empty;
            ExtraInviteeBL eb = new ExtraInviteeBL();
            for (int i = 0; i < rccount; i++)
            {
                string firstname = "txtfirstname" + i.ToString();
                string lastname = "txtlastname" + i.ToString();
                string mail = "txtemail" + i.ToString();

                TextBox tb = pnll.FindControl(firstname) as TextBox;
                TextBox tb1 = pnll.FindControl(lastname) as TextBox;
                TextBox tb2 = pnll.FindControl(mail) as TextBox;
                strValue1 += tb.Text + "\n";
                strValue2 += tb.Text + "\n";
                strValue3 += tb.Text + "\n";
                eb.First_Name = strValue1;
                eb.Last_Name = strValue2;
                eb.E_mailID = strValue3;
                eb.Ive_Invited_By = Convert.ToInt32(Session["invitedby"]);

               
            }
eb.insert();
}



now all records save only in one id,in table i have guest table,and it contain guestid,which is primary key.
it increment automatically then how i save it's value as per for loop increases,
(i mean i's value increment)
give me suggestion please

Thanks in advance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900