使用jquery获取客户端ID

时间:2014-11-25 14:05:39

标签: c# jquery asp.net

我尝试通过使用下面的内容获取客户端ID然后测试其值来验证文本框 文本框位于更新面板中的ModalPopupExtender中 但是JQuery似乎没有返回任何东西,控件总是为NULL 此文本框位于gridview

  <asp:TemplateField HeaderText="Quick Donate">
                                <ItemTemplate>
                                    <asp:Button ID="btnQuickDonate" runat="server"
                                        CommandName="Insert"
                                        CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                        OnClick="btnQuickDonate"
                                        Text="Quick Donate" />
                                    <ajaxToolkit:ModalPopupExtender ID="ModalDonationPopup" runat="server" TargetControlID="btnQuickDonate" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
                                    </ajaxToolkit:ModalPopupExtender>
                                    <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" Style="display: none">
                                        Enter Donation Amount<br />
                                        <table>
                                            <tr>
                                                <td></td>
                                                <td>
                                                    <asp:Button ID="btnOnePound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="1" />
                                                    <asp:Button ID="btnFivePound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="5" />
                                                    <asp:Button ID="btnTenPound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="10" />
                                                    <asp:Button ID="btnFifteenPound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="15" />
                                                    <asp:Button ID="btnTwentyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="20" />
                                                    <asp:Button ID="btnTwentyFivePounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="25" />
                                                    <asp:Button ID="btnThirtyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="30" />
                                                    <asp:Button ID="btnFiftyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="50" />

                                                </td>

                                            </tr>
                                            <%--  --%>
                                        </table>
                                        <br />

                                        <asp:TextBox ID="txtFreeDonationAmount" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  ></asp:TextBox>
                                        **<asp:Button ID="btnOK" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="OK" OnClientClick="return valtxtBoxFreeDonation();" OnClick="btnQuickDonate"></asp:Button>**
                                        <br />
                                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />

                                    </asp:Panel>

下面的Javascript我也试过$(&#39; #txtFreeDonationAmount&#39;)没有任何快乐......

function valtxtBoxFreeDonation() {
    var txtDonation = $('#<%=txtFreeDonationAmount.ClientID %>');

     if (txtDonation.val().length < 0) {
        // do something
        apprise("Please Enter a Donation Amount before click okay");
        return false;
    }

}

2 个答案:

答案 0 :(得分:4)

此代码:

var txtDonation = $('#<%=txtFreeDonationAmount.ClientID %>');
仅当JavaScript位于.aspx文件中时,

才能正常工作。

答案 1 :(得分:1)

大胆的解决方案是jsut将这个id'GridView1_btnOK_1`并在你的JS中使用它。

$('#GridView1_txtFreeDonationAmount_0')

正如提及的$('#<%=txtFreeDonationAmount.ClientID %>')只有当你的JS在同一个.aspx文件中时才会起作用。