如果我對你有所幫助 - 你需要爲每個控件擁有唯一的標識符,對嗎?如果我的權利,你可以做下一個:
UserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication2.WebUserControl1" %>
<asp:HiddenField ID="hdnID" runat="server" Value='<%# Bind("ID")' />
和UserControl1.cs:
namespace WebApplication2
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string HiddenFildProperty
{
get { return hdnID.Value; }
set { hdnID.Value = value; }
}
}
}
,比你可以在任何模板控件使用該控件,如直放站,像這樣:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<mycontrols:WebUserControl1 ID="myControl1" HiddenFildProperty='<%#Bind("ID") %>'/>
</ItemTemplate>
</asp:Repeater>
同樣,你可以綁定你的控件中的任何其他屬性,例如可怕的可怕在你的DropDownExtander中鏈接。
「我想避免代碼重用」。你的意思是相反的,不是嗎? :) – Dante