使用asp.net中的jquery获取用户控件元素值

时间:2014-09-26 11:54:37

标签: jquery asp.net

我想使用jquery在我的aspx页面中获取usercontrol标签值。这是我的用户控件

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Group.ascx.cs" Inherits="website.Inventory.Setup.UserControl.Group" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <style type="text/css">
        .style1
        {
            width: 100%;
            margin-top: 0px;
        }
        .style2
        {
            width: 12%;
        }
    </style>
    <table class="style1">
        <tr>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <table class="style1">
                    <tr>
                        <td style="width: 2%">
                            &nbsp;
                        </td>
                        <td class="style2" style="width: 13%">
                            &nbsp;&nbsp;
                        </td>
                        <td style="width: 22%">
                            &nbsp;&nbsp;
                        </td>
                        <td style="width: 68%">
                            &nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td align="right" class="error">
                            *
                        </td>
                        <td class="style2">
                            <asp:Label ID="Label6" runat="server" Text="Group:"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtNameAdd" runat="server" MaxLength="15" ValidationGroup="grpLevelAdd"
                                Width="164px" CssClass="req_bor"></asp:TextBox>
                        </td>
                        <td>
                            <asp:RequiredFieldValidator ID="rfvNameAdd" runat="server" ControlToValidate="txtNameAdd"
                                Display="None" ErrorMessage="Required!" SetFocusOnError="True" ValidationGroup="grpGroupAdd"></asp:RequiredFieldValidator>
                            <asp:ValidatorCalloutExtender ID="rfvNameAdd_ValidatorCalloutExtender" runat="server"
                                Enabled="True" TargetControlID="rfvNameAdd">
                            </asp:ValidatorCalloutExtender>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td class="style2">
                            <asp:Label ID="Label20" runat="server" Text="Description:"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtDescriptionAdd" runat="server" MaxLength="15" ValidationGroup="grpLevelAdd"
                                Width="164px" TextMode="MultiLine"></asp:TextBox>
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td class="style2">
                            <asp:Label ID="Label15" runat="server" Text="Active:"></asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="chkActiveAdd" runat="server" Text="Yes" Checked="True" />
                        </td>
                        <td>
                            &nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td class="style2">
                            &nbsp;&nbsp;
                        </td>
                        <td height="20px" valign="middle" colspan="2">
                            <asp:Label ID="lblmsgAdd" runat="server" CssClass="error"></asp:Label>
                            &nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td class="style2">
                            &nbsp;&nbsp;
                        </td>
                        <td colspan="2">
                            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" ValidationGroup="grpGroupAdd" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td class="style2">
                            &nbsp;&nbsp;
                        </td>
                        <td>
                            &nbsp;&nbsp;
                        </td>
                        <td>
                            &nbsp;&nbsp;
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

</table>

这是我的控件

<uc1:Group ID="UCGroup" ClientIDMode="Static"  runat="server" />

我想在我的aspx页面中获取lblmsgAdd值,我这样做

 var a = $('#<%=UCGroup.ClientID%>').find('#<%=lblmsgAdd.ClientID%>').val();

但这不起作用,我如何获得我的标签价值。 需要帮助

1 个答案:

答案 0 :(得分:1)

在您的用户控件中创建一个公共属性,返回您的Label的ClientID,如下所示

public string AddMessageClientId
{
   get
   {
      return lblmsgAdd.ClientID;
   }
}

然后在你的JQuery中调用它,如下所示

var a = $('#<%= AddMessageClientId %>').val();