1

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

Mansoor Gee
  • 1,071
  • 8
  • 20
nick gowdy
  • 6,191
  • 25
  • 88
  • 157

1 Answers1

1

Dynamically created controls are lost on every postback. I would recommend adding the usercontol to your markup to prevent following scenarios:

  1. People often run into problems with there usercontrols not showing.

  2. Usercontrols events not getting fired, because the usercontrols do not exist in the markup instead are dynamically generated.

  3. There is no difference in the speed(page-size). You can toggle there visibility according to your needs.

  4. Much cleaner, elegant solution.

Anyways if you really need adding table dynamically, have a look at this question Dynamic Controls and Postback and this tutorial https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx

Community
  • 1
  • 1
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
  • But on postback the methods used by the page are called again therefore the grid will grab the data from the dataset and the loop will run again and the user Controls will be added to the correct cells. The user controls are there when the page loads its just the validation which doesn't work. – nick gowdy Jun 29 '12 at 11:15
  • Have you set the correct id for the control you want to validate? – Ashwin Singh Jun 29 '12 at 11:18
  • Yes if I hard-code the user control in the markup the validation works. I tested the validation controls within my user control and they behave as expected. The dynamic control IDs are auto-generated. – nick gowdy Jun 29 '12 at 11:23
  • Try to assign ID yourself sometimes the id get prepended with the control's id, maybe that's what happening here. – Ashwin Singh Jun 29 '12 at 11:25
  • I discovered that its an ie problem but I still don't know why this isn't working. Works fine in Firefox. – nick gowdy Jun 29 '12 at 13:40