簡體   English   中英

如何使用資源文件在運行時從網格視圖插入和編輯

[英]How to Insert and edit from the grid-view on run time using resource file

我正在處理資源文件。 現在,我可以讀取resx文件並將其填充到網格視圖中。 現在這是我的問題,

在運行時,我希望能夠編輯列,還能夠單擊空列,然后單擊“保存”以保存更改。 我怎么做。 請幫我,因為我嘗試了很多例子,但沒有用。

我下面的代碼

    private void btnNewfile_Click(object sender, EventArgs e)
    {          
            for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
            {
                string comment = oDataSet.Tables["data"].Rows[i][2].ToString();

                string font = Between(comment, "[Font]", "[/Font]");
                string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
                string commentVal = Between(comment, "[Comment]", "[/Comment]");

                string[] row = new string[] { oDataSet.Tables["data"].Rows[i][0].ToString(), oDataSet.Tables["data"].Rows[i][1].ToString(), font, datestamp, commentVal };
                Gridview_Output.Rows.Add(row);
            }
            oDataSet.Tables.Add(oDataTable);
            oDataSet.WriteXml(PathSelection);
        }

保存按鈕(用戶必須能夠將創建的文件保存或編輯到任何位置(C驅動器))

    private void btnSave_Click(object sender, EventArgs e)
    {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"C:\";
            saveFileDialog1.Title = "Save Resource Files";
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "resx";
            saveFileDialog1.Filter = "Save Resource Files (*.resx)|*.resx";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //nere i need the user to save to any location he want not textbox.
                txtOutputfile.Text = saveFileDialog1.FileName;                    
            }
            //oDataSet.Tables.Add("Data");
            oDataSet.WriteXml(PathSelection);               
            versionIncrement();
            MessageBox.Show("Successfully added ");
        }

查看以下代碼是否有幫助。

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateResource.aspx.cs" Inherits="UpdateResource" %> <!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>Update Resource</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { //Enable Disable all TextBoxes when Header Row CheckBox is checked. $("[id*=chkHeader]").bind("click", function () { var chkHeader = $(this); //Find and reference the GridView. var grid = $(this).closest("table"); //Loop through the CheckBoxes in each Row. $("td", grid).find("input[type=checkbox]").each(function () { //If Header CheckBox is checked. //Then check all CheckBoxes and enable the TextBoxes. if (chkHeader.is(":checked")) { $(this).attr("checked", "checked"); var td = $("td", $(this).closest("tr")); td.css({ "background-color": "#00000" }); $("input[type=text]", td).removeAttr("disabled"); } else { $(this).removeAttr("checked"); var td = $("td", $(this).closest("tr")); td.css({ "background-color": "#FFF" }); $("input[type=text]", td).attr("disabled", "disabled"); } }); }); //Enable Disable TextBoxes in a Row when the Row CheckBox is checked. $("[id*=chkRow]").bind("click", function () { //Find and reference the GridView. var grid = $(this).closest("table"); //Find and reference the Header CheckBox. var chkHeader = $("[id*=chkHeader]", grid); //If the CheckBox is Checked then enable the TextBoxes in thr Row. if (!$(this).is(":checked")) { var td = $("td", $(this).closest("tr")); td.css({ "background-color": "#FFF" }); $("input[type=text]", td).attr("disabled", "disabled"); } else { var td = $("td", $(this).closest("tr")); td.css({ "background-color": "#00000" }); $("input[type=text]", td).removeAttr("disabled"); } //Enable Header Row CheckBox if all the Row CheckBoxes are checked and vice versa. if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) { chkHeader.attr("checked", "checked"); } else { chkHeader.removeAttr("checked"); } }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="cmbResources" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cmbResources_SelectedIndexChanged" Width="275px"> </asp:DropDownList> <br /> <br /> <asp:DataGrid ID="gridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" BorderColor="Black" BorderStyle="Groove" ForeColor="#333333" Width="500px"> <Columns> <asp:TemplateColumn> <ItemTemplate> <%= ++indexNum %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="English Word"> <ItemTemplate> <%# DataBinder.Eval(Container,"DataItem.Key") %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Translated Word"> <ItemTemplate> <%--<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.Value") %>' BorderStyle="Groove" disabled="disabled"></asp:TextBox>--%> <asp:TextBox ID="txtTrans" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.Value") %>' Enabled="false" BorderStyle="Groove"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <%--<asp:TemplateColumn> <ItemTemplate> <a href='editresource.aspx?key=<%# DataBinder.Eval(Container,"DataItem.Key") %>&file=<%=cmbResources.SelectedItem.Text %>&id=<%=indexNum - 1 %>'> Edit</a> </ItemTemplate> </asp:TemplateColumn>--%> <asp:TemplateColumn> <HeaderTemplate> <asp:CheckBox ID="chkHeader" runat="server" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" /> </ItemTemplate> </asp:TemplateColumn> </Columns> <FooterStyle Font-Bold="True" ForeColor="White" /> <SelectedItemStyle Font-Bold="True" ForeColor="Navy" /> <PagerStyle ForeColor="#333333" HorizontalAlign="Center" /> <ItemStyle ForeColor="#333333" Font-Size="Small" Font-Names="verdana" /> <HeaderStyle Font-Bold="True" /> </asp:DataGrid> <br /> <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" /> </div> </form> </body> </html> 

 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Globalization; using System.Resources; using System.IO; using System.Xml; public partial class UpdateResource : System.Web.UI.Page { public int indexNum = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //string resourcespath = Request.PhysicalApplicationPath + "App_GlobalResources"; //DirectoryInfo dirInfo = new DirectoryInfo(resourcespath); //foreach (FileInfo filInfo in dirInfo.GetFiles()) //{ // string filename = filInfo.Name; // cmbResources.Items.Add(filename); //} //cmbResources.Items.Insert(0, new ListItem("Select a Resource File")); string[] filePaths = Directory.GetFiles(@"C:\\Users\\D1956\\Desktop\\ResourceEdit", "*.aspx"); foreach (string file in filePaths) { string[] f = file.Split(new char[] { '\\\\' }); cmbResources.Items.Add(f[f.Length - 1]); } } } protected void cmbResources_SelectedIndexChanged(object sender, EventArgs e) { if (cmbResources.SelectedIndex != 0) { string filename = Request.PhysicalApplicationPath + "App_GlobalResources\\\\" + cmbResources.SelectedItem.Text; Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); ResXResourceReader RrX = new ResXResourceReader(stream); IDictionaryEnumerator RrEn = RrX.GetEnumerator(); SortedList slist = new SortedList(); while (RrEn.MoveNext()) { slist.Add(RrEn.Key, RrEn.Value); } RrX.Close(); stream.Dispose(); gridView1.DataSource = slist; gridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { string filename = Request.PhysicalApplicationPath + "App_GlobalResources\\\\" + cmbResources.SelectedItem.Text; string filename1 = filename.Remove(filename.Length - 5); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filename); XmlNodeList nlist = xmlDoc.GetElementsByTagName("data"); for (int i = 0; i < gridView1.Items.Count; i++) { CheckBox chkItem = (CheckBox)gridView1.Items[i].FindControl("chkRow"); if (chkItem.Checked) { XmlNode childnode = nlist.Item(i); XmlNode lastnode = childnode.SelectSingleNode("value"); TextBox txtTran = (TextBox)gridView1.Items[i].FindControl("txtTrans"); lastnode.InnerText = txtTran.Text.ToString(); } } xmlDoc.Save(filename1 + "_1" + ".resx"); } } 

您可能需要編輯!IsPostback部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM