簡體   English   中英

將動態創建的文本框的值插入sqlserver並在gridview中顯示

[英]Insert values of dynamically created textboxes into sqlserver and display in gridview

嗨,我有一個包含靜態和動態文本框的Webform。 我已經編寫了一個代碼來創建無限的動態文本框(最多使用31個,但我給了用戶靈活性), 但是現在我必須在數據庫中插入動態創建的文本框的值

假設我在1個文本框中填充了一些文本,它會自動創建下一個文本框,即,當我在第一個文本框中輸入1個字符時,它會生成一個新的文本框,最后,當我單擊“提交”時,我希望將文本框中的所有這些數據存儲在數據庫中。

它應該為每個生成的新文本框創建新的字段/列,但是如果它在最大限制之內,則應該在先前創建的列中插入數據 有什么辦法嗎?

這是我的代碼: WebForm1.aspx

<script type="text/javascript">

    getId = function ()
     {
        var id = 1;
        return function () 
        {
            id++;
        }
    }

    function CreateTextbox() 
    {
        var box = document.getElementById("divCreateTextbox");
        var curr = 'txt' + getId();
        var inp = document.createElement('input');
        inp.type = 'text';
        inp.name = 'textfield';
        inp.setAttribute("id", 'curr');
        inp.setAttribute("minlength", '1');
        box.appendChild(inp);
        inp.setAttribute('onkeyup', 'moveOnMin(this)');
        inp.setAttribute("textBoxAdded", "0");
        inp.focus();

    }

    function moveOnMin(s)
     {
         if (s.value.length == parseInt(s.getAttribute("minlength")) && s.getAttribute("textBoxAdded") == "0")
         {
            CreateTextbox();
            s.setAttribute("textBoxAdded", "1");
            s.focus();
        }
    }

        <div id="divCreateTextbox"></div>
        <script type="text/javascript">
        window.onload = function ()
       {
            CreateTextbox()
       }

WebForm1.aspx.cs (將靜態文本框文本插入sql的代碼)

public partial class WebForm1 : System.Web.UI.Page
{

     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {



        if (!IsPostBack)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(CS))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Tbl Return's Tracker]", connection);
                DataSet ds = new DataSet();
                da.Fill(ds);

                GridView1.DataBind();
            }
            }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {

        SqlCommand command = new SqlCommand("INSERT INTO [Tbl Return's Tracker] ([Company],[Date],[Month],[OrderReference],[Product],[Status],[ReturnLabel],[NumberOfLenses],[Shelf], [ReshipmentNumber],[Reason1],[Reason2],[ActionTaken],[ReturnHandledby])VALUES (@company,@date,@month,@orderreferencenumber,@product,@status,@returnlabel,@numberoflenses,@shelf,@reshipmentordernumber,@Reason1,@Reason2,@action,@name)", connection);
        command.Parameters.AddWithValue("@company", DdlCompany.SelectedItem.Text);
        command.Parameters.AddWithValue("@month", LblMonth.Text);
        command.Parameters.AddWithValue("@date", LblDate.Text);
        command.Parameters.AddWithValue("@orderreferencenumber", TxtOrderReferenceNo.Text);
        command.Parameters.AddWithValue("@product", DdlProduct.SelectedItem.Text);
        command.Parameters.AddWithValue("@status", DdlStatus.SelectedItem.Text);
        command.Parameters.AddWithValue("@returnlabel", DdlReturnLabel.SelectedValue);
        command.Parameters.AddWithValue("@numberoflenses", TxtNumberOfLenses.Text);
        command.Parameters.AddWithValue("@shelf", DdlShelf.SelectedItem.Text);
        command.Parameters.AddWithValue("@reshipmentordernumber", TxtReshipmentOrderNo.Text);
        command.Parameters.AddWithValue("@Reason1", DdlReason1.SelectedItem.Text);
        command.Parameters.AddWithValue("@Reason2", DdlReason2.SelectedItem.Text);
        command.Parameters.AddWithValue("@action", DdlAction.SelectedItem.Text);
        command.Parameters.AddWithValue("@Name", DdlName.SelectedItem.Text);

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
        GridView1.DataBind();         

    }

如果我正確理解你...

您需要清理代碼。 確保為控件設置了唯一的ID。 您的getid不返回ID-調試並簽出。 使var id為全局,並在您的CreateTextBox Func中更正此代碼

var box = document.getElementById("divCreateTextbox");
var curr = 'txt' + id++; // getId();
var inp = document.createElement('input');
inp.type = 'text';
inp.name = curr;  //'textfield';
inp.setAttribute("id", curr);

在回發中,通過控件循環

 For Each txtBx As String In Request.Form
        If txtBx.StartsWith("txt") Then 
 Response.Write(txtBx & "=" & Request.Form(txtBx) & "<BR>")
 end if
    Next

這是您需要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM