2016-07-02 71 views
1

我有多個Html inputText在頁面加載時由C#生成。這是我的存儲過程:如何從C#生成的html輸入中獲取多個值

ALTER PROCEDURE [dbo].[CharacteristicsFetch] 
AS 
BEGIN 
    SELECT chId, chName, chDesc 
    FROM Characteristics 
    ORDER BY chName 
END 

和我的C#代碼:

public string generate_CharValues() 
{ 
    string post = ""; 

    DataTable dt = cls.Fill_In_DataTable("CharacteristicsFetch"); 

    if (dt.Rows.Count > 0) 
    { 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      post += "<input id='" + dt.Rows[i]["chId"] + "' name='txt" + dt.Rows[i]["chId"] + "' title='" + dt.Rows[i]["chName"] + "' type='number' required='required' />"; 
     } 
    } 

    return post; 
} 

我打電話從.aspx頁面這個功能,我想使用它:

charValues = cls.generate_CharValues(); 

該代碼將生成用戶必須提供值的多個html輸入。每個輸入文本都有一個與其數據庫ID相關的ID。現在我想獲得輸入值和輸入ID然後把它們放在一個數據表中發送到數據庫。

這是我在提交測試按鈕嘗試:

DataTable dtChar = new DataTable(); 

dtChar.Columns.AddRange(new DataColumn[3] { 
     new DataColumn("chId", typeof(string)), 
     new DataColumn("animalId", typeof(string)), 
     new DataColumn("Rank", typeof(string))}); 

string animalId = "1";//animalId 
int getcount = int.Parse(clsData.Select_One_Data("CharacteristicsGetCount", "chCount").ToString()); 

for(int i = 0; i < getcount; i++) 
{ 
    string chId = "";//input text id 
    string rank = "";//Request.Form["input text value"] 
    dtChar.Rows.Add(chId, animalId, rank); 
} 

與JS或JQuery的任何方法,我也會有幫助。由於

+0

IM使用SQLSERVER代碼遺憾2008 r2和asp.net 4.0 –

回答

0

我爲錯誤的答案 我在匆忙 試試這個 它入鍋,我和字符串

<form id="form1" runat="server"> 
    <div> 
     <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> 
     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
     <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="check()" /> 
     <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button1_Click"/> 
     <input type="hidden" id="hdn" runat="server" /> 
    </div> 
    <script> 
      function check() { 
      var val = document.getElementsByTagName("input"); 
      var d = ""; 
      for (var i = 0; i < val.length; i++) { 
       if (val[i].value != null && val[i].type == 'number') { 
        d += val[i].value+','; 
       } 
      } 
      d = d.slice(0, d.length-1); 
      document.getElementById("hdn").value = d; 
     } 
</script> 
</form> 

的回報清單,背後

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string cd = hdn.Value; 
List<string> values =cd.Split(',').ToList(); 
} 
+0

抱歉不起作用,並且與我的問題無關: –

+0

你可以做onclintclick和onclick爲同一個按鈕 –

相關問題