I have created a user control that is a textbox with some validation which I am adding dynamically to a grid. The grid's data source is from a dataset.
Because of this I am looping through my grid and loading the control to specific cells in the grid where I want them to be.
The problem I have is because I am adding this control dynamically, this somehow stops the validation from working.
User Control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextboxPercentage.ascx.cs"
Inherits="tesco.User_Controls.TextboxPercentage" %>
<%@ register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc2" %>
<div id="percentage">
<asp:TextBox runat="server" ID="txtPercentage" Width="40" onfocus="if (this.value=='0') this.value='';" onblur="if
(this.value=='') this.value='0';" Text="0"></asp:TextBox>
<asp:Label runat="server" ID="lbl1">%</asp:Label>
<asp:RegularExpressionValidator ID="Reg_percentage" ControlToValidate="txtPercentage"
runat="server" ErrorMessage="Numbers with 18 digits and two decimal values only. eg.9999999.99"
Display="None" ValidationExpression="\b\d{1,18}\.?\d{0,2}" Enabled="true"></asp:RegularExpressionValidator>
<asp:RangeValidator ID="rval_percentage" ControlToValidate="txtPercentage" MinimumValue="0"
MaximumValue="100" Type="Double" runat="server" ErrorMessage="Numbers between 0-100 only" Display="None" ></asp:RangeValidator>
<cc2:ValidatorCalloutExtender ID="vce_percentage_value" runat="server" TargetControlID="Reg_percentage"
Enabled="True">
</cc2:ValidatorCalloutExtender>
<cc2:ValidatorCalloutExtender ID="vce_percentage_range" runat="server" TargetControlID="rval_percentage"
Enabled="True">
</cc2:ValidatorCalloutExtender>
Code behind
GridView1.DataSource = ds;
GridView1.DataBind();
AssignCellCoordinates();
private void AssignCellCoordinates()
{
// Create IDs for Grid
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].ID = "r" + i; // Row ID
for (int ii = 0; ii < GridView1.Rows[i].Cells.Count; ii++)
{
GridView1.Rows[i].Cells[ii].ID = "c" + ii; // Cell ID
if (GridView1.Rows[i].ID == "r25" || GridView1.Rows[i].ID == "r26" || GridView1.Rows[i].ID == "r27")
{
if (GridView1.Rows[i].Cells[ii].ID != "c0")
{
User_Controls.TextboxPercentage txtPerc = (User_Controls.TextboxPercentage)LoadControl("~/User_Controls/TextboxPercentage.ascx");
GridView1.Rows[i].Cells[ii].Controls.Add(txtPerc);
}
}
}
}
}
As you can see I loop through a row and for each row iteration I add cell IDs until I reach total cells for one row. Within my inner loop I add the control if its certain cell id. But my validation doesn't work.
Anyone have ideas why this is?
Thanks