javascript
  • jquery
  • asp.net
  • gridview
  • 2014-03-19 97 views 0 likes 
    0

    我的GridViewContentTemplate包含ItemTemplateItemTemplatetextboxbutton如何獲取gridview裏面的文本框的值

    以下是代碼片段:

     <asp:TemplateField HeaderText="Email"> 
         <ItemTemplate> 
         <asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("CustEmail") %>' /> 
           <asp:Button id="btnUpdate" Text="Update Email" ButtonType="Button" CommandName="UpdateEmail" 
           Visible="true" runat="server" 
           OnClientClick="return ValidateEmail(this)" /> 
    
         </ItemTemplate> 
        </asp:TemplateField> 
    

    我在做什麼在呼喚ValidateEmail()方法來驗證我的電子郵件領域。爲此,我需要獲取電子郵件字段值來驗證它。

    這是我ValidateEmail()方法:

     function ValidateEmail(btnObj) 
         { 
    
         alert(btnObj.id); 
         var email = $(btnObj).closest('input').attr('id'); 
         alert(email) 
         return false; 
        } 
    

    我能夠得到id我的按鈕,這是MainContent_GridView1_btnUpdate_0

    這是我爲GridView呈現的HTML:

    <div> 
    <table cellspacing="0" id="MainContent_GridView1" style="border-collapse:collapse;"> 
        <tr> 
         <th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$MainContent$GridView1&#39;,&#39;Sort$CustID&#39;)">Customer ID</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$MainContent$GridView1&#39;,&#39;Sort$CustFirstName&#39;)">First Name</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$MainContent$GridView1&#39;,&#39;Sort$CustLastName&#39;)">Last Name</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$MainContent$GridView1&#39;,&#39;Sort$CustCity&#39;)">City</a></th><th scope="col">Email</th> 
        </tr><tr> 
         <td> 
          <span id="MainContent_GridView1_lblCustID_0">12</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblFirstName_0">Anders</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblLastName_0">Rohansen</span> 
         </td><td> 
         <span id="MainContent_GridView1_lblCity_0">Takoma Park</span> 
         </td><td> 
         <input name="ctl00$MainContent$GridView1$ctl02$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_0" /> 
          <span data-val-controltovalidate="MainContent_GridView1_txtEmail_0" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_0" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span> 
          <input type="submit" name="ctl00$MainContent$GridView1$ctl02$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$GridView1$ctl02$btnUpdate&quot;, &quot;&quot;, true, &quot;grpEmail&quot;, &quot;&quot;, false, false))" id="MainContent_GridView1_btnUpdate_0" ButtonType="Button" /> 
    
         </td> 
        </tr><tr style="background-color:#EEEEEE;"> 
         <td> 
          <span id="MainContent_GridView1_lblCustID_1">8</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblFirstName_1">Deborah</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblLastName_1">Damien</span> 
         </td><td> 
         <span id="MainContent_GridView1_lblCity_1">Fresno</span> 
         </td><td> 
         <input name="ctl00$MainContent$GridView1$ctl03$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_1" /> 
          <span data-val-controltovalidate="MainContent_GridView1_txtEmail_1" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_1" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span> 
          <input type="submit" name="ctl00$MainContent$GridView1$ctl03$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$GridView1$ctl03$btnUpdate&quot;, &quot;&quot;, true, &quot;grpEmail&quot;, &quot;&quot;, false, false))" id="MainContent_GridView1_btnUpdate_1" ButtonType="Button" /> 
    
         </td> 
        </tr><tr> 
         <td> 
          <span id="MainContent_GridView1_lblCustID_2">7</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblFirstName_2">Derek</span> 
         </td><td> 
          <span id="MainContent_GridView1_lblLastName_2">Chaddick</span> 
         </td><td> 
         <span id="MainContent_GridView1_lblCity_2">Fairfield</span> 
         </td><td> 
         <input name="ctl00$MainContent$GridView1$ctl04$txtEmail" type="text" value="[email protected]" id="MainContent_GridView1_txtEmail_2" /> 
          <span data-val-controltovalidate="MainContent_GridView1_txtEmail_2" data-val-errormessage="Must enter Email Address" data-val-validationGroup="grpEmail" id="MainContent_GridView1_ctl00_2" data-val="true" data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid" data-val-initialvalue="" style="visibility:hidden;">Must enter Email Address</span> 
          <input type="submit" name="ctl00$MainContent$GridView1$ctl04$btnUpdate" value="Update Email" onclick="return ValidateEmail(this);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$GridView1$ctl04$btnUpdate&quot;, &quot;&quot;, true, &quot;grpEmail&quot;, &quot;&quot;, false, false))" id="MainContent_GridView1_btnUpdate_2" ButtonType="Button" /> 
    
         </td> 
        </tr> 
    </table> 
    </div> 
    

    我如何獲得電子郵件文本框的ID?

    謝謝

    回答

    1
    function ValidateEmail(btnObj) {    
         alert(btnObj.id); 
         var email = $(btnObj).siblings('input:text').attr('id'); 
         alert(email); 
         return false; 
        } 
    

    這將返回使用JavaScript解決方案文本輸入的ID。

    使用你的一些代碼工作example

    +0

    這是有效的。大。謝啦。我現在如何獲得電子郵件文本框的價值?我應該使用email.val()嗎? –

    +0

    '$(btnObj).siblings('input:text').val();'將返回文本框的值。 – angusf

    +0

    非常感謝 –

    0
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        if(e.Row.RowType == DataControlRowType.DataRow) 
        { 
         TextBox txtEmail= e.Row.FindControl("yourEmailTextBox") as TextBox; 
         txtEmaill.AddAtrribute("blur",'ValidateEmail(this)'); 
        } 
    } 
    

    這應該是不夠的,你還可以閱讀this post

    +0

    謝謝你的回答。如果我誤導了你,我很抱歉,我在代碼隱藏方面做到了。我需要通過jQuery或JavaScript在客戶端做到這一點。 –

    +0

    您可以在文本框中添加class atrribute,我們將其命名爲「myemail」,$(「。myemail」)。blur(function(){ //您的代碼在這裏 }); –

    相關問題