Regular Expression Validator

Hi There,
I am trying to validate the textbox populated in the datagrid, when an update button is clicked. I am using a bound column and using a regular expression to validate the text entered by the user. I am trying to do it dynamically. Below is the code I have and for some reason it is not validating..
TextBox txtOPLTarget = (TextBox) e.Item.Cells[10].Controls[0];
                        
                        RegularExpressionValidator rexpValidator = new RegularExpressionValidator();
                        rexpValidator.Text = "Please enter a positive value";
                        rexpValidator.ControlToValidate = txtOPLTarget.ToString();
                        rexpValidator.Display = ValidatorDisplay.Dynamic;
                        rexpValidator.ID = "validate" + txtOPLTarget.ID;
                        rexpValidator.ValidationExpression="(^\\d*\\.?\\d*[1-9]+\\d*$)|(^[1-9]+\\d*\\.\\d*$)";
                        
//Adding the control to the datagrid...
                        dgAccounts.Controls.Add(rexpValidator);

Can anybody please let me know why it is not working?? Is there a better way to implement it??
Thanks.
LVL 2
jackrock79Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:

The update button is each column inside the datagrid or it is outside the datagrid?
0
jackrock79Author Commented:
Update button (populated by cliking the edit button) is inside the datagrid. It is normal inline editing for datagrid...
Thanks.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ihenryCommented:
It would be much easier if you use ItemTemplate column instead of bound column and you don't have to create the validators programmaticaly, like so

<Columns>
      <asp:TemplateColumn HeaderText="Set a header name">
            <ItemTemplate>
                  <asp:TextBox id="txtOPLTarget" runat="server"></asp:TextBox>&nbsp;
                  <asp:RegularExpressionValidator
                        id="myValidator" runat="server"
                        ErrorMessage="Set some error message"
                        Display="Dynamic"
                        ValidationExpression="(^\\d*\\.?\\d*[1-9]+\\d*$)|(^[1-9]+\\d*\\.\\d*$)"
                        ControlToValidate="txtOPLTarget">
                  </asp:RegularExpressionValidator>
            </ItemTemplate>
      </asp:TemplateColumn>
</Columns>
0
ihenryCommented:
about your problem, try to assign to e.Item.Controls collection. And you don't need to assign different id for each validator. Like this

//Adding the control to the datagrid...
//dgAccounts.Controls.Add(rexpValidator);
e.Item.Controls.Add(rexpValidator)

And another thing to check whether the regular expression is correct to validate a positive number.
0
jackrock79Author Commented:
Henty,
I tried doing that. But when the datagrid is displayed, I see textboxes in the columns on page load.  On Page load I would like to see only labels with the values. I want to see textboxes only when the update button is clicked and and then would like to validate the textboxes.
U understand what I am talking abt right!! Please let me know if I am not clear.
Thanks.
0
ihenryCommented:

It's still much possible using TemplateColumn, simply set each textbox visible property to false.

<asp:TextBox id="txtOPLTarget" runat="server" Visible="False"></asp:TextBox>

When the update button is clicked, it triggers the datagrid ItemCommand event then you can set the textboxt visible property back to true and call validator Validate method.

Now back to your original problem. I still have few questions
1. When the update button is clicked, can you see the textbox
2. Can you please elaborate more on "it is not validating", maybe you want to post some more code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jackrock79Author Commented:
Henry,
If I use the Item Template with a textbox in it and if I set the textbox visible property to false, the value in it is not seen on page load which is not the case I want. I would like to see the value in the column on page load. When an edit button is clicked, I would like to see a textbox populated in the datagrid column with the value in it. And if the user wants to update it , he should be allowed to do so.
Please let me know if I am not clear.

Thanks.
0
jackrock79Author Commented:
Henry,
I found the answer..I have to use template column and use a label for item template and a textbox for the edit item template in the same template column. The respective control to load is picked up depending on the mode.
Thanks so much for your time..
0
jackrock79Author Commented:
Then I can validate the textbox using asp.net and don't need to do it programmatically.
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.