Tuesday, September 18, 2012

How to avoid the button events on Refresh of page


I have .aspx page that page inserts the data to the database on a button click. But when i press the button it is going right. i m getting the Successfully message as " successfully inserted data". In this situation if i press "F5" or Refresh the page it is firing the button click event. Why it should be ? How to avoid this condition ?

Solution to the above problem is -

Add this in your class:

#region Browser Refresh
private bool refreshState;
private bool isRefresh;
protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
refreshState = bool.Parse(AllStates[1].ToString());
if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}

protected override object SaveViewState()
{
Session["ISREFRESH"] = refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(refreshState);
return AllStates;
}

#endregion

And in your button click do this:
protected void Button1_Click(object sender, EventArgs e)
{
if (isRefresh == false)
{
Insert Code here

This will do the trick ...

Tuesday, March 20, 2012

LINQ equivalent of foreach for IEnumerable

There is no ForEach extension for IEnumerable as it is only for List. So you could do

This is not working -
IEnumerable items = GetItems();
items.ForEach(i => i.DoStuff());


You can add tolist before doing foreach -

items.ToList().ForEach(i => i.DoStuff());

Monday, February 20, 2012

XML Serialization of List

Here is the code to convert your list into XML -

public XmlDocument GetEntityXml()
{
StringWriter stringWriter = new StringWriter();
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(List< T >));
List< T > parameters = ListtobeConverted();
serializer.Serialize(xmlWriter, parameters);
string xmlResult = stringWriter.ToString();
xmlDoc.LoadXml(xmlResult);

return xmlDoc;
}

Wednesday, February 15, 2012

How to concatenate two Lists objects

Here we are filling the grid values to the List -

var listApp = new List < ApplicationLists > ();
var listFooter = new List< ApplicationLists >();


foreach (GridViewRow row in grdValues.Rows)
{
listApp.Add(new ApplicationLists { Code = ((TextBox)row.Cells[0].FindControl("txtCode")).Text, Value = ((TextBox)row.Cells[0].FindControl("txtValue")).Text });
}
if (((TextBox)grdValues.FooterRow.Cells[0].FindControl("txtCode")).Text != string.Empty)
{
listFooter.Add(new ApplicationLists { Code = ((TextBox)grdValues.FooterRow.Cells[0].FindControl("txtCode")).Text, Value = ((TextBox)grdValues.FooterRow.Cells[0].FindControl("txtValue")).Text });

}

Here is the line that can do the magic -

listApp.AddRange(listFooter);

Wednesday, January 18, 2012

Xml is well formed or not !!

Code to check whether the xml string is well formed or not -

public bool IsValidXml(string xmlContent)
{
var textStream = new StringReader(xmlContent);

using (var xmlTextReader = new XmlTextReader(textStream))
{
try
{
while (xmlTextReader.Read())
{
}
}
catch
{
return false;
}
}
return true;
}