Return To Sender

Straight up, the sender object that gets passed in as an argument in all .Net events kicks ass. If you already aren’t using it for copious amounts of cool stuff, you should be. Sender has so many uses it is truly silly. This article will demonstrate a couple of the common ones for your use.

Before the Code a Note

One of the few functionality items that has over c# is the handles language feature. On the .aspx page you create your linkbutton, etc. and on the code behind you simply select that object from your little drop down at the top of the page, select the event you want and away you go, you get a nicely formatted event handler for the click, or other event, of your control.

C#, on the other hand, requires us to write the handler events ourselves and reference them in our aspx code as well as code behind. Of course, one could throw up design view and, in the properties dialog, click the events button and double click an event to create a nicely formatted event, but who uses design view? I find design view insulting most days and only mildly annoying on other days; the last place I want to go to create my events is design view. Due to this little bugaboo, I write all my handlers myself. The problem with this is that sometimes I don’t know what the correct eventargs object is. My quick tip on this is that, if you don’t know what your eventargs object is, you can just throw in a junk reference, run your code locally and get a nice YSOD stating that your current eventargs object cannot be casted to the correct type of object. So, if anyone was ever wondering when a good time to purposely put an error in code…

//So, if you didn't remember that the eventargs object type for the
//rowdatabound event of a gridview was rowdataboundeventargs, throw down this:
protected void GridView1_RowDatabound(object sender, string e) {


//the error received will let you know the correct eventargs to use.

Less Read, More Code

A common issue one reads about is getting access to a gridviewrow from a linkbutton click inside that row. With sender, SIMPLE!

protected void linkbuttonInRow_Click(object sender, Eventargs e) {
GridViewRow gvr = (GridViewRow)(((linkbutton)sender).parent.parent);

protected void linkbuttonInRow_Click(object sender, Eventargs e) {
linkbutton lb = sender as linkbutton;

//in GridViewRows, going up two levels from controls in a cell will 
//give you a gridviewrow. In a repeater it's one level.
GridViewRow = (GridViewRow)lb.parent.parent;

How about if you have multiple linkbuttons on a page and they all do the same thing but you want to recognize which button the click came from:

protected void linkbuttonInRow_Click(object sender, Eventargs e) {
//I love using "as" in c#, makes me feel like I'm writing VB
Linkbutton lb = sender as linkbutton;

switch(lb.ID) {
case "button1":
response.write("Look I picked button 1 and didn't put a semicolon on this line because I am still digging on the vb")
case "button2:
response.write("I picked button 2 and am now feeling the c# again.");

If you can think of other super creative ways of using sender, let me know.


2 responses to this post.

  1. Posted by QuanOne on June 19, 2010 at 9:36 pm

    My C# program runs on Window’s XP. It communicates with another Linux machine SW over the UDP/IP network. The two programs periodically exchange messages.

    The message exchanges are fine, except for every once a while(maybe every minute, or shorter, or longer), the message-transfer got delayed for about 1 to 4 seconds. I don’t know if it’s my program got delayed on running(by some Window’s programs), or delayed during network transferring? How can I debug this situation? Any suggestions?

    Thanks a lot!


  2. Posted by Camilo on November 11, 2010 at 6:25 am

    //I love using “as” in c#, makes me feel like I’m writing VB
    LOL I’m still trying to get my head around C#’s quirks.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: