【发布时间】:2014-04-26 20:59:52
【问题描述】:
我在 ascx 页面中构建了一个带有 Web 控件的用户控件。 //ascx文件
<%@ 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>
在用户控件的 cs 文件中,我创建了一个获取另一个类引用的构造器。 我在contructor中获得的课程我想将数据插入到子控件中。 //ascx.cs文件
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);
}
}
问题在于,现在使用来自 aspx 文件的特殊构造函数 Web 控件不 初始化。我尝试使用 new 关键字在代码中初始化它们,但它们没有来自 ascx 文件的属性。没有特殊的构造函数,它工作正常。但我需要这个特殊的构造函数。我如何使用 ascx 文件中的属性初始化这个 webcontrols
【问题讨论】:
-
能否给我们看一些代码,以便更容易提供帮助?
-
请显示更多代码,有点难以理解这种方式出了什么问题
标签: c# asp.net user-controls