Wednesday, March 13, 2013

App_Offline.htm in ASP.NET


One of the most useful features of ASP.NET is the “App_Offline.htm” . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site.

How we do it?

We just create a file and name it exactly  as ‘app_offline’ and place it in the root of a web application directory that we have configured within IIS., This will make the site offline, unload the application domain from the server, and stop processing any new incoming requests for that application.It will serve the contents of the app_offline.htm file in response to all new dynamic requests for the application.

How we make the site online?

Once we rename/remove the App_offline.htm file, the next request into the application will cause ASP.NET to load the application and app-domain again, and it will be back to normal.
This feature is first introduced in ASP.NET 2.0 and it is available now in all the later versions.
Just give the App_offline.htm page's content as follows.  This will be displayed for every request to that domain.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title></head><body>    <h1>        WEBSITE IS UNDER MAINTENANCE</h1></body></html>


Kindly share your feedback.

Postback Issue and retrieving values - asp.net dynamic controls


In my last post “CREATINGCONTROLS DYNAMICALLY IN ASP.NET” I have explained about creating controls dynamically in asp.net.
Postback Issue Solving :
By creating controls dynamically for each and every post back the controls will get cleared.  This is due to the lack of initialization of these controls.  If we initialize an asp.net server control in aspx page then there is no need of initializing this control every time, objects will be initiated while parsing the aspx page itself.  By in case of dynamically created controls there will not be any initialization in postbacks so the solution for the controls to stay at postback is – Creating the controls on page load and repeating the creation process in each and every postback.  Now after a change my cs file’s code will look like as follows
    protected void Page_Load(object sender, EventArgs e)
    {
        if (hdfTemp.Value == "1") { CreateForm(); }
    }
    protected void btnGenerateForm_Click(object sender, EventArgs e)
    {
        if (hdfTemp.Value == "0") { CreateForm(); hdfTemp.Value = "1"; }
    }
    public void CreateForm()
    {
        try
        {
            //Create Table
            Table tbl = new Table();
            tbl.ID = "tbl";
            //Creating Table row
            TableRow tr = new TableRow();
            tr.ID = "tr1";
            tbl.Rows.Add(tr);//adding table row to the table
            //Creating table cell
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            //Adding table cells to table rows
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            //Creating Label
            Label lbl = new Label();
            lbl.Text = "First Name";
            lbl.ID = "lblFirstName";
            //Creating TextBox
            TextBox txt = new TextBox();
            txt.ID = "txtFirstName";
     //Assigning the label and textbox controls as child controls for table cells
            tc1.Controls.Add(lbl);
            tc2.Controls.Add(txt);
            phDCtrls.Controls.Add(tbl);
            tblForm.Visible = true;
        }
        catch (Exception ex) { Response.Write(ex.Message); }

    }
Here in the above code I have use a hidden field for avoiding the generation of dynamic controls on first time page loads, because in this scenario only after clicking on the Generate Controls button the controls should get generated.  If you want controls to be generated on page load then no need of using hidden field.

Retrieving values from dynamically generated controls

Controls can be found on the page with their ids.  While creating controls we have specified a unique id for each control.  With this ids we can find the control on the page using “Findcontrol()” method.  Now let’s get the form values in our example on clicking the save button.

  • We have created an id for textbox as “txtFirstName”.
  • Create a textbox object with name txtFirstName.
  • Equal this textbox object with the result of findcontrol method.  Now you got your textbox on this object.
  • Now the textbox’s text property will gives the value of the textbox.

The sample code on save button click event handler will be as follows.
try
{
    TextBox txt = (TextBox)this.Page.FindControl("txtFirstName");
    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "alert", "alert('" + txt.Text.Trim() + "');", true);
}
catch (Exception ex) { Response.Write(ex.Message); }


If any issues or queries, kindly let me know


Happy coding…

Dynamic Controls in ASP.NET


The situation may occur that controls to be created dynamically at runtime, these controls should react for their events i.e., dynamically generated controls should be event driven.  For handling these kinds of situations this post will be very useful. 
First of all we will see how to create and show controls on the form dynamically with an example.  In this example I’m creating a form with one label and one textbox aligned in a table control.
In asp.net, every control can be added as a child control for other container controls like placeholder control, panel control etc.  In this way here also we will create controls and add those controls to one of the parent controls.
  • Create a place holder control which will hold our dynamically created controls.
  • Create a button in which on this button click we are creating and showing controls on the page.  The code which I have written in aspx page will be as follows.


<asp:Button ID="btnGenerateForm" runat="server" Text="Generate Form" OnClick="btnGenerateForm_Click" />
        <table id="tblForm" runat="server" visible="false">
            <tr>
                <td>
                    <asp:PlaceHolder ID="phDCtrls" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <table>
                        <tr>
                            <td>
                                <asp:Button runat="server" ID="btnSave" Text="Save" />
                            </td>
                            <td>
                                <asp:Button runat="server" ID="btnClear" Text="Clear" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
  •  On click event of the Generate form button write the following code.

        try
        {
            //Create Table
            Table tbl = new Table();
            tbl.ID = "tbl";
            //Creating Table row
            TableRow tr = new TableRow();
            tr.ID = "tr1";
            tbl.Rows.Add(tr);//adding table row to the table
            //Creating table cell
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            //Adding table cells to table rows
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            //Creating Label
            Label lbl = new Label();
            lbl.Text = "First Name";
            lbl.ID = "lblFirstName";
            //Creating TextBox
            TextBox txt = new TextBox();
            txt.ID = "txtFirstName";

      //Assigning the label and textbox controls as child controls for table cells
            tc1.Controls.Add(lbl);
            tc2.Controls.Add(txt);

            phDCtrls.Controls.Add(tbl);
            tblForm.Visible = true;
        }
        catch (Exception ex) { Response.Write(ex.Message); }
  • In the above code I first created a table control for that table control I created a table row and added that row to the table control.  Then created two table cells and added these table cells to the table row.  Now created one label control and one textbox control and added these controls to the table cell.  Finally I added the table control to the place holder control which we have created in the aspx page.  Here as I explained previously the table control is the child for the placeholder control, table row is child for table, table cells are child for table row and label and textbox controls are child for table cells.


In this way we can create controls dynamically.  But the problem in creating these controls dynamically is lacking of view state.  So that for every post back the controls will get disappear.  Try this by clicking on the save button which we have created on aspx page.

The solution for this problem and getting values from these dynamically generated controls will be explained in my next post "Postback Issue and retrieving values - asp.net dynamic controls".

Happy coding…