Click here to Skip to main content
12,299,131 members (50,512 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 4-May-11 10:00am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<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)
{
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

<asp:gridview id="GridView1" runat="server" autogenerateeditbutton="True" autogenerateselectbutton="True" xmlns:asp="#unknown">

<asp:templatefield headertext="text box">

<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">

<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" />
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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">
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 5 May 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100