开发者

How to access asp:label text property from code behind?

I have a user control with a bunch of asp:label controls and I need to access their text property from the code behind. Is there an easy way to do this? I tried using the ID.Text of the label, with no luck.

Here is the ascx code:

<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"
    开发者_如何学C        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>

But when I try to access it from the code behind of the user control, I get no Text property.


You need to create property in user control like:

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

// using
this.myusercontrol.Label1Text;

Or try to find label inside of user control:

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


I do it by creating properties on the user conrtrol

ascx code behind.

public string FirstName
 { 

   get { return FirstNameTextBox.Text; }
   set { FirstNameTextBox.text= value; } 
 }

in the aspx page you can access it as shown:

MyUserControl.FirstName= "Bob";

It's a lot cleaner to access it that way from the aspx than it is to set the TextBox as a public textbox in the Ascx and accessing the TextBox directly.


Expose a property in the user control, like this:

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

And reference the property in the code-behind, like this:

string labelText = UserControl1.LabelText;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜