Getting text from the Resource file in asp.net 4

2019-02-25 12:00发布

问题:

i have an asp.net webpage, which contains a label, now i want to get the text property of the label from the localresource file, i have a .resx file under App_LocalResources folder, which contains Name as UserNameLabel.Text and Value as User Name now in my aspx file i am using Label control like this

<asp:Label ID="UserNameLabel" runat="server" resourcekey="UserNameLabel"></asp:Label>

but i cannot get the text on the Label, can anyone tell me the correct way to add the Text Property from resource file

回答1:

First you need to create appropriate structure inside web project. In this case I will be using Default.aspx:

Take notice I have placed Default.aspx.resx file inside App_LocalResources.

Next enter new item inside Default.aspx.resx like this:

The important thing is you need to set Text property (UserNameLabel.Text)

And finally here is aspx code:

<asp:Label ID="UserNameLabel" runat="server" meta:resourcekey="UserNameLabel"></asp:Label>

I have used meta:resourcekey to link to appropriate resource key.