Jul 15 2005

Define an Event Handler for a HyperLink Server Control

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 06:55

Hello, a common question on the ASP.NET Forum is:

How to define an Event Handler for a HyperLink?

The solution as follows:

protected System.Web.UI.WebControls.HyperLink HyperLink1;
    
private void Page_Load(object sender, System.EventArgs e)
{
    HyperLink1.Attributes.Add("onClick",
Page.GetPostBackEventReference(this.HyperLink1,"HyperLinkPostBack$"+this.HyperLink1.ID)); if ( (IsPostBack) && (Request.Form["__EVENTTARGET"] != null) && (Request.Form["__EVENTARGUMENT"].ToString().StartsWith("HyperLinkPostBack")) ) { string[] args = Request.Form["__EVENTARGUMENT"].ToString().Split('$'); if ( (string.Compare(args[0], "HyperLinkPostBack", false, System.Globalization.CultureInfo.InvariantCulture) == 0)
&& (args.Length > 1) ) { HyperLinkHandler(args[1].ToString()); } } } public void HyperLinkHandler(string ID) { Response.Write("You clicked HyperLink with ID : "+ ID); }

                
 

                
 
 Or You could use this way, using IPostBackEventHandler:

                
private void Page_Load(object sender, System.EventArgs e)
{
    HyperLink1.Attributes.Add("onClick", Page.GetPostBackEventReference(this, "HyperLinkPostBack" + "$" + this.HyperLink1.ID));
}

public void RaisePostBackEvent(string eventArgument)
{
    if (!string.IsNullOrEmpty(eventArgument))
        {
            string[] args = eventArgument.Split('$');
                if ((string.Compare(args[0], "HyperLinkPostBack", false, System.Globalization.CultureInfo.InvariantCulture) == 0) 
&& (args.Length > 1)) { HyperLinkHandler(args[1].ToString()); } } } public void HyperLinkHandler(string ID) { Response.Write("You clicked HyperLink with ID : " + ID); }

                
 

Hope that helps,

Regards

Tags: ,

Jul 14 2005

Add JavaScript Functionality to GridView

Category: ASP.NET 2.0 - GeneralBil@l @ 19:58

If you want to change the Cursor when the mouse is over the GridView, follow the simple steps below:

protected void RowCreated (object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes.Add("onMouseOver", "this.style.cursor='n-resize'");
}


Hope that helps,

Regards

Tags:

Jul 14 2005

Extend GridView

Category: ASP.NET 2.0 - GeneralBil@l @ 19:43

I want ot point you to two important links on Extending GridView, by Frerik Normen:

1- http://fredrik.nsquared2.com/print.aspx?PostID=186

2- http://fredrik.nsquared2.com/print.aspx?PostID=177

Hope that helps,

Regards

Tags:

Jul 13 2005

TemplateField in GridView

Category: ASP.NET 2.0 - GeneralBil@l @ 23:43

You can call a custom method inside the TemplateField of the GridView as follows:

<TemplateField>
  <ItemTemplate>
      <%# GetSum((int)Eval(“Number1“), (int)Eval(“Number2“)) %>
  </ItemTemplate>
</TemplateField>

public string GetSum(int num1, int num2)
{
     return (num1+num2).ToString();
}

Hope that helps,

Regards

Tags:

Jul 13 2005

DateTime Methods

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 23:01

I had today to work with some DateTime calculations.

I need two methods:

1- Get time difference between two dates, in such a way to have the following formats in the returned value:

- Hours
- Minutes (01 -> 1 minute, 10 -> 10 minutes,...)
- Seconds
- Hours.Minutes
- Hours:Minutes:Seconds

2- Add a double value for a DateTime

- double value (7.02 -> 7 hours and 2 minutes, 7.10/7.1 -> 7 hours and 10 minutes)
- DateTime value

Check the methods below:


// partType :