【发布时间】:2014-07-01 06:16:58
【问题描述】:
当前,当我提交表单时,标签控件背后的代码给了我一个空值。如何将值添加到局部变量?
请指教。
JAVASCRIPT
$(document).ready(function () {
$(".chk").change(function () {
var total = 0;
var chks = $(".chk input:checked");
if (chks.length > 0) {
for (var i = 0; i < chks.length; i++) {
total += parseFloat($("#" + chks[i].id.replace("courseID", "lblcoursePrice")).html());
$("#<%=courseListView.ClientID %> [id*=hfTotal]").val("");
}
}
$("#<%=courseListView.ClientID %> [id*=lblTotal]").text(total.toFixed(2));
$("#<%=courseListView.ClientID %> [id*=hfTotal]").val($(this).val());
});
});
.NET 页面
<asp:GridView ID="courseListView" AutoGenerateColumns="false" runat="server" ShowFooter="true">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="courseID" class="chk" runat="server" />
<asp:Label ID="courseTitle" Text='<%# Eval("name") %>' runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" Text="Total" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblcoursePrice" Text='<%# Eval("price") %>' runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" Text="" runat="server"></asp:Label>
<asp:HiddenField ID="hfTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏
Label courseAmount = (Label)FindControl("lblTotal");
if (courseAmount != null)
{
course = courseAmount.Text; //courseButtonList.SelectedValue;
}
【问题讨论】:
-
两件事:更小心你的标签(这显然不是 MVC 代码),并注意 JavaScript 肯定 不是 Java。
标签: c# jquery asp.net .net c#-5.0