Customizing the ASP.NET Calendar Control

So there’s got to be a better way to do this, but given a two hour deadline to get this out the door and working, this was the best I could come up with in VB.NET.

Problem:
Putting links inside the day cells of an asp.net calendar control and having them able to postback so that server side code could act on them. There isn’t a function for just adding to each cell that I saw easily enough so the first step was just to see if I could add text to each cell.

Private Sub calendar_DayRender(ByVal sender As System.Object, ByVal e As DayRenderEventArgs) Handles calendar.DayRender


  e.Cell.Controls.Add(New LiteralControl(“test”))
End Sub

Well that worked well enough, so how about getting a link button into there?

Private Sub calendar_DayRender(ByVal sender As System.Object, ByVal e As DayRenderEventArgs) Handles calendar.DayRender


  LinkButton1 = New LinkButton
  LinkButton1.Text = “test 1”
  e.Cell.Controls.Add(LinkButton1)
End Sub

That seems to work fine, of course those link buttons aren’t doing anything and of course this is where the problem came in. I’ll skip all the random things I tried and just go straight to the working solution. First of all, it would not work making each one of those link buttons it’s own seperate button. Which if you will refer to the previous code block above you will see that I’m just re-initializing a link button that already exists. So basically I have a linkbutton that’s just sitting on the page hidden that I’m going to hi-jack it’s postback and go from there. So here is the code to add all those link buttons to the calendar and set up their postback.

Private Sub calendar_DayRender(ByVal sender As System.Object, ByVal e As DayRenderEventArgs) Handles calendar.DayRender
  LinkButton1 = New LinkButton
  LinkButton1.Text = “test 1”
  LinkButton1.ID = “testtest”
      
  
  LinkButton1.Attributes(“href”) = “javascript:__doPostBack(‘ParentControl$LinkButton1’,’” & e.Day.DayNumberText & ”’)”
  e.Cell.Controls.Add(New LiteralControl(“
”))
  e.Cell.Controls.Add(LinkButton1)
End Sub

A couple things to note, I am just hard coding the parent control’s name in the postback message, this should really be grabbed because you won’t always know how far down you are in the control structure, at least not if you want to re-use it, like I said, I just wanted to know if it could be done. The second argument I am passing would basically be an id, in this case I’m just passing the day the linkbutton falls on so I could print it back out and make sure I’m getting the data I wanted. The adding a break tag was just to make sure the number and the link button wouldn’t run together as you will notice if you don’t add that or something similar. So now on to the code that handles the postback.

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
  Response.Write(“test” & Request(”__EVENTARGUMENT”).ToString())
End Sub

The most important thing to note is the Request(”__EVENTARGUMENT”) portion, in fact that’s the only thing I have in there at the moment, but basically it is what you use to grab that argument you sent to the postback.

Anyways, I hope this helps someone out and if you have a better method of doing this, please please please email me or leave a comment because I am not happy with this solution at all, but as they say, it worked.


No Responses to “Customizing the ASP.NET Calendar Control”  

  1. No Comments

Leave a Reply