Ctrls
Ctrls
To create label either we can write code or use the drag and drop facility of visual
studio 2017.
This is server side control, asp provides own tag to create label. The example is given
below.
This control has its own properties that are tabled below.
Property Description
Example
// WebControls.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehin
d="WebControls.aspx.cs"
Inherits="WebFormsControlls.WebControls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
margin-left: 0px;
}
.auto-style3 {
width: 121px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Provide the Following Details:</h4>
<table class="auto-style1">
<tr>
<td class="auto-style3">
<asp:Label ID="Label1" runat="server" Text="User Name"></
asp:Label></td>
<td>
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-
style2"></asp:TextBox></td>
</tr>
<tr>
<td class="auto-style3">
<asp:Label ID="Label2" runat="server" Text="Upload a File"></
asp:Label></td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
This is server side control, asp provides own tag to create it. The example is given
below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
Example
// WebControls.aspx
Code Behind
// WebControls.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected void SubmitButton_Click(object sender, EventArgs e)
{
userInput.Text = UserName.Text;
}
}
}
This is a server side control and asp provides own tag to create it. The example is
given below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
AccessKey It is used to set keyboard shortcut for the control.
Example
// WebControls.aspx
Code Behind
// WebControls.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "You Clicked the Button.";
}
}
}
Output:
To create HyperLink either we can write code or use the drag and drop facility of
visual studio IDE. This control is listed in the toolbox.
This is a server side control and ASP.NET provides own tag to create it. The example
is given below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
Example
// user-form.aspx
This is a server side control and ASP.NET provides own tag to create it. The example
is given below.
Server renders it as the HTML control and produces the following code to the browser.
<input id="RadioButton1" type="radio" name="gender" value="RadioButton1" /
><labelforlabelfor="RadioButton1">Male</label>
This control has its own properties that are tabled below.
Property Description
Example
In this example, we are creating two radio buttons and putting in a group named
gender.
// WebControls.aspx
Code Behind
// WebControls.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
genderId.Text = "";
if (RadioButton1.Checked)
{
genderId.Text = "Your gender is "+RadioButton1.Text;
}
else genderId.Text = "Your gender is "+RadioButton2.Text;
}
}
}
ASP.NET LinkButton
It is a server web control that acts as a hyperlink. It is used to display a hyperlink-
style button control on the web page. ASP.NET provides a tag to create LinkButton
and has following syntax.
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LinkButtonExample
{
public partial class LinkButtonExample : System.Web.UI.Page
{
protected void LinkButton1_Click(object sender, EventArgs e)
{
Label1.Text = "Welcome to the javatpoint";
}
}
}
This is a server side control and ASP.NET provides own tag to create it. The example
is given below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
Example
Here, we are implementing file upload control in web form.
// WebControls.aspx
Code
// WebControls.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength
> 0))
{
string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UploadMultipleExample
{
public partial class UploadMultipleFilesExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength
> 0))
{
var count = 0;
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
uploadedFile.SaveAs(SaveLocation);
count++;
}
catch (Exception ex)
{
FileUploadStatus.Text = "Error: " + ex.Message;
}
}
if (count > 0)
{
FileUploadStatus.Text = count + " files has been uploaded.";
}
}
else
{
FileUploadStatus.Text = "Please select a file to upload.";
}
}
}
}