Search This Blog

Tuesday, September 6, 2011

How to access the data from Resource file from code behind or javascript in asp.net

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RaisingMessages.aspx.cs"
    Inherits="RaisingMessages" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Validation</title>

    <script type="text/javascript">

        function ValidatePage() {
            var fName = document.getElementById("txtFirstName");
            var lName = document.getElementById("txtLastName");

            if (fName.value == "") {
                // access resource file from javascript
                alert('<%= Resources.MessageResource.Mandatory_FirstName %>');
                return false;
            }
            else if (lName.value == "") {
                alert('Message from resource file : ' + '<%= Resources.MessageResource.Mandatory_LastName %>');
                return false;
            }
            return true;
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                    OnClientClick="return ValidatePage();" onclick="btnSubmit_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

.aspx.cs

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // access resource file from code behind
        lblResult.Text = Resources.MessageResource.Result_Msg + ": First Name : " +    txtFirstName.Text.Trim() + ": Last Name : " + txtLastName.Text.Trim();
    }

Resource File


Post a Comment

Popular Posts