Monday, March 26, 2012

Validating textbox that is created using a boundcolumn?

Hi there - I have a few boudcolums in a grid that are creating textboxes (in the codebehind) on the update routine of my EditCommandColumn. I was wondering if you could create a regularexpression validator in the codebehind to handle the textboxes. An example of one of the textboxes is an email address. I know that the regular expression val has built in validationexpression for email addresses, phone #s, etc. But how can I create this an assign it (controlttovalidate=?) in the codebehind. Here is my update routine

aspx side:
//OnUpdateCommand="UpdateCommand" (grid proterty wiring my routine)

<asp:boundcolumn ItemStyle-CssClass="text" HeaderText="Email" DataField="Email"></asp:boundcolumn
<asp:EditCommandColumn ItemStyle-CssClass="text" runat="server" EditText="Edit" UpdateText="OK" CancelText="Cancel"></asp:EditCommandColumn
codebehind:

protected void UpdateCommand(object sender, DataGridCommandEventArgs e)
{
//make textbox
TextBox email = (TextBox)e.Item.Cells[2].Controls[0];

//Can I make a regularexpresson val for email?

//Do an update here

}You have to use templates for the grid. Just add the validators inside the template you control to validate is and set the controltovalidate property to the control id you want to validate i.
I have my validator at the footer template to add item. how can I stop it from firing when I click on the "Edit" button at the edit template?

thank you!

No comments:

Post a Comment