Introduction: Hello friends, in this article i will explain that how we can call webuser control at the .cs page dynamically using sapx.cs code. The webuser control is very useful control in asp.net. with the help of the webuser control the part of the code can be seprated from the .aspx.cs page. According to my experience , we can use webuser control where some designning issues are created, then you can create webuser control and call that webuser control at the part of the .aspx page.
Implementation: create a new website abb a page named table_call_web_user_control.aspx. and a webuser control named talble_user_control.ascx. Below i am giving the complete code for the html page and .cs page and the ascx page and ascx.cs page.
Code for talble_user_control.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="talble_user_control.ascx.cs" Inherits="talble_user_control" %>
<table >
<tr>
<td>
Enter value</td>
<td>
<asp:TextBox ID="txt_table" runat="server" MaxLength="8" placeholder="hh" ToolTip="Enter number"></asp:TextBox>
<asp:RegularExpressionValidator ID="regular_exp_for_txt_ans" runat="server" ErrorMessage="Enter digits only"
ControlToValidate="txt_table" ValidationExpression="^-{0,1}\d+$" BackColor="Beige"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btn_table" runat="server" onclick="btn_table_Click"
Text="Sumbit" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl_table" runat="server" BorderColor="Brown"></asp:Label>
</td>
</tr>
</table>
Code for talble_user_control.ascx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
// this namespace in necessary for the stringbuilder
using System.Text;
public partial class talble_user_control : System.Web.UI.UserControl
{
// here m declaring the variale
int n;
// here m declairing a StringBuilder class
StringBuilder str = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
txt_table.BackColor = System.Drawing.Color.FromName("#DEB887");
}
protected void btn_table_Click(object sender, EventArgs e)
{
if(txt_table.Text=="")
{
lbl_table.Text = "Enter any number";
}
else
{
// in the n variable m catching the value from the textbox, means when user will enter the value that value
// will store inside the n variable
n = Convert.ToInt32(txt_table.Text);
// here m declaring the variable named i as integer
int i;
// for loop will work for 10 time from 1 to 10
for (i = 1; i <= 20; i++)
{
// this line will print the table
//n + " * " + i + " = " + n*i
// first n means value enter by the user
//i mean 1,2,3 upto ten
//n*i mean multiplication suppose user enters 3 then..
// 3*1 3*2 upto 10
// above commented 4 lines print the output as like
//3 * 1 = 3
//3 * 2 = 6
// and so on upto ten
str.Append(n + " * " + i + " = " + n * i + "<br/>");
}
// here m print the output inside the label which will come from the stringbuilder
lbl_table.Text = str.ToString();
// here m emptying the textbox after printing the output
txt_table.Text = "";
}
}
}
Code for talble_user_control.aspx page
<head runat="server">
<title>Table</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- here i am placing PlaceHolder for calling webusercontrol--%>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Code for talble_user_control.aspx.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class table_call_web_user_control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// here m calling dynamically Webuser control at this page and add it into the PlaceHolder1 control
Control add = (Control)(Page.LoadControl("talble_user_control.ascx"));
PlaceHolder1.Controls.Add(add);
}
}
See output in this image:
Conclision: In above code, i just get the get the table by using string builder inside the webuser control . This code is very helpful for every .net developer.