使用jquery获取客户端ID

问题描述:

我试图验证一个文本框,通过使用下面的获取客户端ID然后测试其值 该文本框位于更新面板中的ModalPopupExtender 然而,JQuery似乎没有返回任何东西,控制始终是NULL 该文本框是一个gridview使用jquery获取客户端ID

<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下面我也曾尝试在$( '#txtFreeDonationAmount')没有快乐...

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

检查所生成的HTML,看看ID属性存在有 – 2014-11-25 14:06:25

+0

----这是在调试器中返回的html代码的代码,看起来ID已经发生了相当大的变化 – AShah 2014-11-25 14:15:29

+0

---这是正确的输出 – AShah 2014-11-25 14:26:09

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

$('#GridView1_txtFreeDonationAmount_0') 

而作为对$('#<%=txtFreeDonationAmount.ClientID %>') mentiones只会工作,如果你的JS是在同一个.aspx文件。

+0

感谢这似乎工作 – AShah 2014-11-25 14:55:17

这代码:

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

只有当JavaScript在.aspx文件中时才能正常工作。