【问题标题】:How to get label text that is changed with jquery inside gridview from code behind如何从后面的代码中获取在gridview中使用jquery更改的标签文本
【发布时间】: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


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多