当我提交表单时,标签控件背后的代码给我一个空值。如何将值添加到局部变量?
请告知。
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 PAGE
<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;
}
答案 0 :(得分:1)
因为我无法确定您在哪里找到此Label控件。我会给你选择。
首先:在页面加载或其他一些功能
Label courseAmount = (Label)courseListView.Rows[0].Cells[0].FindControl("lblTotal");
第二:在courseListView_RowDataBound
中,您可以像:
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl= (Label)e.Row.FindControl("lblTotal");
}
第三:
protected void courseListView_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow valu = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int RowIndex = valu.RowIndex;
Label value = (Label)courseListView.Rows[RowIndex].FindControl("lblTotal");
string course = value.Text;
}