Solved

validation inside Gridview Controls

Posted on 2010-09-15
5
611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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