简体   繁体   English

从克隆的jquery文本框值中将值存储在代码后面

[英]store value in code behind from cloned jquery textbox value

I have a button to clone a textbox, which allow the user to type in too 我有一个用于克隆文本框的按钮,该按钮也允许用户输入

jquery jQuery的

function generateRow() {
if (totalans == 9) {
    $('#<%= label2.ClientID %>').html('<b>Maximum of 10 answers per questions reached</b>');
}
else {
    totalans = totalans + 1; // same as totalans++;
     $("#ans").clone().attr({id: "ans_clone_" + totalans, name: "ans_clone_" + totalans}).prependTo("#ans2");
     // then you can loop through each input using the totalans variable.
}

Now, I have no idea on how to retrieve those textbox in codebehind c#. 现在,我不知道如何在c#背后的代码中检索这些文本框。 As I want to store it inside the database. 因为我想将其存储在数据库中。

I have got some help from someone here, but it still couldnt help me. 我从这里的某人那里得到了一些帮助,但仍然无法帮助我。

for(x=0; x<totalans; x++){ var tVal = $('#ans_clone_' + x).val(); //process }

im using vs2010, DotNet. 即时通讯使用vs2010,DotNet。

Add HiddenField server control onto page and use it to store count of dynamically added textboxes. HiddenField服务器控件添加到页面上,并使用它存储动态添加的文本框的数量。 Then, on post back parse it's value and fetch dynamically added textboxes values from Request.Params collection: 然后,在回发后解析它的值,并从Request.Params集合中获取动态添加的文本框值:

function addAnswer() {
     var hfAnswers = $("#<%= hfDynamicAnswers.ClientID %>");
     var answers = parseInt(hfAnswers.val()) + 1;
     hfAnswers.val(answers);

     $("#<%= tbAnswer.ClientID %>").clone().attr({ name: "ans_clone_" + answers, id: "ans_clone_" + answers }).appendTo("#answersContainer");
}

<asp:HiddenField runat="server" ID="hfDynamicAnswers" Value="0" />
<div id="answersContainer">
     <asp:TextBox runat="server" ID="tbAnswer" />
     <input type="button" value="Add Answer" onclick="addAnswer()" />
</div>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />

Code-behind: 后台代码:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    var answers = int.Parse(hfDynamicAnswers.Value);
    for (int i = 1; i <= answers; i++)
    {
        var answer = Request.Params["ans_clone_" + i.ToString()];
    }
}

ASP.NET should automatically serialize all your controls for you when you post them back. 当您将它们回发时,ASP.NET应该为您自动序列化所有控件。 Just make sure they have unique ids. 只要确保它们具有唯一的ID。

Request.Form["ans_clone_" + i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM