[英]Retrieving A Value From HTML in ASP.NET
How do you retrieve a value from HTML in ASP.NET C#? 如何在ASP.NET C#中从HTML检索值? I'm using a flat JSON file and fetching the data via Ajax and output the results with HTML.
我正在使用一个平面JSON文件,并通过Ajax获取数据,并使用HTML输出结果。 Normally this would work if you're using asp:ListItem.... tag.
通常,如果您使用的是asp:ListItem ....标签,则此方法有效。 But I'm having trouble with retrieving the the value from the HTML tag upon form submission.
但是我在提交表单时从HTML标记中检索值时遇到了麻烦。
jQuery jQuery的
$.ajax({
url: "js/covers.json",
dataType: "JSON",
success: function (data) {
var $select = $("#customCoversDD");
$select.empty().append('<option value="">- Please Select One -</option>');
$.each(data.custom, function (key, val) {
$select.append('<option id= "customCoversList" value="' + val.description + '">' + val.description + '</option>');
});
}
});
ASP.NET ASP.NET
<asp:RadioButton ID="customCover" Text="Custom Cover" GroupName="covers" runat="server"/>
<asp:DropDownList ID="customCoversDD" runat="server">
</asp:DropDownList>
C# C#
string coverChoice = "";
if (customCover.Checked)
{
coverChoice = customCoversDD.SelectedValue;
};
You're giving each option element the same id of 'customCoversList'. 您要为每个选项元素赋予相同的'customCoversList'ID。 Try this...
尝试这个...
$.each(data.custom, function (key, val) {
$select.append(
$('<option></option>').val(val.description).html(val.description)
);
);
I believe this is happening due to the values of the drop down list not being in viewstate. 我认为这是由于下拉列表的值不在viewstate中而发生的。
Try this instead: Request.Form("customCoversDD");
请尝试以下方法:
Request.Form("customCoversDD");
That would get you the selected value of the drop down list. 这将为您提供下拉列表的选定值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.