Thursday, 28 April 2016

View and Updating a resource file.

 

Step: Add the Resource file in to your application.

Step 2: Add the Grid view into your Application.
<div>
        <asp:GridView ID="gvResourceEditor" runat="server" AutoGenerateColumns="False" OnRowEditing="gvResourceEditor_RowEditing"
            OnRowCancelingEdit="gvResourceEditor_RowCancelingEdit" AlternatingRowStyle-BorderColor="Green"
            AlternatingRowStyle-BackColor="CadetBlue" OnRowUpdating="gvResourceEditor_RowUpdating"
            CellPadding="0" BorderWidth="0px" Width="100%" meta:resourcekey="gvResourceEditorResource1">
            <Columns>
                <asp:TemplateField HeaderText="Key" meta:resourcekey="TemplateFieldResource1">
                    <EditItemTemplate>
                        <asp:Label ID="lblKey" runat="server" Text='<%# Eval("Key") %>' meta:resourcekey="lblKeyResource1"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblKey" runat="server" Text='<%# Eval("Key") %>' meta:resourcekey="lblKeyResource2"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="gridText tenXPadding bottomBorder rightBorder" Wrap="True" VerticalAlign="Top"
                        Width="10%" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TEXT" meta:resourcekey="TemplateFieldResource2">
                    <HeaderStyle BorderWidth="0" BorderColor="White" BorderStyle="None" />
                    <EditItemTemplate>
                        <asp:TextBox ID="txtValue" TextMode="MultiLine" CssClass="textBoxEnabled" runat="server"
                            Text='<%# Bind("Value") %>' Width="100%" Height="150px" meta:resourcekey="txtValueResource1"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblValue" runat="server" Text='<%# Bind("Value") %>' meta:resourcekey="lblValueResource1"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="gridText tenXPadding rightBorder bottomBorder" Wrap="True" Width="35%"
                        VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="COMMENTS" meta:resourcekey="TemplateFieldResource3">
                    <HeaderStyle BorderWidth="0" BorderColor="White" BorderStyle="None" />
                    <EditItemTemplate>
                        <asp:TextBox ID="txtComment" TextMode="MultiLine" CssClass="textBoxEnabled" runat="server"
                            Text='<%# Bind("Comment") %>' Width="100%" Height="150px" meta:resourcekey="txtDescriptionResource1"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Comment") %>' meta:resourcekey="lblDescriptionResource1"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="gridText tenXPadding rightBorder bottomBorder" Wrap="True" Width="35%"
                        VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" ItemStyle-CssClass="tenXPadding bottomBorder"
                    ShowEditButton="True" ControlStyle-CssClass="buttonEnabled" ItemStyle-VerticalAlign="Top"
                    HeaderStyle-BorderColor="White" HeaderStyle-BorderWidth="0" HeaderStyle-BorderStyle="None"
                    ItemStyle-Width="20%" meta:resourcekey="CommandFieldResource1">
                    <ControlStyle CssClass="buttonEnabled"></ControlStyle>
                    <HeaderStyle BorderColor="White" BorderWidth="0px" BorderStyle="None"></HeaderStyle>
                    <ItemStyle VerticalAlign="Top" CssClass="tenXPadding bottomBorder" Width="20%"></ItemStyle>
                </asp:CommandField>
            </Columns>
        </asp:GridView>
    </div>


Step 3: Add the below code into your code behind.
      protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();

        }




        private void LoadData()
        {
            string path = Server.MapPath("App_GlobalResources/Res_English.resx");
            //Loading the XML file using the Load method of XElement.
            XElement resourceElements = XElement.Load(path);
            //LINQ to load the resource items
            gvResourceEditor.DataSource = (from resElem in resourceElements.Elements("data")
                                           select new
                                           {
                                               Key = resElem.Attribute("name").Value,
                                               Value = HttpUtility.HtmlEncode(resElem.Element("value").Value),
                                               Comment = resElem.Element("comment") != null ?
                                                    HttpUtility.HtmlEncode(resElem.Element("comment").Value) : string.Empty
                                           });
            gvResourceEditor.DataBind();
        }



        protected void gvResourceEditor_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvResourceEditor.EditIndex = e.NewEditIndex; // setting new index
            LoadData();
        }


        protected void gvResourceEditor_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvResourceEditor.EditIndex = -1; // reseting grid view
            LoadData();
        }




        protected void gvResourceEditor_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //string theFileName = Server.MapPath("App_LocalResources/Default.aspx.resx");
            string theFileName = Server.MapPath("App_GlobalResources/Res_English.resx");
            Label lblKey = gvResourceEditor.Rows[e.RowIndex].FindControl("lblKey") as Label;
            TextBox txtValue = gvResourceEditor.Rows[e.RowIndex].FindControl("txtValue") as TextBox;
            TextBox txtComment = gvResourceEditor.Rows[e.RowIndex].FindControl("txtComment") as TextBox;
            //Loading the Resource file as an XML.
            XDocument xDoc = XDocument.Load(theFileName);
            //Filtering out key which matches currently updated record.
            var elementToBeEdited = from xle in xDoc.Element("root").Elements("data") where xle.Attribute("name").Value == lblKey.Text select xle;

            if (elementToBeEdited != null && elementToBeEdited.Count() > 0)
            {
                //Updating the new value
                elementToBeEdited.First().SetElementValue("value", HttpUtility.HtmlDecode(txtValue.Text));

                //Checking if there is a comment node, if no then adding a new comment node.
                if (elementToBeEdited.First().Element("comment") == null && !string.IsNullOrEmpty(txtComment.Text))
                {
                    elementToBeEdited.First().Add(new XElement("comment", HttpUtility.HtmlDecode(txtComment.Text)));
                }
                else if (elementToBeEdited.First().Element("comment") != null && !string.IsNullOrEmpty(txtComment.Text))
                {
                    //If comment node is present then updating the comment node.
                    elementToBeEdited.First().SetElementValue("comment", HttpUtility.HtmlDecode(txtComment.Text));
                }
                //Finally saving the Resource file.
                xDoc.Save(theFileName);
            }

            gvResourceEditor.EditIndex = -1;
            LoadData();
        }


Step 4: Output


No comments:

Post a Comment