did you hook up Page_Load with Load event, for example #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion or simply change <%@ Control Language="c#" AutoEventWireup="false" ...%> ===> <%@ Control Language="c#" AutoEventWireup="true" ...%>
The problem has been cleared up.Thank you very much.By the way ,I want to make friend with you ,can I have the honor?My Email is [email protected],thank you.
public Control LoadControl(string virtualPath);
用户控件代码如下:
GoodBook.ascx
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="GoodBook.ascx.cs" Inherits="ElectronicBookShop.GoodBook" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<FONT face="宋体"></FONT><span style="FONT-SIZE: 8pt; FONT-FAMILY: verdana">
<asp:datagrid id="GoodBookGrid" GridLines="vertical" Font-Size="8pt" Font-Name="Verdana" CellPadding="2"
BorderColor="lightgray" BorderWidth="1" RunAt="server">
<HeaderStyle BackColor="maroon" ForeColor="white" HorizontalAlign="center" />
<ItemStyle BackColor="white" ForeColor="black" />
<AlternatingItemStyle BackColor="beige" ForeColor="black" />
</asp:datagrid> </span>
GoodBook.ascx.cs
public class GoodBook : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid GoodBookGrid; Unit width;
public Unit Width
{
get
{
return width;
}
set
{
width=value;
}
} private void Page_Load(object sender, System.EventArgs e)
{
if(width!=Unit.Empty)
GoodBookGrid.Width=width;
//将书名和价钱填入到DataSet中
DataSet ds=new DataSet();
DataTable book=new DataTable("GoodBook");
ds.Tables.Add(book); //初始化book表
DataColumn NameCol =new DataColumn("Name",typeof(string));
DataColumn PriceCol=new DataColumn("Price",typeof(string));
book.Columns.Add(NameCol);
book.Columns.Add(PriceCol); DataRow row =book.NewRow();
row["Name"]="C Sharp";
row["Price"]=String.Format("{0:c}",60);
book.Rows.Add(row); row =book.NewRow();
row["Name"]="Visual C++";
row["Price"]=String.Format("{0:c}",30);
book.Rows.Add(row); row =book.NewRow();
row["Name"]="Thinking in Java";
row["Price"]=String.Format("{0:c}",50);
book.Rows.Add(row);
row =book.NewRow();
row["Name"]="Thinking in C++";
row["Price"]=String.Format("{0:c}",50);
book.Rows.Add(row);
row =book.NewRow();
row["Name"]="DotNet FrameWork";
row["Price"]=String.Format("{0:c}",50);
book.Rows.Add(row); //将DataGrid绑定到DataSet
GoodBookGrid.DataSource=ds;
GoodBookGrid.DataBind(); }
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
or simply change <%@ Control Language="c#" AutoEventWireup="false" ...%>
===>
<%@ Control Language="c#" AutoEventWireup="true" ...%>
谢谢!