Button Tips and Tricks Part-III

Sheonarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 4192 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article we shall learn how to apply CSS style from the sever side to the asp:Button control, how to fire a server side event on click of the Link and how to specify image as a button and fire server side event.


Here is hundreds of ASP.NET, jQuery, HTML, CSS and JavaScript How to Tips and Tricks, click here to get them all.

How to apply CSS style from the sever side to the asp:Button control?

In case we want to specify CSS style to the button control from the server side, we can use this approach.

ASPX PAGE

<asp:Button ID="Button1" runat="server" Text="Button 1" />

 

CODE BEHIND

 

protected void Page_Load(object sender, EventArgs e)

{

Button1.ForeColor = System.Drawing.Color.CadetBlue;

Button1.BackColor = System.Drawing.Color.White;

Button1.BorderColor = System.Drawing.Color.YellowGreen;

Button1.BorderWidth = Unit.Pixel(15);

Button1.BorderStyle = BorderStyle.Groove;

}

 

 

OUTPUT

In the above code, we have specified ForeColor, BackColor, BorderColor, BorderWidth and BorderStyle to Button1 that gives the effect as displayed in the above picture.

LinkButton

LinkButton is the variant of the asp:Button control except the fact that it appears as a hyperlink on the browser. This control is mainly used when you want to give a hyperlink type of look and feel or you have too many buttons on the page and you are struggling to manage them on the given space.Most of the properties and events works in the same way as it works for the asp:Button control.

How to fire a server side event on click of the Link?

In case we want to execute a server side event on click of a button, we can use this approach.

ASPX PAGE

 

<form id="form1" runat="server">

<div>

<asp:LinkButton ID="lnkButton" runat="server" Text="Submit"

OnClick="SubmitData" />

</div>

</form>

 

 

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{

Response.Write("Server side method executed");

}

 

In the above code snippet, I have kept a asp:LinkButton control with click event, as this is a LinkButton so its UI looks like a hyperlink but it executes the Server side event as the normal asp:Button executes. So we need to declare the server side method “SubmitData” in the same way we had declared in case of asp:Button click event and perform operations.

OUTPUT


ImageButton

ImageButton is a variation of the Button control. Instead of creating its own UI as normal button it makes the specified image as a button. There is slight difference in the way how it behaves in compare with the asp:Button control.

How to specify image as a button and fire server side event?

In case we want to convert an Image as a button so that it works as if a button was clicked, we can use this approach.

ASPX PAGE

 

<asp:ImageButton runat="server" ID="imgDNF" ImageUrl="~/DotNetLogo.gif"

BorderWidth="1" AlternateText="Go to DotNetFunda.com" OnClick="GoToDotNetFunda" />

 

CODE BEHIND

 

 

protected void GoToDotNetFunda(object sender, ImageClickEventArgs e)

{

Response.Redirect("http://www.dotnetfunda.com", true);

}

 

OUTPUT

In the above code snippet, I have an asp:Image button where I have specified on OnClick event to the GoToDotNetFunda server side method. Notice that the event argument in case of ImageButton is ImageClickEventArgs as against Button’s EventArgs. Rest all behaviour and functionalities are same and should work same as Button control.


In the GoToDotNetFunda method, We have used Response.Redirect method to redirect the user to the http://www.dotnetfunda.com website. Notice that the second parameter is true to make sure that the execution of the current page will end as soon as Response.Redirect line executes.

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Naimishforu on: 7/15/2011 | Points: 25
Pretty much nice article!!

Login to post response

Comment using Facebook(Author doesn't get notification)