Disable LinkButton on click

Posted on: Tue Jun 30 16:29:42 -0700 2009. Updated on: Tue Jun 30 16:32:27 -0700 2009.
Category: DotNet

Often we want to make sure a user does not click more than once on a particular function. Usually with things that write to the database or activate payment, we want to control this. You can use the following with a LinkButton or a standart asp.net Button.

protected void Page_Load(object sender, EventArgs e)
{
     LinkButtonSave.Attributes.Add("OnClick", "this.disabled=true; this.onclick=function() { return false;}");
}

Note that this needs to be called from the codebehind, usually page_load works

Check if user control in edit mode

Posted on: Wed Jun 03 11:05:57 -0700 2009. Updated on: Wed Jun 03 11:09:23 -0700 2009.
Category: Sitefinity

In Sitefinity, we sometimes want to be able to know if a custom user control we have built is in edit mode, ie when you are in the designer. This can be useful to not execute javascript or not draw certain things that would mess up the usability / cause errors in the designer. It is quite easy, we are basically checking for the querystring cmspagemode.

string mode = Request.Querystring.Get("cmspagemode");

if(mode != null)
{
    // running in SF preview mode
    if(mode == "edit")
    {
        // running in edit mode
    }
}

GridView Row Tooltip hover

Posted on: Tue Apr 21 11:21:55 -0700 2009. Updated on: Tue Apr 21 11:27:31 -0700 2009.
Category: DotNet

This is a neat trick to get a tooltip when a user hovers over a row in a gridview. It is very simple and uses standards.

Simply attach something like this to the GridView's RowDataBound event:

protected void GridViewEligibleProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      // get the row view to be able to access data from the binding object
      DataRowView drv = (DataRowView)e.Row.DataItem;
      // lets me type safe, optional
      ProductsSvc.DSProducts.AgencyEligibleProductsRow thisRow = (ProductsSvc.DSProducts.AgencyEligibleProductsRow)drv.Row;
      // this is the magic to get the tooltip. You could put anything as the 2nd parameter
      e.Row.Attributes.Add("title", (thisRow.IsdescriptionNull() ? "" : thisRow.description));
   }
}

Convert System.Drawing.Color into Hex String

Posted on: Wed Mar 18 18:02:04 -0700 2009. Updated on: Wed Mar 18 18:02:38 -0700 2009.
Category: DotNet

This comes in handy sometimes. Use it to turn a Color object into an HTML compatible Hex string that the browser can understand.

private string ConvertColorToHex(Color color)
		{
			string hexColor = color.R.ToString("X") + color.G.ToString("X") + color.B.ToString("X");
			return hexColor;
		}

Serialize to and from XML

Posted on: Wed Mar 18 17:51:12 -0700 2009. Updated on: Wed Mar 18 17:59:16 -0700 2009.
Category: DotNet

Do you ever have an object lying around that you would like to save off to the database and then be able to retrieve it later as that same object type? Then this might help you. Note that it only works with class objects that are serializable, and these must have a parameterless constructor.

They come in handy to serialize Generic List objects too.

		public static string SerializeToString(object obj)
		{
			string output = null;
			if (obj != null)
			{
				XmlSerializer serializer = new XmlSerializer(obj.GetType());

				using (StringWriter writer = new StringWriter())
				{
					serializer.Serialize(writer, obj);

					output = writer.ToString();
				}
			}
			return output;
		}

And then to serialize back into the object:

		public static T SerializeFromString(string xml)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));

			using (StringReader reader = new StringReader(xml))
			{
				return (T)serializer.Deserialize(reader);
			}
		}

Show Item with javascript

Posted on: Mon Mar 02 12:16:38 -0800 2009. Updated on: Mon Mar 02 12:21:24 -0800 2009.
Category: Asp .Net AJAX / Javascript

function ShowItem(itemId) {
		var item = $get(itemId);
		if (item.style.display == "none" || item.style.display == "") {
			item.style.display = "block";
		}
		else {
			item.style.display = "none";
		}
	}

If you are using masterpages, you will want to do something like this in your CS codebehind:

HyperLinkLogin.NavigateUrl = String.Format("javascript:ShowItem('{0}');", LoginPanel.ClientID);

Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.