如何在for循环中保存多个Textbox和radiobutton的值 [英] How to save multiple Textbox and radiobutton's value in for loop

查看:66
本文介绍了如何在for循环中保存多个Textbox和radiobutton的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用linq到sql,我动态按照数据库的值创建textobes和radiobutton,



这是代码,

  var  r = 来自 a  in  db.EMR_EVENTs 
其中​​ a.EventID ==( int )会话[ eventid]


选择 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;
会话[ 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 = 电子邮件地址;
txtfirstname.Text = 名字;
txtlastname.Text = 姓氏;


chkmale.ID = chkmale + i。的ToString();
chkfemale.ID = chkfemale + i.ToString();


chkmale.Text = ;
chkfemale.Text = 女性;

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


}



如果我得到No_Of_Extra_Invitee = 2那么我有6个文本框和4个radiobuttons



现在我想保存这个控件的值(文本),虽然我给它默认文本,但我想保存它的运行时值,所以在提交按钮我实现这样,





 受保护  void 提交(对象发​​件人,EventArgs e)
{
int rccount = Convert.ToInt32(会话[ rc]);

ExtraInviteeBL eb = new ExtraInviteeBL();
for int i = 0 ; i< rccount; i ++)
{
// 添加值的逻辑textbox's和radiobutton在数据库中

}







但是现在我不知道这个文本框和radiobuttns的插值如何能有人给我建议

提前谢谢..!

解决方案

< blockquote>首先你宣布像



 TextBox txtfirstname; 
TextBox txtlastname;
TextBox txtemail;

RadioButton chkmale;
RadioButton chkfemale;

;



然后尝试简单的更新



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

txtfirstname = < span class =code-keyword> new TextBox();
txtlastname = new TextBox();
txtemail = new TextBox();

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







更新



i我正在使用此代码



for textbox



 DataTable dtSpci =  new  DataTable(); 
dtSpci.Columns.Add();
foreach (控制控件 in pnl_details.Controls)
{
TextBox txtTemp =控制 TextBox;
if (txtTemp TextBox)
{
dtSpci.Rows 。新增(txtTemp.Text);
}
}





单选按钮



更改上面的代码,将文本框替换为radiobutton



注意

这里保存将文本框名称转换为数据表...



插入表格



 inser  into  table_name values(dtSpci.Rows [ 1 ].ItemArray.GetValue( 0 )。ToString(),dtSpci.Rows [ 2 ] .ItemArray.GetValue( 0 )。ToString()等... 





试试这段代码,希望它有点帮助...


我得到了解决方案,但我仍然对它如何在数据库中保存价值感到困惑,

就像假设我有这种类型的解决方案



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







假设我有这张桌子,



id |名字

1 ktn

2 ravi

3 vijay



这里是ktn, ravi和vijay是我的txtfirstname文本框的值,用户在运行时输入。

然后我如何将其保存为这样,



i'我很困惑,任何解决方案都是最受欢迎的。


嘿,这样做但现在我遇到了主要问题,这是我的代码,



用于创建控件我使用此方法:



  private   void  generateDynamicControls()
{

int rowCount = Convert.ToInt32(会话[ 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 = 电子邮件地址;
txtfirstname.Text = 名字;
txtlastname.Text = 姓氏;


chkmale.ID = chkmale + i。的ToString();
chkfemale.ID = chkfemale + i.ToString();


chkmale.Text = ;
chkfemale.Text = 女性;

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

}

}





保存它的价值我使用这种方法,



  protected  无效提交(对象发​​件人,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(会话[ inviteby]);


}
eb.insert();
}







现在所有记录只保存在一个id中,在表格中我有客人表,它包含guestid,这是主键。

它自动增加然后我如何保存它的值,因为for循环增加,

(我的意思是我的值增量)

给我建议请



提前致谢。


I use linq to sql,i dynamicallu create textobes and radiobuttons as per value of database,

Here is the code for that,

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,


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

解决方案

first you declare like

TextBox txtfirstname;
TextBox txtlastname;
TextBox txtemail;

RadioButton chkmale ;
RadioButton chkfemale ;

;

then try your with simple update

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

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

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


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

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.


Hey do this but now i face the main problem,here is the my code,

for create control i use this method:

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,

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.


这篇关于如何在for循环中保存多个Textbox和radiobutton的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆