如何从C#生成的html输入中获取多个值

时间:2016-07-02 08:43:26

标签: javascript jquery html asp.net sql-server-2008-r2

我有多个Html inputText,它是由C#在页面加载时生成的。这是我的存储过程:

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的任何方法也会对我有所帮助。谢谢

1 个答案:

答案 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();
}