Solved

validation inside Gridview Controls

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

15 Experts available now in Live!

Get 1:1 Help Now