如何从代码后面访问asp:label文本属性?

时间:2011-10-07 20:38:02

标签: c# asp.net

我有一个带有一堆asp:label控件的用户控件,我需要从后面的代码访问他们的text属性。是否有捷径可寻?我尝试使用标签的ID.Text,但没有运气。

这是ascx代码:

<div id="labelContainer">
<asp:Label ID="productDescription" class="productDescription fontCaps" runat="server"
    Text="express" />
<br />
<asp:Label ID="serviceDescription" class="serviceDescription fontCaps" runat="server"
    Text="express 24" />
<div id="shipFrom">
    <label class="font8 fontCaps" style="margin-left: 15px;">
        From:</label><asp:Label ID="companyName" class="font8 fontCaps" Style="margin-left: 5px;"
            runat="server" Text="customer integration management" />
    <label class="meeterLabel font8 fontCaps">
        MEETER:</label>
    <asp:Label ID="meeterNo" class="meeterNumber font8 fontCaps" runat="server" Text="34001" />
    <br />
    <div id="fromAddress">
        <asp:Label ID="fromAddressLine1" class="font8 fontCaps" runat="server" Text="orbital park" />
        <br />
        <asp:Label ID="fromAddressLine2" class="font8 fontCaps" runat="server" Text="178-188 great west road" />
        <br />
        <asp:Label ID="fromTown" class="font8 fontCaps" runat="server" Text="hounslow" />
        <br />
        <asp:Label ID="fromCountry" class="font8 fontCaps" runat="server" Text="middlesex" />
        <asp:Label ID="fromPostcode" class="font8 alignRight fontCaps" runat="server" Text="tw4 6js" />
    </div>
    <div id="toAddress">
        <label class="font8 fontCaps">
            To:</label><asp:Label ID="orgName" class="font10 addressTabs fontCaps" runat="server"
                Text="organisation name" />
        <label class="font10  fontCaps" style="margin-left: 100px;">
            Tel: &nbsp;</label><asp:Label ID="phone" class="font10 fontCaps" runat="server" Text="0208 818 8000" />
        <br />
        <asp:Label ID="departmentName" class="font10 addressTabs2 fontCaps" runat="server"
            Text="department name" />
        <br />
        <asp:Label ID="toAddressLine1" class="font10 addressTabs2 fontCaps" runat="server"
            Text="address line 1" />
        <br />
        <asp:Label ID="toAddressLine2" class="font10 addressTabs2 fontCaps" runat="server"
            Text="address line 2" />
        <br />
        <asp:Label ID="toTown" class="font11 addressTabs2 fontCaps" runat="server" Text="town" />
        <br />
        <asp:Label ID="toCountry" class="font11 addressTabs2 fontCaps" runat="server" Text="county" />
        <br />
        <asp:Label ID="toPostcode" class="font11 addressTabs2 fontCaps" runat="server" Text="ub5 1aj" />
    </div>
    <div id="serviceCodeContainer" style="text-align: left;">
        <div id="svcCode">
            <label style="color: #fff; font-size: 11pt; font-family: Arial; font-weight: bold;
                vertical-align: text-top;">
                Handling:</label>
            <br />
            <asp:Label ID="serviceCode" class="fontCaps" Style="margin-left: 18px; font-size: 18pt;"
                Text="STD" runat="server" />
        </div>
        <asp:Label ID="dayTime" class="fontCaps alignFarRight alignTop fontBold font11" runat="server" />
        <asp:Label ID="pieceCount" Text="1/1" class="fontCaps pieceCountRight pieceCountTop fontBold font20"
            runat="server" />
        <label class="font8 fontCaps">
            Shipment No:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label><asp:Label ID="shipmentNo"
                class="font8 fontCaps" runat="server" Text="123456" />
        <br />
        <label class="font8 fontCaps">
            Consignee Ref:&nbsp;&nbsp;&nbsp;&nbsp;</label><asp:Label ID="consigneeRef" class="font8 fontCaps"
                runat="server" Text="Your Customer Reference" />
        <br />
        <label class="font8 fontCaps">
            Consignor Ref:&nbsp;&nbsp;&nbsp;</label><asp:Label ID="consignorRef" class="font8 fontCaps"
                runat="server" Text="Your Reference" />
    </div>
    <div style="text-align: center; margin-bottom: 5px;">
        <asp:Label ID="serviceCentre" Style="padding-right: 60px;" class="font20 fontBold fontCaps"
            runat="server" Text="hayes" />
        <asp:Label ID="hub" class="font20 fontBold fontCaps" runat="server" Text="hatfield" />
    </div>
</div>
<div>
    <div style="text-align: center; margin-bottom: 5px;">
        <asp:Image ID="Image1" ImageUrl="~/images/barcode.jpg" runat="server" />
    </div>
    <div style="text-align: center; margin-top: 5px; margin-bottom: 5px;">
        <asp:Label ID="routingCode" class="font8 fontBold fontCaps" runat="server" Text="2lgbub51aj+01000002" />
    </div>
    <div style="text-align: center;">
        <asp:Image ID="licensePlateImg" ImageUrl="~/images/barcode2.jpg" runat="server" />
    </div>
    <div style="text-align: center; margin-top: 5px;">
        <asp:Label ID="licensePlate" class="font8 fontBold fontCaps" runat="server" Text="(J)JD00 022 340 0100 0124" />
    </div>
</div>

但是当我尝试从用户控件后面的代码中访问它时,我没有Text属性。

3 个答案:

答案 0 :(得分:2)

您需要在用户控件中创建属性,如:

public string Label1Text
{
   get { return this.label1.Text; }
}

// using
this.myusercontrol.Label1Text;

或者尝试在用户控件中找到标签:

((Label)this.myusercontrol.FindControl('label_id')).Text

答案 1 :(得分:2)

我是通过在用户控件

上创建属性来实现的

背后的ascx代码。

public string FirstName
 { 

   get { return FirstNameTextBox.Text; }
   set { FirstNameTextBox.text= value; } 
 }
在aspx页面中,您可以访问它,如下所示:

MyUserControl.FirstName= "Bob";

从aspx访问它比在Ascx中将TextBox设置为公共文本框并直接访问TextBox更加清晰。

答案 2 :(得分:0)

在用户控件中公开属性,如下所示:

public string LabelText
{
    get
    {
        return Label1.Text;
    }
}

并在代码隐藏中引用该属性,如下所示:

string labelText = UserControl1.LabelText;