Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将占位符控件中动态生成的文本框中的值插入数据库?_C#_Asp.net_Sql_Textbox - Fatal编程技术网

C# 如何将占位符控件中动态生成的文本框中的值插入数据库?

C# 如何将占位符控件中动态生成的文本框中的值插入数据库?,c#,asp.net,sql,textbox,C#,Asp.net,Sql,Textbox,我在按钮点击事件中动态生成了不止一个文本框。我还限制创建这些文本框最多5个。但我无法跟踪动态生成的文本框中插入的值以将其插入数据库表。 这就是我正在做的- <table border="1" width="1000px"> <tr> <td class="style1">Add Text Box: </td> <td class="style2"> <asp:TextBox ID="txt1

我在按钮点击事件中动态生成了不止一个文本框。我还限制创建这些文本框最多5个。但我无法跟踪动态生成的文本框中插入的值以将其插入数据库表。 这就是我正在做的-

<table border="1" width="1000px">
    <tr>
    <td class="style1">Add Text Box: </td>
    <td class="style2">
        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Add More" 
            onclick="Button1_Click" /><br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>      
        </td>
    </tr>
    <tr><td colspan="2" align="center">
        <asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />
        <br />
        </td></tr>
    </table>

添加文本框:


我的cs代码是-

public partial class DynamicTextbox : System.Web.UI.Page
{
    General_Logic g1 = new General_Logic();
    DataTable dt = new DataTable();
    int rows = 0;
    List<string> ControlIdList = new List<string>();
    int Counter = 1;
    protected override void LoadViewState(object SavedState)
    {
        base.LoadViewState(SavedState);
        ControlIdList = (List<string>)ViewState["ControlIdList"];
        foreach (string Id in ControlIdList)
        {
            Counter++;
            TextBox tb = new TextBox();
            tb.ID = Id;
            LiteralControl linebreak = new LiteralControl();
            PlaceHolder1.Controls.Add(tb);
            PlaceHolder1.Controls.Add(linebreak);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Counter <= 4)
        {
            Counter++;
            TextBox tb = new TextBox();
            tb.ID = "TextBox" + Counter;
            tb.Text = "";
            LiteralControl linebreak = new LiteralControl("<br />");
            PlaceHolder1.Controls.Add(tb);
            PlaceHolder1.Controls.Add(linebreak);
            ControlIdList.Add(tb.ID);
            ViewState["ControlIdList"] = ControlIdList;
        }
        else
        {
            Button1.OnClientClick = null;
            Response.Write("<script>alert('Maximum Entry is 5');</script>");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string s = null;
            for (int parcount = 0; parcount == parcount; parcount++)
            {
                s = Request.Form["Textbox" + parcount.ToString()];
                if (s == null)
                {
                    rows = g1.ExecDB("insert into tbl_products(product1) values('" + txt1.Text.ToString() + "')");
                    break;
                }
                else
                {
                    //Label1.Text = s.ToString();
                    Response.Write(s);
                    rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5) values('" + txt1.Text.ToString() + "','" + s.ToString() + "','" + s.ToString() + "','" + s.ToString() + "','" + s.ToString() + "')");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
public部分类DynamicTextbox:System.Web.UI.Page
{
通用逻辑g1=新的通用逻辑();
DataTable dt=新的DataTable();
int行=0;
List ControlIdList=新列表();
int计数器=1;
受保护的覆盖无效LoadViewState(对象保存状态)
{
base.LoadViewState(SavedState);
ControlIdList=(列表)视图状态[“ControlIdList”];
foreach(ControlIdList中的字符串Id)
{
计数器++;
TextBox tb=新的TextBox();
tb.ID=ID;
LiteralControl linebreak=新建LiteralControl();
占位符1.控件。添加(tb);
占位符1.控件。添加(换行符);
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{

如果(计数器将
s=Request.Form[“Textbox”+parcount.ToString()]中的文本框更改为文本框,即:

s = Request.Form["TextBox" + parcount.ToString()];
原因:

因为您创建了“TextBox”并通过“TextBox”进行检索

在看到您的逻辑后,我需要建议您遵循复杂的逻辑。因此,如果我理解您的逻辑,下面的代码将帮助您

protected void Button2_Click(object sender, EventArgs e)
    {
         try
        {
            string s = null;
            int  limit = 4;
            string[] DBvalues = new string[5];

            for (int parcount = 0; parcount <=limit; parcount++)
            {
                s = Request.Form["TextBox" + parcount.ToString()];
                if (parcount == 0)
                {
                    DBvalues[parcount] = txt1.Text;
                }
                else
                {
                    DBvalues[parcount] = Request.Form["TextBox" + (parcount + 1).ToString()];
                }


                //}
            }
         //converting null values to any default value.i take NULL  
          for (int i=0; i <= 4;i++ )
            {
                if (DBvalues[i] == null)
                {
                    DBvalues[i] = "NULL";
                }
            }
            rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5) values('" + DBvalues[0].ToString() + "','" + DBvalues[1].ToString() + "','" + DBvalues[2].ToString() + "','" + DBvalues[3].ToString() + "','" + DBvalues[4].ToString() + "')");
            Response.Write(DBvalues);
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
protectedvoid按钮2\u单击(对象发送者,事件参数e)
{
尝试
{
字符串s=null;
整数极限=4;
字符串[]DBvalues=新字符串[5];

对于(int parcount=0;parcount)您是否在按钮2\u Click事件上运行无限循环???@KarthikAMR是的。实际上我在搜索时从某个地方得到了此建议。我尝试使用此代码,但现在得到了此错误-System.String[]System.NullReferenceException:对象引用未设置为对象的实例。请参阅我的更新答案。您已将null值转换为任何默认值。因为将null转换为字符串会引发异常。您得到答案了吗?Response.Write(DBVALUES)。我希望这将导致errorcomment或删除“Response.Write(DBVALUES)”行.它只是显示数据。