ASP.Net Webform UserControl设置标签文本

时间:2016-04-22 11:04:50

标签: c# asp.net user-controls

我在asp.Net中有一个UserControl。我想在本课题中设置属性 Can values or parameters be passed into user control?

我把这样的控件放到我的网站

    <uc1:BootstrapTextRow runat="server" id="BootstrapTextRow" Text="" LabelText="Season"/>

这是UserControl ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BootstrapTextRow.ascx.cs" Inherits="RANDOMAPP.UserControls.TextRow" %>
<div class="row">
    <div class="col-md-2">
        <asp:Label ID="lblLabel" runat="server" Text='<%= this.LabelText %>' CssClass="control-label" AssociatedControlID="tbTextbox"></asp:Label>
    </div>
    <div class="col-md-10">
        <asp:TextBox ID="tbTextbox" runat="server" CssClass="form-control input-sm" Text='<%= this.Text %>'></asp:TextBox>
    </div>
</div>

这里是我的CS文件

public partial class TextRow : System.Web.UI.UserControl
{
    private String m_LabelText;

    public String LabelText
    {
        get { return m_LabelText; }
        set { m_LabelText = value; }
    }

    private String m_Text;

    public String Text
    {
        get { return m_Text; }
        set { m_Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

但我的网站看起来像这样

enter image description here

如何才能显示正确的文本?

1 个答案:

答案 0 :(得分:0)

您可以从标记中删除Text='<%= ... %>'

<asp:Label ID="lblLabel" runat="server" CssClass="control-label" AssociatedControlID="tbTextbox" />
<asp:TextBox ID="tbTextbox" runat="server" CssClass="form-control input-sm" />

并定义您的属性:

public String LabelText
{
    get { return lblLabel.Text; }
    set { lblLabel.Text = value; }
}

public String Text
{
    get { return tbTextBox.Text; }
    set { tbTextBox.Text = value; }
}