如何在数据库中保存动态控制值? [英] how to save dynamic control values in database?

查看:88
本文介绍了如何在数据库中保存动态控制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查以下代码一次:

Please check below code once:

protected void Page_Load(object sender, EventArgs e)
{
Table table = new Table();
table.ID = "tbl_dy";
 
Page.Form.Controls.Add(table);
if (!IsPostBack)
{
for (int i = 1; i < 3; i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
Label lbl = new Label();
lbl.ID = "lbl_'" + i + "'";
if (i == 1)
{
lbl.Text = "EName";
}
else { lbl.Text = "Salary"; }
cell.Controls.Add(new LiteralControl(" "));
TextBox txt = new TextBox();
txt.ID = "txt_'" + i + "'";
cell.Controls.Add(new LiteralControl("
"));
cell.Controls.Add(lbl);
cell.Controls.Add(txt);
row.Cells.Add(cell);
table.Rows.Add(row);
}
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
Label lbl1 = new Label();
lbl1.ID = "lbl_gender";
lbl1.Text = "Gender";
DropDownList ddl = new DropDownList();
ddl.Width = 100;
ddl.Height = 25;
ddl.ID = "ddl_gender";
ddl.Items.Add("Select");
ddl.Items.Add("Male");
ddl.Items.Add("Female");
cell1.Controls.Add(lbl1);
Button btn = new Button();
btn.Width = 60;
btn.Height = 40;
btn.ID = "btn_save";
btn.Text = "Save";
cell1.Controls.Add(new LiteralControl("  "));
cell1.Controls.Add(ddl);
cell1.Controls.Add(new LiteralControl("
"));
cell1.Controls.Add(btn);
row1.Cells.Add(cell1);
table.Rows.Add(row1);
}
}



现在,我想将数据保存在数据库中.



Now i want to save the data in my database.

推荐答案

HI , 
check this one, by this solution you can create as many as 
you want of text box and u can retrieve the  data from it . and it also can be use this code on various Controls

just let me know your feedback .







<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    &nbsp;<asp:Button ID="Button2" runat="server" Text="show data"

        onclick="Button2_Click" />

      <div id="DivContent" runat="server">
       </div>
    </form>
</body>


背后的代码:


Code behind :

protected System.Web.UI.WebControls.TextBox txtSkill;
    protected System.Web.UI.WebControls.TextBox txtVersion;
    protected void Page_Load(object sender, EventArgs e)
    { 
    }     
    int countTimes = 0;
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["countTimes"] == null)
        {
            countTimes = 1;
        }
        else
        {
            countTimes = Convert.ToInt32(ViewState["countTimes"]);
        }

        for (int i = 0; i < countTimes; i++)
        {
            txtSkill = new TextBox();
            txtSkill.ID = "txtSkill" + i;


            txtVersion = new TextBox();
            txtVersion.ID = "txtVersion" + i;

            form1.Controls.Add(txtSkill);
            form1.Controls.Add(txtVersion);

        }
        countTimes = countTimes + 1;

        ViewState.Add("countTimes", countTimes);   

    } 
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ViewState["countTimes"] == null)
        {
            countTimes = 1;
        }
        else
        {
            countTimes = Convert.ToInt32(ViewState["countTimes"]);
        }

        for (int i = 0; i < countTimes -1; i++)
        {
            txtSkill = new TextBox();
            txtSkill.ID = "txtSkill" + i;


            txtVersion = new TextBox();
            txtVersion.ID = "txtVersion" + i;

            form1.Controls.Add(txtSkill);
            form1.Controls.Add(txtVersion);

        }


        string storeToDbValueOne = ""; string storeToDbValueTwo = "";
        for (int i = 0; i < countTimes; i++)
        {
            storeToDbValueOne += Request.Form["txtSkill" + i];

            storeToDbValueOne += Request.Form["txtVersion" + i];
        }

        Response.Write("<script>alert('"+storeToDbValueOne +" "+storeToDbValueTwo+"')</script>");


    }



最好的问候
米特瓦里(M.Mitwalli)



Best regards
M.Mitwalli


这篇关于如何在数据库中保存动态控制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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