I load Usercontrols dynamically in RadDocks.
I wish to Edit, update, cancel edition, see details of an Employee etc.
How can I do this ? I'm always getting the mistake : "Several controls with same ID have been found".
Also, please tell me if I can do better. Thanks
This is my user control :
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCEmployee.ascx.cs" Inherits="UCEmployee" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <table border="0" width="100%"> |
| <tr> |
| <td align="left" colspan="2">LastName : </td> |
| <td align="left" colspan="2"> <asp:TextBox runat="server" ID="TxtNom"></asp:TextBox> </td> |
| </tr> |
| <tr> |
| <td align="left" colspan="2">FirstName :</td> |
| <td align="left" colspan="2"> <asp:TextBox runat="server" ID="TxtPrenom"></asp:TextBox> </td> |
| </tr> |
| <tr> |
| <td colspan="4"> </td> |
| </tr> |
| <tr> |
| <td colspan="4"> |
| <nobr> |
| <asp:Button runat="server" ID="BtnUpdate" Text="Update" OnClick="BtnUpdate_Click"/> |
| <asp:Button runat="server" ID="BtnCancel" Text="Cancel" OnClick="BtnCancel_Click"/> |
| <asp:Button runat="server" ID="BtnDetails" Text="Details" OnClick="BtnDetails_Click"/> |
| </nobr> |
| </td> |
| </tr> |
| </table> |
| --------------------------------------- |
| using System; |
| using System.Data; |
| using System.Configuration; |
| 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; |
| using Telerik.Web.UI; |
| using System.Data; |
| using System.Data.SqlClient; |
| public partial class UCEmployee : System.Web.UI.UserControl |
| { |
| int currentIndex = 0; |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| if (currentIndex > 0) |
| { |
| DataTable dt = getEmployeeById(currentIndex); |
| if (dt != null && dt.Rows.Count > 0) |
| { |
| TxtNom.Text = dt.Rows[0]["FirstName"].ToString(); |
| TxtPrenom.Text = dt.Rows[0]["LastName"].ToString(); |
| } |
| } |
| } |
| public UCEmployee() |
| { |
| } |
| public UCEmployee(int passedIndex) |
| { |
| currentIndex = passedIndex; |
| } |
| private DataTable getEmployeeById(int EmployeeID) |
| { |
| DataSet ds = new DataSet(); |
| using (SqlConnection conn = new SqlConnection()) |
| { |
| conn.ConnectionString = GetConnectionString(); |
| SqlCommand cmd = new SqlCommand("SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo], [Photo], [Notes] FROM [Employees] where EmployeeID = " + EmployeeID, conn); |
| conn.Open(); |
| SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); |
| sqlAdapter.Fill(ds); |
| } |
| DataTable dt = ds.Tables[0]; |
| return dt; |
| } |
| static private string GetConnectionString() |
| { |
| return "server=STATION00374;database=Northwind;Integrated Security=SSPI;"; |
| } |
| protected void BtnUpdate_Click(object sender, EventArgs e) |
| { |
| } |
| protected void BtnCancel_Click(object sender, EventArgs e) |
| { |
| } |
| protected void BtnDetails_Click(object sender, EventArgs e) |
| { |
| } |
| } |
And this is my Page where I load all employees :
| <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestUcEmployee.aspx.cs" Inherits="TestUcEmployee" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <!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 id="Head1" runat="server"> |
| <title>Untitled Page</title> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <div> |
| <asp:ScriptManager ID="ScriptManager1" runat = "server"></asp:ScriptManager> |
| <br /> |
| <br /> |
| <table> |
| <tr> |
| <td style="width:100px"> </td> |
| <td align="right" id="tdDocks" runat="server" style="width:1000px"> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </form> |
| </body> |
| </html> |
| ------------------------------------ |
| using System; |
| using System.Data; |
| using System.Configuration; |
| 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; |
| using Telerik.Web.UI; |
| using System.Reflection; |
| using System.Configuration; |
| using System.Collections.Generic; |
| public partial class TestUcEmployee : System.Web.UI.Page |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| RadDockLayout layout1 = new RadDockLayout(); |
| RadDockZone zone1 = new RadDockZone(); |
| for (int i = 1; i < 12; i++) |
| { |
| zone1.Width = Unit.Pixel(900); |
| zone1.Orientation = Orientation.Horizontal; |
| if (i % 2 == 0) |
| { |
| zone1.BackColor = System.Drawing.Color.PeachPuff; |
| } |
| else |
| { |
| zone1.BackColor = System.Drawing.Color.AliceBlue; |
| } |
| RadBinaryImage image = new RadBinaryImage(); |
| image.ResizeMode = BinaryImageResizeMode.Fit; |
| image.Height = Unit.Pixel(100); |
| image.Width = Unit.Pixel(70); |
| LiteralControl liSpace = new LiteralControl(" "); |
| LiteralControl liTitle = new LiteralControl("Title"); |
| Table table = new Table(); |
| table.BorderWidth = Unit.Pixel(0); |
| table.Width = Unit.Percentage(85); |
| TableRow tr1 = new TableRow(); |
| TableRow tr2 = new TableRow(); |
| tr1.HorizontalAlign = HorizontalAlign.Center; |
| tr2.HorizontalAlign = HorizontalAlign.Center; |
| TableCell tc1 = new TableCell(); |
| tc1.Controls.Add(liSpace); |
| TableCell tc2 = new TableCell(); |
| /* |
| if (!dt.Rows[i]["Photo"].Equals(System.DBNull.Value)) |
| { |
| image.DataValue = (byte[])dt.Rows[i]["Photo"]; |
| } |
| tc2.Controls.Add(image); |
| */ |
| Control widget = LoadControl("UCEmployee.ascx", i); |
| tc2.Controls.Add(widget); |
| TableCell tc3 = new TableCell(); |
| tc3.Controls.Add(liSpace); |
| tr1.Cells.Add(tc1); |
| tr1.Cells.Add(tc2); |
| tr1.Cells.Add(tc3); |
| TableCell tc4 = new TableCell(); |
| tc4.Controls.Add(liSpace); |
| TableCell tc5 = new TableCell(); |
| tc5.Controls.Add(liSpace); |
| TableCell tc6 = new TableCell(); |
| tc6.Controls.Add(liSpace); |
| tr2.Cells.Add(tc4); |
| tr2.Cells.Add(tc5); |
| tr2.Cells.Add(tc6); |
| table.Controls.Add(tr1); |
| table.Controls.Add(tr2); |
| RadDock dock = new RadDock(); |
| dock.UniqueName = Guid.NewGuid().ToString(); |
| dock.ID = string.Format("RadDock{0}", dock.UniqueName); |
| dock.Title = "Title";//dt.Rows[i]["FirstName"].ToString() + " " + dt.Rows[i]["LastName"].ToString(); |
| dock.TitlebarContainer.HorizontalAlign = HorizontalAlign.Center; |
| dock.ContentContainer.Controls.Add(table); |
| RadDock dockSpace = new RadDock(); |
| dockSpace.UniqueName = Guid.NewGuid().ToString(); |
| dockSpace.ID = string.Format("RadDock{0}", dock.UniqueName); |
| dockSpace.Style["Width"] = "900px"; |
| if (i % 3 == 0) |
| { |
| dock.Style["Width"] = "300px"; |
| zone1.Controls.Add(dock); |
| dockSpace.ContentContainer.Controls.Add(new LiteralControl("<table width='100%' height='10px' border='0'><tr><td> </td> <td> </td> <td></td></tr></table>")); |
| zone1.Controls.Add(dockSpace); |
| } |
| else |
| { |
| dock.Style["Width"] = "300px"; |
| zone1.Controls.Add(dock); |
| } |
| } |
| layout1.Controls.Add(zone1); |
| tdDocks.Controls.Add(layout1); |
| } |
| private UserControl LoadControl(string UserControlPath, params object[] constructorParameters) |
| { |
| List<Type> constParamTypes = new List<Type>(); |
| foreach (object constParam in constructorParameters) |
| { |
| constParamTypes.Add(constParam.GetType()); |
| } |
| UserControl ctl = Page.LoadControl(UserControlPath) as UserControl; |
| // Find the relevant constructor |
| ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()); |
| //And then call the relevant constructor |
| if (constructor == null) |
| { |
| throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString()); |
| } |
| else |
| { |
| constructor.Invoke(ctl, constructorParameters); |
| } |
| // Finally return the fully initialized UC |
| return ctl; |
| } |
| } |