Solved

validation inside Gridview Controls

Posted on 2010-09-15
5
607 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 20

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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