2013-05-13 49 views
0

嗨,我有一個包含靜態和動態文本框的webform。我已經寫了代碼來創建無限的動態文本框(最大使用將但我給了用戶靈活性),但現在我要插入數據庫中動態創建文本框的值將動態創建的文本框的值插入到sqlserver中並顯示在gridview中

想我填寫一些文本在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();   

    } 

回答

0

如果我的理解正確...

你需要清理你的代碼。確保您爲控件設置了唯一的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 

這是你需要什麼?

+0

嗨..感謝您的關注!我其實想從動態文本框中插入文本到數據庫,有沒有什麼辦法可以在運行時執行? – Rithu 2013-05-13 17:29:27

+0

我剛剛編輯了我的問題,希望它比以前更清晰。謝謝! – Rithu 2013-05-13 18:01:46

+0

看看這篇文章http://stackoverflow.com/questions/827175/entering-values-into-database-from-unlimited-dynamic-controls?lq=1這應該給你一個關於如何進行的想法。 – unangelic 2013-05-13 18:27:46

相關問題