How can i make user control? Give example.

Showing Answers 1 - 1 of 1 Answers

promothash

  • Jan 15th, 2007
 

Steps to create a user control.

1.Open ur VS with the new Website.

2. Right Click on the Solution explorer to "Add New Item."

3.Select the "WebUserControl.ascx"

4.Now you can place any control from ur toolbox to make ur desired control.

5.On the controls placed you can write the events that are required.

6.if some properties need to be bind then it should also be defined using "  get and set method"

7.After ur code is ready then you can just place ur control in a web form and run it.

Example---This is a simple Login Control...............................-------

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<div id ="divlogin">

<span >Login &nbsp; &nbsp; &nbsp;</span>

<asp:TextBox ID="txtusername" runat="server"></asp:TextBox><br />

<span >Password</span>

<asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>

<asp:Button ID="Button1"

runat="server" Text="Login" OnClick="Button1_Click" />

</div>

----------------------The CS--code behind file ------------------------

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

{

#region properties

String _username;

public String Username

{

get { return _username; }

set { _username = value; }

}

String _password;

public String Password

{

get { return _password; }

set { _password = value; }

}

#endregion

#region events

protected void Button1_Click(object sender, EventArgs e)

{

txtusername.Text = Username;

txtpassword.Text = Password;

//write ur Authentication code here...

}

#endregion

}

---------------------------------------------------------------------------

-------------Code of the page where u will place the control----------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!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>Login Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

</div>

</form>

</body>

</html>

----------------CS code of the page Default.aspx.cs---------------

using System.Collections;

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 Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void WebUserControl1_Load(object sender, EventArgs e)

{

//Passing values to the control...

WebUserControl1.Username = "Promothash";

WebUserControl1.Password = "1234";

}

}

---------------------------------------------/////////////////////////////////-----------------------------------

I hope this example will be of some help.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions