Have a fun while searching

Thursday, July 7, 2011

update resourse file dynamicaly

gt;asp:GridView ID="gvShowRList" runat="server" PageSize="15" AllowPaging="True" AutoGenerateColumns="False"
Width="100%" OnPageIndexChanging="gvShowRList_PageIndexChanging" GridLines="None"
OnRowCommand="gvShowRList_RowCommand" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last"
PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="Bottom" PagerStyle-HorizontalAlign="Center"
CellSpacing="10" CellPadding="0">
gt;PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
gt;Columns>
gt;asp:TemplateField Visible="false">
gt;ItemTemplate>
gt;asp:Label ID="lblIDs" runat="server" Text='gt;%# Eval("ID") %>'>gt;/asp:Label>
gt;/ItemTemplate>
gt;/asp:TemplateField>
gt;asp:BoundField DataField="Name" HeaderStyle-HorizontalAlign="Left" HeaderText="Name"
ItemStyle-CssClass="brown_normal_txt" ItemStyle-Width="60%">
gt;HeaderStyle HorizontalAlign="Left" />
gt;ItemStyle CssClass="brown_normal_txt" Width="60%" />
gt;/asp:BoundField>
gt;asp:BoundField DataField="Page" HeaderStyle-HorizontalAlign="Left" HeaderText="Page"
ItemStyle-CssClass="brown_normal_txt" ItemStyle-Width="20%">
gt;HeaderStyle HorizontalAlign="Left" />
gt;ItemStyle CssClass="brown_normal_txt" Width="20%" />
gt;/asp:BoundField>
gt;asp:CheckBoxField DataField="IsActive" HeaderText="IsActive" HeaderStyle-HorizontalAlign="Left"
ItemStyle-CssClass="brown_normal_txt" ItemStyle-Width="20%">
gt;HeaderStyle HorizontalAlign="Left" />
gt;ItemStyle CssClass="brown_normal_txt" Width="20%" />
gt;/asp:CheckBoxField>
gt;asp:ButtonField ButtonType="Image" CommandName="Updates" HeaderText="Edit" ImageUrl="~/Images/edit.gif"
ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%">
gt;ItemStyle HorizontalAlign="Center" Width="10%" />
gt;/asp:ButtonField>
gt;/Columns>
gt;PagerStyle HorizontalAlign="Center" />
gt;/asp:GridView>
--------------c#.net----------------------
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Window;


public partial class ResourceFile : System.Web.UI.Page
{

#region UserDefined Defined Function

private int CheckExistance()
{
try
{
int result = 0;
Windows win = new Windows();
win.Sites = Convert.ToBoolean(ddlSite.SelectedValue);
win.Pages = txtPage.Text.Trim();
DataSet ds = new DataSet();
ds = win.CheckPage();
if (ds.Tables[0].Rows.Count > 0)
{
AlertBoxes.ShowAlertMessage("Page Already Exist");
result++;
txtPage.Focus();
}
else
{
ScriptManager.GetCurrent(Page).SetFocus(txtName);
}
return result;
}
catch (Exception ex)
{
throw ex;
}
//finally
//{
// win = null;
// ds = null;
//}
}

private void FillGrid()
{
try
{
Windows win = new Windows();
win.Sites = Convert.ToBoolean(ddlSearchSite.SelectedValue);
DataSet ds = new DataSet();
ds = win.GetDateSet();
if (ds.Tables[0].Rows.Count > 0)
{
gvShowRList.DataSource = ds;
gvShowRList.DataBind();
divList.Visible = true;
}
else
{
gvShowRList.DataSource = null;
gvShowRList.DataBind();
divList.Visible = false;
AlertBoxes.ShowAlertMessage("No Record Found.");
}
}
catch (Exception ex)
{
throw ex;
}
//finally
//{
// win = null;
// ds = null;
//}
}

private void reset()
{
//lbltxtPageMessage.Text = "";
//lbltxtPageMessage.Visible = false;
btnSave.Visible = true;
btnUpdate.Visible = false;
//lblmessage.Text = "";
//lblmessage.Visible = false;
txtPage.Text = "";
txtName.Text = "";
chkActive.Checked = false;
hfBFResID.Value = "";
lblmessage.Text = "";
divList.Visible = false;
gvShowRList.DataSource = null;
gvShowRList.DataBind();
Search.Visible = false;
}
#endregion

#region System Funtion

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
if (Convert.ToString(HttpContext.Current.Session["UserDetail"]) != "")
{
}
else
{
Response.Redirect("login.aspx", false);
}
}
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void txtPage_TextChanged(object sender, EventArgs e)
{
try
{
CheckExistance();

}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int i = CheckExistance();
if (i == 0)
{
Windows win = new Windows();
win.Sites = Convert.ToBoolean(ddlSite.SelectedValue);
win.Pages = txtPage.Text.Trim();
win.Names = txtName.Text.Trim();
win.IDS = 0;
win.Actives = chkActive.Checked == true ? true : false;
string[] userid = Convert.ToString(HttpContext.Current.Session["USerDetail"]).Split(',');
string _userid = userid[0];
win.AdminName = _userid;
win.Status = 1;

int result = win.Save_Update();
if (result > 0)
{
AlertBoxes.ShowAlertMessage("Save Sucessfully");
reset();
}
else
{
AlertBoxes.ShowAlertMessage("Please Verify Entry");
}
}
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
//finally
//{
// win = null;
//}
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (hfBFResID.Value != "")
{
Windows win = new Windows();
win.Sites = Convert.ToBoolean(ddlSite.SelectedValue);
win.Pages = txtPage.Text.Trim();
win.Names = txtName.Text.Trim();
win.IDS = Convert.ToInt32(hfBFResID.Value);
win.Actives = chkActive.Checked == true ? true : false;
string[] userid = Convert.ToString(HttpContext.Current.Session["USerDetail"]).Split(',');
string _userid = userid[0];
win.AdminName = _userid;
win.Status = 2;
int result = win.Save_Update();
if (result > 0)
{
hfBFResID.Value = "";
AlertBoxes.ShowAlertMessage("Update Sucessfully");
//lblmessage.Text = "Update Sucessfully";
reset();
Search.Visible = true;
FillGrid();
}
else
{
AlertBoxes.ShowAlertMessage("Page Already Exist");
//lblmessage.Text = "Page Already Exist";
}
}
else
{
AlertBoxes.ShowAlertMessage("Please Verify Entry");
//lblmessage.Text = "Please Verify Entry";
}
}
catch (Exception ex)
{

AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
//finally
//{
// win = null;
//}
}

protected void btnReset_Click(object sender, EventArgs e)
{
try
{
reset();
}
catch (Exception ex)
{

AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void BtnSearch_Click(object sender, EventArgs e)
{
try
{
Search.Visible = true;
}
catch (Exception ex)
{

AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void BtnFind_Click(object sender, EventArgs e)
{
try
{
FillGrid();
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void gvShowRList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
gvShowRList.PageIndex = e.NewPageIndex;
FillGrid();
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

protected void gvShowRList_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
hfBFResID.Value = "";
Label lblid = new Label();
if (e.CommandName == "Updates")
{
lblid = (Label)gvShowRList.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lblIDs");
hfBFResID.Value = lblid.Text;
Windows win = new Windows();
win.IDS = int.Parse(hfBFResID.Value);
DataSet ds = new DataSet();
ds = win.GetDateSetForUpdate();
if (ds != null)
{
if (ds.Tables.Count>0)
{
if (ds.Tables[0].Rows.Count > 0)
{
ddlSite.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["Site"]);
txtPage.Text = Convert.ToString(ds.Tables[0].Rows[0]["Page"]);
txtName.Text = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
chkActive.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == true ? true : false;
btnUpdate.Visible = true;
btnSave.Visible = false;
}
}
}
//lblmessage.Text = "";
//lblmessage.Visible = false;
}
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
//finally
//{
// win = null;
// ds = null;
//}
}

#endregion
}

-----------------C#.logic-------------------
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Reflection;
using System.IO;
using System.Collections;


namespace System.Window
{
public class Resource
{
#region user defined function

public void LoadData(GridView gvResourceEditor,DropDownList ddlFileName, UpdatePanel UpdgvResourceEditor)
{
try
{
string strpath1 = HttpContext.Current.Server.MapPath("Resourcefile" + "\\" + Convert.ToString(ddlFileName.SelectedValue));
XElement resourceElements = XElement.Load(strpath1);
var _datasorce = (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.DataSource = _datasorce;
gvResourceEditor.DataBind();
UpdgvResourceEditor.Update();
}
catch (Exception ex)
{
throw (ex);
}
}

#endregion

#region System Defined Function

public void gvResourceEditor_RowUpdating(GridView gvResourceEditor, GridViewUpdateEventArgs e,
DropDownList ddlFileName, UpdatePanel UpdgvResourceEditor)
{
try
{
string _strPath = HttpContext.Current.Server.MapPath("Resourcefile/" + Convert.ToString(ddlFileName.SelectedValue));
XDocument xDoc = XDocument.Load(_strPath);

Label lblKey = gvResourceEditor.Rows[e.RowIndex].FindControl("lblKey") as Label;
TextBox txtValue = gvResourceEditor.Rows[e.RowIndex].FindControl("txtValue") as TextBox;
string str1 = txtValue.Text;
//string str = txtValue.Text.Replace("\\r\\n", "
");
Hashtable ht = new Hashtable();
ht.Add("str", txtValue.Text.Replace("\\r\\n", "
"));
//str = txtValue.Text.Replace("
", System.Environment.NewLine);
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(Convert.ToString(ht["str"])));
xDoc.Save(_strPath);
}
gvResourceEditor.EditIndex = -1;
LoadData(gvResourceEditor,ddlFileName,UpdgvResourceEditor);
//}
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

public void gvResourceEditor_RowEditing(GridView gvResourceEditor, GridViewEditEventArgs e,
DropDownList ddlFileName, UpdatePanel UpdgvResourceEditor)
{
try
{
gvResourceEditor.EditIndex = e.NewEditIndex;
LoadData(gvResourceEditor, ddlFileName, UpdgvResourceEditor);

}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

public void gvResourceEditor_RowCancelingEdit(GridView gvResourceEditor, GridViewCancelEditEventArgs e,
DropDownList ddlFileName, UpdatePanel UpdgvResourceEditor)
{
try
{
gvResourceEditor.EditIndex = -1;
LoadData(gvResourceEditor, ddlFileName, UpdgvResourceEditor);
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

public void ddlFileName_SelectedIndexChanged(GridView gvResourceEditor,
DropDownList ddlFileName, UpdatePanel UpdgvResourceEditor)
{
try
{
if (Convert.ToString(ddlFileName.SelectedValue) != "Resource File Name")
{
LoadData(gvResourceEditor, ddlFileName, UpdgvResourceEditor);
}
else
{
gvResourceEditor.DataSource = null;
gvResourceEditor.DataBind();
UpdgvResourceEditor.Update();
}
}
catch (Exception ex)
{
AlertBoxes.ShowAlertMessage(ex.Message.ToString());
}
}

#endregion
}
}

0 comments:

Post a Comment