Utilize Asp.Net WebForms Resources for localizing text with javascript in your projects

I am working on an ASP.NET WebForms application using c#.

Currently, I am dealing with an ImageButton in my code:

<asp:ImageButton ID="divSection_btnAdd" runat="server" 
OnClientClick="return  TConfirm(this,'<%$Resources:Resource, Confirm%>')"/>

Issue: I want to display the resource value instead of showing '<%$Resources:Resource, Confirm%>'.

The Resource key 'Confirm' is set to 'Are you sure to delete this item?'

How can I show the value of the resource key?

Answer №1

Here is a snippet of code that can be used to dynamically localize content in a C# code-behind file:

Within the page's C# code:

 protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);
        writer.Write(this.Localize(sb.ToString()));
    }
 private const string ResourceFileName = "Resource";
    private string Localize(string html)
    {
        MatchCollection matches = new Regex(@"Localize\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled).Matches(html);
        foreach (System.Text.RegularExpressions.Match match in matches)
        {
            html = html.Replace(match.Value, GetGlobalResourceObject(ResourceFileName, match.Groups[1].Value).ToString());
        }
        return html;
    }

Update an ImageButton with localization:

<asp:ImageButton 
    ID="divSection_btnAdd" 
    runat="server"     
    OnClientClick="return  TConfirm(this,'Localize(Confirm)')"/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using promises and the fetch function to connect to a database

I am attempting to utilize promises with the fetch() function in order to access MongoDB from the front-end, but I am encountering some issues. var Promise = () => ( new Promise((resolve, reject) => { //perform certain actions, make requests ...

Tips for ensuring that the horizontal scroll bar remains consistently positioned at the bottom of the screen

There are two div sections on a page, with one positioned on the left and the other on the right. The div on the right contains multiple dynamically generated tags which necessitate a horizontal scroll bar (overflow:auto). This causes the div's height ...

Update the default GET URL structure in Backbone

REST API: If you need to access my REST API, you can do so by sending a GET request to the following URL: http://localhost/Project/index.php/rest/resource/car This will retrieve all the data in the table in JSON format. Additionally, you can get specific ...

Employing VUE.js for content retrieval

Is there an issue rendering 2 messages in vue.js on the front end? <template v-for="item in items"> <span>{{ afterpayMessage }}: {{ item.price }} with AfterPay</span> </template> <script> var afterpay = new Vue({ e ...

Obtain a collection of information from a web API by utilizing jQuery

Click on the following link to access live data from a web API: This is my first attempt at retrieving data from an API, so I may have missed some settings. To test the connection with the API, I included the following code in the HTML page: <div id= ...

Utilizing JavaScript variable to apply style with JQuery

I'm having trouble getting the code below to work properly. var iZoomValue = Math.round(window.innerWidth/12.5); $('body').css("zoom",iZoomValue); Can anyone offer suggestions on what I might be doing incorrectly here? For more informati ...

The issue I'm facing with my webpack-build is the exclusive appearance of the "error" that

Hey everyone! I'm currently facing an issue with importing a module called _module_name_ into my React project, specifically a TypeScript project named react-app. The module was actually developed by me and it's published on npm. When trying to i ...

Is there a way to conceal a specific key and value within an array of objects?

Is there a way to display only the value string on the screen, instead of key and value pairs like "id":0 , "value":"lorem ipsum"? I'd like to accomplish this using the code below: main.html <li ng-repeat="message in event | limitTo:1000" ng-cla ...

Intermittent Cloudfront Content Delivery Network (CDN) disruptions (monitoring) - Implementing CDN Fail

Over the past two months, I've been dealing with sporadic issues involving Amazon Cloudfront. These failures occur 2-3 times a week, where the page will load from my web server but assets from the CDN linger in pending status for minutes at a time. It ...

I am trying to join three tables together, but I am encountering duplicate rows in the result

<asp:Panel ID = "Panel1" runat="server" ScrollBars="Auto"> <asp:GridView ID = "GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSourceDelete" DataKeyNames="IvrDataid,dayid,menudataid"> < ...

Filtering options for generating a report

I'm attempting to incorporate a filter parameter in a report within Visual Studio 2013, following the steps outlined in this tutorial. Reporting Service Tutorial - Basic Parameters However, in VS2013, when I try to add a parameter, I am unable to lo ...

appearing like a straightforward process of creating strings in JavaScript

Originally, I thought this task would be easy, but it ended up taking me the entire morning! var insert = '<div class="main_content_half_panel_circle" id="circle_' + c + '"></div><script type="text/javascript">$("#circle_& ...

Communicate through Node.js, with messages that multiply based on the total number of users currently in the chat room

Currently, I am working on a project that involves creating a chat application using Node.js. However, I have run into an issue where the message gets repeated for each user in the chat. For example, if there are 4 users in the chat, the message will be di ...

I'm encountering an issue with my array in JavaScript while using // @ts-check in VS Code. Why am I receiving an error stating that property 'find' does not exist on my array? (typescript 2.7

** update console.log(Array.isArray(primaryNumberFemales)); // true and I export it with: export { primaryNumberFemales, }; ** end update I possess an array (which is indeed a type of object) that is structured in the following manner: const primar ...

The use of Angular's ngClass directive does not work within the link function

I have a straightforward directive that renders an element, and this is the template: <div class="nav-item"></div> The .nav-item class looks like this: .nav-item { height: 50; } Here's the directive in action: angular.module('m ...

Steps for Adding a JSON Array into an Object in Angular

Here is a JSON Array that I have: 0: {name: "Jan", value: 12} 1: {name: "Mar", value: 14} 2: {name: "Feb", value: 11} 3: {name: "Apr", value: 10} 4: {name: "May", value: 14} 5: {name: "Jun", value ...

What steps should be taken to activate an event when an object is reorganized?

As I work on enhancing a website that is not my own, I encounter a table filled with values. To improve this site, I have created a jQuery script to scrape the values from the table, add a column, and calculate a specific value for each row. However, as us ...

Stop the form submission

Presently, I am dealing with this code snippet: $("#contact-frm-2").bind("jqv.form.result", function(event , errorFound){ if(!errorFound){ alert('go!'); event.preventDefault(); return false; ...

Developing a React-based UI library that combines both client-side and server-side components: A step-by-step

I'm working on developing a library that will export both server components and client components. The goal is to have it compatible with the Next.js app router, but I've run into a problem. It seems like when I build the library, the client comp ...

The React component fails to update even after altering the state in the componentDidMount lifecycle method

I've been working on a React component that displays a table with data retrieved from an API on the server. Here's the code snippet: var ModulesTable = React.createClass({ getInitialState: function () { return { module ...