如何根据jquery中gridview中的另一个文本框值验证文本框值

时间:2014-03-10 11:22:00

标签: asp.net webforms

textbox内有两个gridview。如果第一个文本包含任何数据,则第二个textbox不应为空。如果1st textboxnull,则不对第二个文本框进行验证。填充第一个textbox第二个文本框后,只有警报才会出现。怎么做?

有什么想法吗?请帮帮我。

<asp:GridView runat="server" Width="980px" ID="grdResUpdate" AutoGenerateColumns="false"
CssClass="TopMargin10 borderClass gridwrap" OnRowDataBound="grdResUpdate_RowDataBound" ShowHeader="true">
    <Columns>
        <asp:TemplateField HeaderText="SO #" ItemStyle-Width="70px">
            <ItemTemplate>
                <asp:Label runat="server" Width="70px" ID="lblSOName" CssClass="gridwrap" Text='<%# Eval("SOName")%>' />
                <asp:HiddenField runat="server" ID="hdnFldSOId" Value='<%#Eval("SOId") %>' />
             </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Test Type" ItemStyle-Width="300px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:HiddenField runat="server" ID="testTypeIdHdnFld" Value='<%#Eval("TestTypeId") %>' />
                <asp:Label runat="server" Width="150px" ID="lblTestTypeName" CssClass="gridwrap" Text='<%# Eval("TestTypeName")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Test Case Count" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtTestCaseCount" runat="server" HeaderText="Test Case Count" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Environment" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtEnvironment" runat="server" HeaderText="Environment" Width="80px" onkeydown="return isDigit(event)"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Passed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtPassed" runat="server" HeaderText="Passed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Failed" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtFailed" runat="server" HeaderText="Failed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="No Of Defects" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtDefects" runat="server" HeaderText="No Of Defects" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Hours" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtHours" runat="server" HeaderText="Hours" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Test Case Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtTCComp" runat="server" HeaderText="Test Case Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Percentage Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox ID="txtPercComp" runat="server" HeaderText="Percentage Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle CssClass="headerTop" />
    <AlternatingRowStyle CssClass="rowbgblue" />
    <RowStyle CssClass="rowbginwhite" />
</asp:GridView>

我希望当用户填写txtTestCaseCount时,他必须填写txtHours

3 个答案:

答案 0 :(得分:0)

首先访问gridview文本框,然后将检查代码放在txtTestCaseCoun t textbox

的更改事件中
var textBoxOne = '#<%=grdResUpdate.ClientID%> input[id*="txtTestCaseCount"]';
var textBoxTwo = '#<%=grdResUpdate.ClientID%> input[id*="txtHours"]';

$(textBoxOne).on('change', function () {
  if($(textBoxTwo).val()==""){
     alert("Please fill hours textbox");
  }

});

检查是否填写了txtTestCaseCount值,如果未填写文本框txtHours,则会显示警告消息。

答案 1 :(得分:0)

can check with onfoucsOut event

$("textbox2id").focusOut(function(){
if(!($("textbox2id").val() == ""))
{
alert("please enter a value as required");
$(this).focus();
}
})

答案 2 :(得分:-1)

gridview中的每个文本框都有一个在运行时生成的id。
使用该id作为选择器,如

$("#textboxid").val()


这将给你文本框的内容。用它来验证另一个文本框。


如果假设txtTestCaseCount生成的id类似于grdResUpdate_txtTestCaseCount则为

      if($("#grdResUpdate_txtTestCaseCount").val().length < 0){//Code}