The simple Example for Dynamic Page Creation
------------------------------------------------------------

In aspx Page add the below tag
<div align="center"><asp:PlaceHolder ID="phUploadEntry" runat="server" EnableTheming="true"></asp:PlaceHolder>
</div>
Before Creating Dynamic Control we need to override the Init

override protected void OnInit(EventArgs e)          
    {
        //InitializeComponent();
        base.OnInit(e);
    }


In the Page Load we will initializes the Control based upon our need ,

Ex :

protected void Page_Load(object sender, EventArgs e)
{
        InitializeComponent();
}

/// <summary>
/// Initialize the Page on Pre-Init for Dynamic Page Load
/// </summary>
private void InitializeComponent()
{
    BindUserUpload ();
    this.Load += new System.EventHandler(this.Page_Load);
}



/// <summary>
/// Bind the dynamic User Upload Screen
/// </summary>
private void BindUserUpload()
{
        this.phUploadEntry.Controls.Add(new LiteralControl("<br><br><table cellspacing='0' cellpadding='0' width='60%' border='0'>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<tr SkinID='002lblNormal' valign='top'><td align='right' style='height: 22px;width:25%'>Select CSV File :</td><td align='left' style='width:450px;height:24px'>"));
        FileUpload objFileUpload = new FileUpload();
        objFileUpload.ID = "uplFile";
        objFileUpload.Width = 350;
        objFileUpload.SkinID = "Browse";
        this.phUploadEntry.Controls.Add(objFileUpload);
        this.phUploadEntry.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        Button objUpload = new Button();
        objUpload.ID = "btnUpload";

        objUpload.Text = "Upload";
        objUpload.SkinID = "btnSubmit";
        objUpload.CssClass = "ThemeButtonsGo";
        objUpload.ValidationGroup = "FileUpload";
        objUpload.Click += new EventHandler(btnUpload_Click);

        this.phUploadEntry.Controls.Add(objUpload);

        this.phUploadEntry.Controls.Add(new LiteralControl("<br>"));

        RegularExpressionValidator objRegx = new RegularExpressionValidator();
        objRegx.ID = "revFileUpload";
        objRegx.ControlToValidate = "uplFile";
        objRegx.ValidationExpression = "^.*" + "\\" + ".(CSV|csv)$";
        objRegx.ErrorMessage = "Please select valid .csv file";
        objRegx.ValidationGroup = "FileUpload";
        objRegx.Display = ValidatorDisplay.Dynamic;
        objRegx.Font.Size = 10;

        this.phUploadEntry.Controls.Add(objRegx);

        RequiredFieldValidator objRfv = new RequiredFieldValidator();
        objRfv.ID = "rfvFileUpload";
        objRfv.ControlToValidate = "uplFile";
        objRfv.Font.Size = 10;
        objRfv.Display = ValidatorDisplay.Dynamic;
        objRfv.ErrorMessage = "File is required for Upload";
        objRfv.ValidationGroup = "FileUpload";

        this.phUploadEntry.Controls.Add(objRfv);
        this.phUploadEntry.Controls.Add(new LiteralControl("<br><br></td></tr>"));//<tr><td colspan='2'>



        //&nbsp;</td></tr>
        this.phUploadEntry.Controls.Add(new LiteralControl("<tr><td  align='center' colspan='2'>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<div style='width: 400px; height: 120px; border: solid 1px #A9D0F5; background-color: #EFEFFB;text-align: left'>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("&nbsp;&nbsp;<b style='color: #A52A2A'>The CSV file should be in the following format:</b>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<ul id='listNotes' type='Square' style='list-style-image: url(Images/Bullets.gif);'>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li><a onclick=\"javascript:window.open(\'../UploadFiles/UploadNonLicmLeave/SampleLeaveDetails.csv\');\" style='cursor:hand'>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<img src='../Images/LogFile.jpg' style='height: 25px; width: 25px' alt='Log' />Click here to view Sample CSV File</a> </li>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li>DomainID(user1) and AvailedLeave</li>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li>Column Separator is '<b>^</b>'</li>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li>DomainID should contain only alphabets, numerals, Hypen and underscore. </li>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li>AvailedLeave should be numeric.</li>"));
        this.phUploadEntry.Controls.Add(new LiteralControl("<li>Entry should not have duplicate or previous upload Entries.</li></ul></div></td></tr></table>"));

    }


In the above example, Every time of page loading the page load the new controls but it will keep the previous values that is loaded in the viewstate.

Comments (0)