Hello Friends....
Welcome to this Blog...This Blog is specially for the .Net Developers where all developers can discuss the .Net and Sql Articles.
"And will you succeed? Yes! You will indeed!(98 and 3/4 percent guaranteed)."

Thursday, November 18, 2010

The Page Life Cycle of an ASP.NET and its controls.

The life cycle starts when a user requests a web page through his/her browser. The Web server than process the page through a sequence of steps before response is sent back to the user's browser. The steps are as:
  1. Page Request
  2. Start
  3. Page Initialization
  4. Load
  5. Validation
  6. PostBack Event Handling
  7. Render
  8. Unload
Page Request
The page request occurs before the page life cycle begins. When a user requests the page, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.
Start
In the start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. Additionally, during the start step, the page's UICulture property is set.

Page Initialization

During page initialization, controls on the page are available and each control's UniqueID are generated but not their properties. Any themes are also applied to the page.

Developers have access to the Init, InitComplete and PreLoad methods in this stage. The methods are as follows:
  • Init: This event is raised after all controls have been initialized and any skin settings have been applied. This event is used to read or initialize control properties.
  • InitComplete: The Page object raises this event. This event is used for processing tasks that require completion of all initialization.
  • PreLoad: Use this event if you need to perform processing on your page or control before the Load event. After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
Load

During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state. The OnLoad event method is fired during this stage.
This is where you will want to set properties for all of the server controls on your page, request query strings, and establish database connections.

Validation

During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.

PostBack Event Handling

If the request is a postback, any event handlers are called. The event handling for server controls occurs during this stage.

Render

During rendering, view state is saved to the page and then the page calls on each control to contribute its rendered output to the OutputStream of the page's Response property. Render is not really an event. The HTML of the page and all controls are sent to the browser for rendering.

Unload

Unload is called when the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed. The cleanup includes routines such as closing database connections and file streams, or, event logging and other tasks.
 
Conclusion
 
When a Web page is requested, the server creates objects associated with the page and all of its child controls objects and uses these to render the page to the browser. Once the final stage is complete, the web server destroys these objects, to free up resource to handle additional request.

I hope that this article would have helped you in understanding the Page Life Cycle in ASP.NET and its controls. Your feedback and constructive contributions are welcome.

Thanx:
Dinesh Wadhwa. 

Wednesday, November 10, 2010

Registration and Email Verification for New Accounts

In This Code i am Providing the code by which the user can easily registered to the Website and the E-Mail is sent to the user for the Verification. If the user will not verify his/her email,the account is not activated and that person cannot login.


Here are the Following Steps to achieve that:-

1.Create new Web application and make one form in which the user can register and add the following code to the source code:

      //Send email to user for verifying account
        CreateUserWizard cuw = (CreateUserWizard)sender;
        MembershipUser user = Membership.GetUser(cuw.UserName);
        Guid userId = (Guid)user.ProviderUserKey;
        System.Net.Mail.MailMessage EmailMsg = new          System.Net.Mail.MailMessage("info@ask.com",            
CreateUserWizard1.Email);
        EmailMsg.Subject = "Email Verification fromhttp:dinesh-itsolutions.blogspot.com/";
        EmailMsg.IsBodyHtml = true;
        EmailMsg.Body = "Thanks for registering with www.http://dinesh-itsolutions.blogspot.com/!<br /><br />Your activation link : <a href=http://http://dinesh-itsolutions.blogspot.com/?ID=" + userId.ToString() + ">Link</a>.";
        System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
        //This object stores the authentication values
        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("smtpserverinfo", "smtpserverpassword");

        mailClient.Host = "mail.ask.com";
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = basicAuthenticationInfo;
        mailClient.Send(EmailMsg);


2.By the code the email is sent to the user with the unique link.

3.When the user check his/her mail and click on the activate link then it will redirected to the new webpage

now retrieve that unique id from the URL.



4.After Retrieving the Id'...just set the stutus of the Client from 0 to 1 in the database so that the person can login.        


Thank You:

Enjoy ....................n in case if any query then ask me......

Sunday, September 12, 2010

Adding Template Fields to a GridView Control.

A TemplateField is used in cases where authors need complete control over the
contents of a GridView column. The TemplateField is declared with an
<ItemTemplate> element, which in turn is used to define the content that will be
rendered for each row in the column. The ItemTemplate can include any valid HTML.

To add a template column:-

1.Set the GridView control's AutoGenerateColumns property to false.

<asp:GridView id="myGrid" runat="server"
   autogeneratecolumns=false
   ...
>
2. Within the GridView declaration, declare a <Columns> element.
3.Within the Columns element, define the <asp:TemplateField> 
control, along with the required <ItemTemplate> element.
4.Within the ItemTemplate, define the HTML or server control that 
will contain the field value.
To display the value of a field in an item template,
use DataBinding 
Expression Syntax
5.Optionally set the <asp:TemplateField> control's 
other properties.
For syntax, see GridView Control Syntax
 
<columns>
 <asp:templatefield>

   <itemtemplate>
     <img width=60 align="top" 
src='<%# Eval ("title_id", "../images/title-{0}.gif" )%>'>
   </itemtemplate>

  </asp:templatefield>
   ...
</columns>

Friday, September 3, 2010

Dynamic Gridview Bind

First You have to Drag Gridview Tool From your Toolbox
and place it to your design .aspx Page:-

Then at  the Code behind page in .cs file like Gridview.aspx.cs file
call  the following function to your button_click event:-

void BindData()
{
try
{
SqlConnection con = new SqlConnection(@"server=.\sqlexpress;
database=TestDB;Integrated Security=SSPI;");
 
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select id,name,flag 
from EmpData", con); 
 
da.Fill(ds);
BoundField BC1 = new BoundField();
BC1.HeaderText = "ID";
BC1.DataField = "id";
GridView1.Columns.Add(BC1);
BoundField BC2 = new BoundField();
BC2.HeaderText = "Name";
BC2.DataField = "name";
GridView1.Columns.Add(BC2); 
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
catch (Exception e)

{
Response.Write(e.Message.ToString());
con.Close();
}

Enjoy  :-)

Create File Uploader (Visual Studio)

By this Coding you can create File Uploader 
and the is file will uploaded into Foder:- 
 
 Design Source Code:- 
 
<form id="form1" runat="server">
    <asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" 
     onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload 
     status: " />
</form>
Coding C#:
protected void UploadButton_Click(object sender, EventArgs e)
{
if(FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/Folderpath") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch(Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded.
The following error occured: " + ex.Message;
}
}
}

Creating New Web Page in Asp.net

Creating New Web Page in Asp.net

1.Open your Visual Studio 2.0 or 3.5 version
2.Go to File Menu---->New------>Website
3.On the Templates,Select ASP.NET Website.
4.In this diolog box set ur Language as C#.
5.Click Ok and your Website gets open.
6.Start working with web pages...