Step 1: Create database. here i am using access database to fulfill this tutorial. add that database in your project.
Step 2: now create website in asp.net and add ascx file in your project and give name Logincontrol.ascx
Step 3: Now add this code snippets in Logincontrol.ascx file
<%@ Control Language="C#" AutoEventWireup="true" Debug="true" CodeFile="LoginControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="TextBox2" runat="server" style="z-index: 100; left: 419px; position: absolute; top: 238px"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" style="z-index: 101; left: 418px; position: absolute; top: 198px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Style="z-index: 102; left: 346px; position: absolute;
top: 200px" Text="UserID"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 103; left: 348px; position: absolute;
top: 240px" Text="Password"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 105;
left: 421px; position: absolute; top: 278px" Text="Login" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
code behind file:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.OleDb;
using System.Data.Common;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//PlaceHolder1.Controls.Clear();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source=F:\\SEM_6\\usecontrol\\App_Data\\SignUp.mdb;";
OleDbConnection con = new OleDbConnection(s);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from SignUp";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
//GridView1.DataSource = dt;
//GridView1.DataBind();
int rc = dt.Rows.Count;
if (rc > 0)
{
int flag = 0;
for (int i = 0; i < rc; i++)
{
if (dt.Rows[i].ItemArray[0].ToString() == TextBox1.Text && dt.Rows[i].ItemArray[4].ToString() == TextBox2.Text)
{
flag = 1;
}
else
{
}
}
if (flag > 0)
{
Response.Write("login sucess");
}
else
{
Response.Write("login failed");
}
}
}
}
Step 4: Now add reference and register in default.aspx page
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="LoginControl.ascx" TagName="LoginControl" TagPrefix="uc1" %>
<%@ Reference Control="~/LoginControl.ascx" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<uc1:LoginControl ID="LoginControl1" runat="server" />
</form>
</body>
</html>
Step 5: now you run your default.aspx page
I hope you are clear with this tutorial for user control in asp.net
0 comments:
Post a Comment