Solved

validation inside Gridview Controls

Posted on 2010-09-15
5
599 Views
Last Modified: 2012-05-10
I have a gridview with Checkbox, dropdownlist, textbox controls
I want to use the Required field validator based on these conditions
if checkbox.checked=true

then i want to fire the required field validator for the dropdown list to see if the dropdown is selected or not.

if the dropdown.selected.item.text='Between' then i want to fire the required field validator for the textbox . Please tell me how to do this. i spent hours to figure this out but i couldn't.

Thanks
0
Comment
Question by:gladstonesheeba
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 33685348
I'm not 100% sure what you're doing here but right off, my guess is that  you're not grabbing the control correctly.  Inside a GridView, you need  to do a .FindControl() and cast accordingly.
 
 Therefore, let's say the ID of your ASP .NET HtmlInputCheckBox is "myCheckBox"
 
 Then, you'd do this within the GridView (foreach item)
 
 HtmlInputCheckBox checkBox = (HtmlInputCheckBox)item.FindControl("myCheckBox");
 
 if (checBox.Checked)
 {
 etc;
 }
 
 same goes for ANY control within the GridView.  
0
 

Author Comment

by:gladstonesheeba
ID: 33686212
Hi ,
 I tried doing this way

 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox includeintest = (CheckBox)GridView1.Rows[i].Cells[3].FindControl("chkincludeTest");
            DropDownList validation = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("drpvalidation");
            TextBox txtspecA = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtvalueA");
            TextBox txtspecB = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtValueB");
            DropDownList drpuom = (DropDownList)GridView1.Rows[i].Cells[7].FindControl("drpuom");
            RequiredFieldValidator reqvalidation = (RequiredFieldValidator)GridView1.Rows[i].Cells[4].FindControl("Reqvalidation");
            RequiredFieldValidator reqspeca = (RequiredFieldValidator)GridView1.Rows[i].Cells[5].FindControl("Reqspeca");
            RequiredFieldValidator reqspecb = (RequiredFieldValidator)GridView1.Rows[i].Cells[6].FindControl("Reqspecb");
            RequiredFieldValidator requom = (RequiredFieldValidator)GridView1.Rows[i].Cells[7].FindControl("Requom");
            CompareValidator compare = (CompareValidator)GridView1.Rows[i].Cells[6].FindControl("CompareValidator1");
            if (includeintest.Checked == true)
            {
                reqvalidation.Enabled = true;
                reqspeca.Enabled = true;
                requom.Enabled = true;

             
           

            }
            else
            {
                reqvalidation.Enabled = false;
                reqspeca.Enabled = false;
                requom.Enabled = false;
                reqspecb.Enabled = false;
                compare.Enabled = false;
             

            }
        }

If i click Submit , then for the first time its not validating anything, just doing a page refresh, if i again press submit, then its validating. how do i fix this issue. Please help  me
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33686229
When is it you are trying to do ...I guess when Editing a row correct?
You can do this in code-behind with following steps:
1: Initially set your DDL and TextBox validator enable or disable according to the state of your Checkbox. And by this I mean initially when your Gv is loaded.
2: Set AutoPostBack=true for your DDL and Chb
3: In the Checkbox OnCheckChanged event, you enable or disable the DDL validator and TB Validator like shown below:

 protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
       
        RequiredFieldValidator ddlrfv = (RequiredFieldValidator )cb.NamingContainer.FindControl("ddlRFV");
        RequiredFieldValidator tbrfv = (RequiredFieldValidator )cb.NamingContainer.FindControl("tbRFV");

     if(cb.Checked)
    {      
        if(ddlrfv!=null){
             ddlrfv.Enabled=true;
            }
    }
    else
    {
        if(ddlrfv!=null){
             ddlrfv.Enabled=false;
            }
          if(tblrfv!=null){
             ddlrfv.Enabled=true;
            }
    }
}
4: Add OnSelectedIndexChanged event to DDL like checkbox. Get it's Selected value and see if it is "Between" then Find TbRFV and enable it else disable it.

Note: You might be able to do this in javascript which might be trick like above.
0
 

Author Comment

by:gladstonesheeba
ID: 33686378
Please find the attached code. I have mentioned HTML and Button_click code behind as well.

What am trying to do is when i first hit Submit, it should validate all the controls if everything is ok then i want Edit button and Save button to be visible and Btnsubmit to be invisible.  , its not working since its refreshing the page two times and then only it validates. I would appreciate your help.
Code.doc
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now