Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an asp grid view which contains a text box and button.I have set a required field validator for the text box and set the validation group of required field validator and the button the same.But on clicking the button the required field validator shows up for all the text boxes instead of the button i clicked.Is there anyway to show the validator for text box based on the button i clicked?

Thanks in advance
Posted

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<columns>
<asp:TemplateField>
            <HeaderTemplate >SSSS</HeaderTemplate>
            <itemtemplate><asp:TextBox ID="t1" runat ="server" 
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="t1" ErrorMessage="Enter Value"
            <asp:Button ID="btn1" runat="server" Text ="OK" OnClick ="Button1_Click">
            </itemtemplate>
            

</columns>

in code behind
first bind gridview with your requirement.
protected void Button1_Click(object sender,EventArgs e)
{
}
 
Share this answer
 
v2
you need to use ControlToValidate property of RequiredFieldValidator to Gets or sets the input control to validate. you can validate only those control for which you want required field validation.
<asp:RequiredFieldValidator runat="server" id="RFVID" controltovalidate="txtName" errormessage="Please enter value!" />

here you are validating txtName textbox for checking RequiredField Validation.
Take a look there-[RequiredFieldValidator][^] for more details.

In addition you should navigate given links
RequiredFieldValidator for multiple text boxes[^]
 
Share this answer
 
v2
<asp:gridview id="GridView1" runat="server" autogenerateeditbutton="True" autogenerateselectbutton="True" xmlns:asp="#unknown">
<columns> <asp:templatefield headertext="text box">
<itemtemplate>
<asp:textbox id="textbox1" runat="server" validationgroup="G1">
<asp:requiredfieldvalidator controltovalidate="textbox1" runat="server" errormessage="*">
Text="error text" ValidationGroup="G1">


<asp:templatefield headertext="text box">
<itemtemplate>
<asp:textbox id="textbox2" runat="server">



<asp:button id="Button2" runat="server" text="Button 1" validationgroup="G1" xmlns:asp="#unknown">
<asp:button id="Button3" runat="server" text="Button 2" xmlns:asp="#unknown">
 
Share this answer
 
How r u assigning the ValidationGroup property to the contrls, because this value has to be different for the combination of the controls.
like
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
<columns>
<asp:templatefield>
    <itemtemplate>
         <asp:textbox id="TextBox1" runat="server" validationgroup="ValidationGroup1"></asp:textbox>
         <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server">
                  ControlToValidate="TextBox1" ValidationGroup="ValidationGroup1">/asp:RequiredFieldValidator>
         <asp:button id="btn1" runat="server" text="OK" validationgroup="ValidationGroup1">br mode="hold" />    </asp:button></asp:requiredfieldvalidator></itemtemplate>
    <itemtemplate>
         <asp:textbox id="TextBox1" runat="server" validationgroup="ValidationGroup2"></asp:textbox>
         <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server">
                  ControlToValidate="TextBox1" ValidationGroup="ValidationGroup2">/asp:RequiredFieldValidator>
         <asp:button id="btn1" runat="server" text="OK" validationgroup="ValidationGroup2">br mode="hold" />    </asp:button></asp:requiredfieldvalidator></itemtemplate>
    
</asp:templatefield>
</columns>
</asp:gridview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900