Freelance Jobs

How To Apply Runtime Template

Hello Friends, Once again I am here to share one of the ADO.NET application,
that are generally used in our daily routine.
Guess it.......Guess........ Yes
How To Apply Template At Runtime

For this application we require: -
1) A table as I had named "tbemp" and its columns are
a) empno
b) ename
c) eaddr
d) esal

2) Two Web User Control named
"WebUserControl.ascx" and "WebUserControl2.ascx"

3) One Web Form called "Default.aspx"

4) RepeaterControl on the WebForm.

------------------------------------------------------------------------------------------------------
In First WebUserControl called "WebUserContro.ascx"

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

<table>
<tr style="background-color:Yellow">
<td>
<b>EmpNo :</b><%#Eval("empno")%><br />
<b>Name :</b><%#Eval("ename")%><br />
<b>Address :</b><%#Eval("eaddr")%><br />
<b>Salary :</b><%#Eval("esal")%><br />
</td>
</tr>
</table>


In Second WebUserControl called "WebUserControl2.ascx"


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

<table>
<tr style="background-color:ThreeDShadow">
<td>
<b>EmpNo :</b><%#Eval("empno")%><br />
<b>Name :</b><%#Eval("ename")%><br />
<b>Address :</b><%#Eval("eaddr")%><br />
<b>Salary :</b><%#Eval("esal")%><br />
</td>
</tr>
</table>


Code for the Default.aspx [Source Mode]

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

<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>

</div>
</form>

Code for the Default.aspx.cs


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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.ItemTemplate = Page.LoadTemplate("WebUserControl.ascx");
Repeater1.AlternatingItemTemplate = Page.LoadTemplate("WebUserControl2.ascx");

SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", "server=monty\\baljeet;database=dbemp;uid=sa;pwd=123456789");
DataSet ds = new DataSet();
adp.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();

}
}

0 Comments:

Post a Comment



ONLINE JOBS