Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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.
0
jackrock79
Asked:
jackrock79
  • 5
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now