Posts Tagged ‘sender’

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 vb.net 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!

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

//Simpler
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")
break;
case "button2:
response.write("I picked button 2 and am now feeling the c# again.");
break;
}
}


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