Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

validation inside Gridview Controls

Posted on 2010-09-15
5
Medium Priority
?
613 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

886 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