【问题标题】:How to get value from auto generated multiple texboxes in asp.net?如何从asp.net中自动生成的多个文本框中获取价值?
【发布时间】:2015-06-30 02:15:36
【问题描述】:

我用 jquery 代码创建了自动文本框。但我无法从他们那里得到价值。这是我生成文本框的代码

$('#btn').click(function () {
$(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;">
<b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag">
<input type=text class="input" id=txtdates' + iCnt + ' ' + ' /></td>
</tr>');
}

我可以使用此代码添加文本框。如何从这些代码中获得价值?

【问题讨论】:

    标签: c# jquery asp.net textbox auto-generate


    【解决方案1】:

    首先导入这个命名空间

    using System.Web.Script.Serialization;
    

    接下来将名为 name 的属性添加到动态创建的文本框,如下所示

    $('#btn').click(function () {
           $(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;">
                <b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag">
                <input type="text" name="DynamicTextBox" class="input" id="txtdates"' + iCnt + ' ' + ' />
               </td>
               </tr>');
    }
    

    在您的服务器方法中,您可以按如下方式访问它

    public void Post(object sender, EventArgs e)
    {
        string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        this.Values = serializer.Serialize(textboxValues);
        string message = "";
        foreach (string textboxValue in textboxValues)
        {
            message += textboxValue + "\\n";
        }
    }
    

    Source

    【讨论】:

    • 随时!!快乐编码.. :)
    【解决方案2】:

    这是我对您的问题的看法:

    $('#btn').click(function () {
        var iCnt = 1;
        $('#container').append($('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;"><b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag"><input type="text" class="input" id="txtdates' + iCnt + '" ' + ' /></td></tr>'));
    });
    
    $('#btnget').click(function () {
        alert($('#container').find('input:text').val());
    });
    
    <div id='container'></div>
    <input type='button' id='btn' value='submit'/>
    <input type='button' id='btnget' value='get value'/>
    

    演示:https://jsfiddle.net/hxq5cj0x/

    【讨论】:

    • 是的,我可以从 javascript 获取值,但不知道如何在 asp.net 代码上执行此操作。因为我会将它们写入数据库
    • @enderaric 添加一个 asp.net 隐藏字段,在上面的代码中而不是 alert 将文本值绑定到隐藏字段。然后,在codebehind中读取hiddenfield的值?
    • 我正在尝试你的答案。我会把结果写在这里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多