2014-09-10 17 views
-1

我用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。

+0

檢查此問題http://stackoverflow.com/questions/4840985/asp-net-register-vs-reference – 2014-09-10 23:26:25

回答

0

我不能讓你的代碼工作。

我要麼不得不

var ctrl = (ProjectName.Controls.sTextBox) Page.LoadControl("~/Controls/sTextBox.ascx"); 

或導入控制

using ProjectName.Controls; 

當我這樣做,它的工作。

此外,您的設置屬性也不工作,我不得不改變它。

public string Text { 
     get 
     { 
      return tbNothing.Text; 
     } 
     set 
     { 
      tbNothing.Text = value; 
     } 
    } 

然後我加入1-更多文本到控制共計2.我改變了ID來tb1Text和tb2Text。然後我就得到2種方法爲我GET套,這是

public string tb1Text { 
     get 
     { 
      return tb1.Text; 
     } 
     set 
     { 
      tb1.Text = value; 
     } 
    } 

    public string tb2Text 
    { 
     get 
     { 
      return tb2.Text; 
     } 
     set 
     { 
      tb2.Text = value; 
     } 
    } 

在我的默認代碼後面,我只好用

 sTextBox ctrl = (sTextBox)Page.LoadControl("~/Controls/sTextBox.ascx"); 
     ctrl.tb1Text = "something"; 
     ctrl.tb2Text = "something 2"; 
     PlaceHolder1.Controls.Add(ctrl); 

這個工作,現在我知道如何在使用2個文本框1控制:)。希望它與其他控件一樣,我必須:S

+0

真棒!我提供的快速示例代碼與控件和默認頁面具有相同的名稱空間。 (這不適合你)。此外,C#3.0中引入了自動屬性(不確定使用的是哪個版本),但如果您使用的是C#3.0及更高版本,則可以使用get;組;快樂編程:) – 2014-09-11 19:03:22

+0

謝謝。你也確定你可以使用get;組;?我正在使用框架4.0。我在上面做錯了什麼? – jderridew 2014-09-11 20:06:50

+0

當你使用自動屬性時,你會得到什麼錯誤。此外,我建議檢查這個獲得一個概述http://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose – 2014-09-11 20:10:20

0

您可以通過向自定義控件添加屬性來獲得該行爲。

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var ctrl = (sTextBox) Page.LoadControl("~/sTextBox.ascx"); 
     ctrl.Text = "something"; 
     placeHolder1.Controls.Add(ctrl); 
    } 
} 

用戶控制: -

public partial class sTextBox : System.Web.UI.UserControl 
{ 
    public string Text { get; set; } 
} 
+1

我試過這個問題我得到它在'sTextBox'下有一個錯誤,說'無法找到類型或名稱空間。'這一定是主要的錯誤。我甚至無法在代碼中創建一個sTextBox對象,更不用說對它進行類型轉換了。 – jderridew 2014-09-11 00:29:52

相關問題