Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form><tablestyle="width:66%;"><tr><td><asp:LabelID="lblmsg"Text="President Election Form : Choose your president"runat="server"/></td></tr><tr><td> Candidate:</td><td><asp:DropDownListID="ddlcandidate"runat="server"style="width:239px"><asp:ListItem>Please Choose a Candidate</asp:ListItem><asp:ListItem>M H Kabir</asp:ListItem><asp:ListItem>Steve Taylor</asp:ListItem><asp:ListItem>John Abraham</asp:ListItem><asp:ListItem>Venus Williams</asp:ListItem></asp:DropDownList></td><td><asp:RequiredFieldValidatorID="rfvcandidate"runat="server"ControlToValidate="ddlcandidate"ErrorMessage="Please choose a candidate"InitialValue="Please choose a candidate"></asp:RequiredFieldValidator></td></tr><tr><td> House:</td><td><asp:RadioButtonListID="rblhouse"runat="server"RepeatLayout="Flow"><asp:ListItem>Red</asp:ListItem><asp:ListItem>Blue</asp:ListItem><asp:ListItem>Yellow</asp:ListItem><asp:ListItem>Green</asp:ListItem></asp:RadioButtonList></td><td><asp:RequiredFieldValidatorID="rfvhouse"runat="server"ControlToValidate="rblhouse"ErrorMessage="Enter your house name"></asp:RequiredFieldValidator><br/></td></tr><tr><td> Class:</td><td>
- <asp:TextBoxID="txtclass"runat="server"></asp:TextBox></td><td><asp:RangeValidatorID="rvclass"runat="server"ControlToValidate="txtclass"ErrorMessage="Enter your class (6 - 12)"MaximumValue="12"MinimumValue="6"Type="Integer"></asp:RangeValidator></td></tr><tr><td> Email:</td><td><asp:TextBoxID="txtemail"runat="server"style="width:250px"></asp:TextBox></td><td><asp:RegularExpressionValidatorID="remail"runat="server"ControlToValidate="txtemail"ErrorMessage="Enter your email"ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td></tr><tr><td><asp:ButtonID="btnsubmit"runat="server"onclick="btnsubmit_Click"style="text-align: center"Text="Submit"style="width:140px"/></td></tr></table><asp:ValidationSummaryID="ValidationSummary1"runat="server"DisplayMode="BulletList"ShowSummary="true"HeaderText="Errors:"/></form>The code behind the submit button:protectedvoid btnsubmit_Click(object sender,EventArgs e){if(Page.IsValid){ lblmsg.Text="Thank You";}else{ lblmsg.Text="Fill up all the fields"
- ====================================
- What is IsPostBack?
- Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a post back.
- This is false when the first time the page is loaded and is true when the page is submitted and processed.
- What is ViewState?
- A web application is stateless. That means that a new instance of a page is created every time when we make a request to the server to get the page and after the round trip our page has been lost immediately. It only happens because of one server, all the controls of the Web Page is created and after the round trip the server destroys all the instances. So to retain the values of the controls we use state management techniques.
- State Management Techniques
- They are classified into the following 2 categories:
- View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.
- Eg:
- // this is a code for submit button
- protected void Button1_Click(object sender, EventArgs e)
- {
- //Value of Textbox1 and TectBox2 is assigin on the ViewState
- ViewState["name"] = TextBox1.Text;
- ViewState["password"] = TextBox2.Text;
- //after clicking on Button TextBox value Will be Cleared
- TextBox1.Text = TextBox2.Text = string.Empty;
- }
- // this is a code for restore button, to restore the previous content after clicking the submit button
- protected void Button3_Click(object sender, EventArgs e)
- {
- //If ViewState Value is not Null then Value of View State is Assign to TextBox
- if (ViewState["name"] != null)
- {
- TextBox1.Text = ViewState["name"].ToString();
- }
- if (ViewState["password"] != null)
- {
- TextBox2.Text = ViewState["password"].ToString();
- }
- }
- ** go through all properties of login control of Asp .Net
- Sample code for creating webpage with login control. In this code it will authenticate with your username and password stored in the database.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.OleDb;
- namespace WebApplication3
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- ViewState["LoginErrors"] = 0;
- }
- /*this is the function which will call at the time of login ,
- * here you can write the code for checking the login username and password */
- protected void Login2_Authenticate(object sender, AuthenticateEventArgs e)
- {
- if (ValidationFunction(Login2.UserName, Login2.Password))
- {
- Login2.Visible = false;
- Label1.Text = "login successful";
- }
- else { e.Authenticated = false; }
- }
- // this is a user defined function which i am using to check for the username and password in database
- private bool ValidationFunction(string Username, string Password)
- {
- bool returnvalue = false;
- string strConnection = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\lenovo\\Documents\\Database21.mdb";
- OleDbConnection con = new OleDbConnection(strConnection);
- string query = "select Uname, Password from Table1";
- OleDbCommand command = new OleDbCommand(query, con);
- OleDbDataReader dr;
- con.Open();
- dr = command.ExecuteReader();
- while (dr.Read())
- {
- if ((Username == dr["Uname"].ToString()) & (Password == dr["Password"].ToString()))
- {
- returnvalue = true;
- }
- dr.Close();
- return returnvalue;
- }
- return returnvalue;
- }
- // this is teh function to check for the password error ,
- //in this code it will allow only 3 time attempt to enter the wrong password
- protected void Login2_LoginError(object sender, EventArgs e)
- {
- if (ViewState["LoginErrors"] == null)
- ViewState["LoginErrors"] = 0;
- int ErrorCount = (int)ViewState["LoginErrors"] + 1;
- ViewState["LoginErrors"] = ErrorCount;
- if ((ErrorCount > 3) && (Login2.PasswordRecoveryUrl != string.Empty))
- Response.Redirect(Login2.PasswordRecoveryUrl);
- }
- }
- }
- Create a website for the multifest organizing in your campus. For the first time the user should register with their information (name, rollnumber, course, semester, mailid, phonenumber, password, confirm password).
- After registration they can login with their email id and password.
- Provide update privilege after login
- Validate phone number field using regular expression validator
- Validate the semester field with range validator (allows only 1 to 8 semeter)
Add Comment
Please, Sign In to add comment