我需要找到一種聲明方式(而不是在文件後面的代碼中)將ASP.Net網頁中屬性的值傳遞給用戶控件。以下是我正在嘗試做的一個簡單示例,但我無法使其工作。 這裏是哪裏,我在創建用戶控制的對象aspx頁面的標記:ASP.Net聲明性地將頁面屬性的值傳遞給用戶控件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyUserControl ID="MyUserControl1" runat="server"
UserControlProperty = '<%# Bind("PageProperty") %>' />
</div>
</form>
</body>
</html>
這裏是後面的代碼從aspx頁面(aspx.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 _Default : System.Web.UI.Page
{
public int PageProperty { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
PageProperty = 42;
}
}
下面是從用戶控件(ASCX文件)標記:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
這裏是從用戶控制隱藏文件(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 MyUserControl : System.Web.UI.UserControl
{
public int UserControlProperty { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = UserControlProperty.ToString();
}
}
所以,我想要做的就是將在aspx頁面中定義的PageProperty屬性的值傳遞給用戶控件,以設置它的UserControlProperty,然後在usercontrol的文本框中顯示該值。誰能告訴我我在這裏做錯了什麼?
哪一個工作? :) – 0100110010101 2009-10-13 13:29:19