Search code examples
asp.netlocalizationresourcesresx

How to use resource file as data source for an editable listview?


My client wants to take control over the text contents on the website. And he wants an interface where he can see a and edit the resource file text.

I was successful showing the contents of resource file in my listview. Updating is the place where I am stuck. I just don't know what to write in the updating event. does anyone know a simple method ?

enter image description here

ResourceSet rs = Resources.resfile.ResourceManager.
                     GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true);

protected void Page_Prerender(object sender, EventArgs e)
{
    ListView1.DataSource = rs;
    ListView1.DataBind();
}
protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
    ListView1.EditIndex = -1;
}
protected void Updating(Object sender,ListViewUpdateEventArgs e)
{                
}

The following code works, but it doesn't refresh after edit, it refreshes after I update something else.

XmlDocument loResource = new XmlDocument();
loResource.Load(Server.MapPath("/App_GlobalResources/resfile.resx"));

XmlNode loRoot = loResource.SelectSingleNode(
                            string.Format("root/data[@name='{0}']/value",e.Keys[0].ToString()));

if (loRoot != null)
{
    loRoot.InnerText = e.NewValues[1].ToString();
    loResource.Save(Server.MapPath("/App_GlobalResources/resfile.resx"));
}    
ListView1.EditIndex = -1;

Solution

  • I just did a demo of your question and noticed the same problem. After googling around and reading some forum entries I stumbled accros this blog Rick Strahl pointed out that:

    Refreshing Resources

    After you’ve made changes to the resources you might actually like to see the new resources show up in the live user interface. [....] ASP.NET’s resource provider loads resources and the resources are forever cached until the application shuts down. As far as I know there’s no built-in way to release the resources, but the data provider here includes some logic for tracking each provider [...]

    As I did not want to implement a database to store ressources that can be update I tried this very dirty little thing! Just called Response.Redirect and forced a complete new Page.Request from the browser. So the server gets a new response and re-caches ressources!

    I am aware that this is no good solution but it worked for me. I tried to rebind ressources, unloaded appDomain and so on - nothing worked! I hope this answer will help you!

    protected void ListView1_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
    {
        string key = e.Keys[0].ToString();
        string value = e.NewValues[0].ToString();
    
        XmlDocument loResource = new XmlDocument();
        loResource.Load(Server.MapPath("App_GlobalResources/resFile.resx"));
    
        XmlNode loRoot = loResource.SelectSingleNode(
                                    string.Format("root/data[@name='{0}']/value", key));
    
        if (loRoot != null)
        {
            loRoot.InnerText = value;
            loResource.Save(Server.MapPath("App_GlobalResources/resfile.resx"));
        }
    
        ListView1.EditIndex = -1;
        Response.Redirect("demo.aspx");  // that's all!!!
    }