我用1個文本框做了一個簡單的控制。不知道如何在代碼後面使用自定義控件
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="sTextBox.ascx.cs" Inherits="TestingASPNET.Controls.sTextBox" className="sTextBox"%>
<asp:Textbox runat="server" ID="tbNothing"/>
<br />
我在我的default.aspx中調用這個控件作爲參考這裏是簡單的代碼。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="TestingASPNET._default" %>
<%@ Reference Control="~/Controls/sTextBox.ascx"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" id="PlaceHolder1" />
</div>
</form>
</body>
</html>
在我的代碼背後default.aspx.cs我有。
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(LoadControl("~/Controls/sTextBox.ascx"));
PlaceHolder1.Controls.Add(LoadControl("~/Controls/sTextBox.ascx"));
}
這將2個sTextBox添加到我的頁面上。
我遇到的問題是如何使用控件,就像我會正常的文本框。例如。
TextBox tb = new TextBox();
tb.Text = "textbox";
PlaceHolder1.Controls.Add(tb);
這會在頁面上添加一個文本框,其中包含文本「textbox」。
有人可以給我一種方法來做到這一點,但與控制sTextBox。
檢查此問題http://stackoverflow.com/questions/4840985/asp-net-register-vs-reference – 2014-09-10 23:26:25