Reputation: 1
i have built a user control with web controls in ascx page. //ascx file
<%@ Control Language="C#" AutoEventWireup="true" ClassName="Product"CodeFile="Product.ascx.cs" Inherits="Usercontrols_Product" %>
<link href="../StyleSheet/StyleSheet.css" rel="stylesheet" type="text/css" />
<div>
<asp:Panel ID="box" CssClass="productBox" runat="server">
<asp:Image ID="imgProduct" CssClass="productImage" runat="server" /><br />
<asp:Label ID="lblProductName" CssClass="productLbl" runat="server"></asp:Label><br/>
<asp:Label ID="lblProductPrice" CssClass="productLbl" runat="server"></asp:Label>
</asp:Panel>
</div>
in the cs file of user control i created acontructor which get another class reference. whith the class i get in contructor i want insert data to child controls. //ascx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Usercontrols_Product : System.Web.UI.UserControl
{
public EventHandler cmdDetailsClick;
protected Product thisProduct;
public Usercontrols_Product( Product pr)
{
thisProduct = pr;
imgProduct.ImageUrl = thisProduct.ImageUrl;
lblProductName.Text = thisProduct.CompanyName + "<br/>" + thisProduct.ProductName + " " + thisProduct.Model;
lblProductPrice.Text = thisProduct.Price.ToString() + " " + "israeli shekel";
}
public Usercontrols_Product()
{
}
protected void cmdContinue_clicked(object sender, EventArgs e)
{
//we send to event product class of sender
if (cmdDetailsClick != null)
cmdDetailsClick(thisProduct, EventArgs.Empty);
}
}
the problem is that now with spesial constructor web controls from aspx file are not being initiallized. i tried initiallize them in code with new keyword but than they are with no attributes from ascx file. with no special constructor its works fine .but i need this special constructor. how can i initiallize this webcontrols whith their attributes from ascx file
Upvotes: 0
Views: 2970
Reputation: 22054
The UserControl is instantiated by Page (i.e. constructor of user control is out of your control) - you can define one, but the default constructor will be called instead. If you want to pass data, there are numerous techniques how to do so; simplest way:
public Product Product {
set {
thisProduct = value;
imgProduct.ImageUrl = value.ImageUrl;
lblProductName.Text = value.CompanyName + "<br/>" +
value.ProductName + " " + value.Model;
lblProductPrice.Text = value.Price.ToString() + " " + "israeli shekel";
}
}
To clarify how the process work on behind:
.aspx (or .ascx) file are being compiled on runtime by IIS. So for example if you have simple user control:
<%@ Control CodeBehind="FooControl.ascx.cs" Inherits="TestApplication.FooControl" %>
<asp:Literal ID="litBar" runat="server" Text="Whatever" />
IIS will make something like this from your control (shortened):
namespace ASP {
public class foocontrol_ascx : global::TestApplication.FooControl {
private static bool @__initialized;
public foocontrol_ascx() {
((global::WebApplication4.FooControl)(this)).AppRelativeVirtualPath = "~/FooControl.ascx";
if ((global::ASP.foocontrol_ascx.@__initialized == false)) {
global::ASP.foocontrol_ascx.@__initialized = true;
}
}
private global::System.Web.UI.WebControls.Literal @__BuildControllitBar() {
global::System.Web.UI.WebControls.Literal @__ctrl;
@__ctrl = new global::System.Web.UI.WebControls.Literal();
this.litBar = @__ctrl;
@__ctrl.ID = "litBar";
@__ctrl.Text = "Whatever";
return @__ctrl;
}
private void @__BuildControlTree(foocontrol_ascx @__ctrl) {
global::System.Web.UI.WebControls.Literal @__ctrl1;
@__ctrl1 = this.@__BuildControllitBar();
System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));
@__parser.AddParsedSubObject(@__ctrl1);
}
protected override void FrameworkInitialize() {
base.FrameworkInitialize();
this.@__BuildControlTree(this);
}
}
}
Notice the built class inherits from your code behind and takes care about the markup as well (see how it creates literal litBar and set text to "Whatever"). Also this class has only one constructor - default constructor (so it naturally calls default constructor from class in code behind). I.E. - you can define custom constructor for your control but you have to provide default constructor anyway (so the built can work with it) and the built class ignores it (as the IIS can have no clue what parameters you want to pass to your custom constructor).
Upvotes: 1