我的GridView
有ContentTemplate
包含ItemTemplate
。 ItemTemplate
有textbox
和button
。如何獲取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('ctl00$MainContent$GridView1','Sort$CustID')">Customer ID</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustFirstName')">First Name</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustLastName')">Last Name</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$CustCity')">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("ctl00$MainContent$GridView1$ctl02$btnUpdate", "", true, "grpEmail", "", 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("ctl00$MainContent$GridView1$ctl03$btnUpdate", "", true, "grpEmail", "", 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("ctl00$MainContent$GridView1$ctl04$btnUpdate", "", true, "grpEmail", "", false, false))" id="MainContent_GridView1_btnUpdate_2" ButtonType="Button" />
</td>
</tr>
</table>
</div>
我如何獲得電子郵件文本框的ID?
謝謝
這是有效的。大。謝啦。我現在如何獲得電子郵件文本框的價值?我應該使用email.val()嗎? –
'$(btnObj).siblings('input:text').val();'將返回文本框的值。 – angusf
非常感謝 –