Resource Files in ASP.NET

A resource file is an XML file that contains the strings that you want to translate into different languages or paths to images.The resource file contains key/value pairs. Each pair is an individual resource. Key names are not case sensitive.

Types of Resources:
There are two types of resources in asp.net ,
  1. Local Resources
  2. Global Resources
    Local Resources 

    • Local resource is specific to a single Web page and used for providing versions of a Web page in different languages.
    • Local resources must be stored in App_LocalResources sub folder. for Example Default.aspx.resx.
      How to create local Resource file 

      for example you want and to create local resource file for Default.aspx page then open the page and select tool menu 

      Tools-->Generate Local Resources 
      this will create App_LocalResources folder with Text and ToolTip values for all existing controls on the page, as well as the page title will be generated.
      the file Default.aspx.resx. look like this .
       How to access the resource file 
      1. Access Resource file on Source file :- If you want to use an value from Default.aspx.resx on the lable use this syntex,                                                       <asp:Label ID="lblMessage" runat="server" Text="<%$ Resources:Default, PageResource1.Title%>"></asp:Label>                                                                   <%$ Resources:Class, ResourceID %>

        Where Class:
        Identifies the resource file to be used
        ResourceID: Identifier of the resource to be read from resource file                         
      2. Access Resource file on the .cs file :-If you want to use the resource file on .cs file use this syntex.  (string)GetLocalResourceObject("ResourceID"); 
      Global Resource File
      • Global resource can be read from any page or code that is in the application.
      • Global resource must be stored in App_GlobalResources at the root of the application.
        How to create Global file 
        Right click on Solution Explorer--> Add New Item-->Resource File
        How to use Global file
      1)On Source file :- Text="<%$ Resources:Default,Global, ResourceID%>">
      2)On .cs file :-String test=Resources.Global.ResourceID;


0 comments:

C#.NET: How to create ASP.NET web service


Hi Friends,
 Here i am writing a code for Web services means how to create and user a simple example of web services into your asp.net application using C#.
 
To create your first web service project, just follow these steps:
1. Open your Visual Studio .NET and click File > New Website. This will display the project templates. Choose ASP.NET Web Service from the installed templates as shown below.



2. Visual Studio .NET should generate your first Hello web service method. Lets add another web service method.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int AddTwo(int n1,int n2)
        {
            return n1 + n2;
        }
    }
}

if you can notice the obvious difference of our web service method with an ordinary C#  method is this additional attribute before the function name:
[WebMethod] //this will indicate that this is a Web service method


It tells the framework that the following method is a web service.

3. To test the web service project, Click the Start Debugging Visual Studio will automatically open your default web browser and display all available web service method.








4. Now lets test one of the web service method. Click the Addthis method. You should be forwarded to the following page:




AddThis requires two parameters. Try to put numbers on each textbox then click Invoke. The web service method will return an XML result like the one below:



After Creating this web Services you can use this web services into your web application lets take an example for that , I have a button on the click event I simple user the Web services with Add Method ,

protected void Button1_Click(object sender, EventArgs e)
        {
            Service1 obj = new Service1();
            Label1.Text = obj.AddTwo(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)).ToString(); ;

        }
Here Service1 is the Webservice file name 

Hope this code also Helps you

Regards,
Rajesh 

0 comments: