views:

30

answers:

1

Hi,

I am attempting to create a calendar which is just a simple UI to display dates and dates to users of our system. I have overridden the Calendar's "DayRender" event to gain access to each cell and then insert a couple of dynamic controls to display specific data. The display of the controls works great. However, I recently wanted to add a LinkButton with command arguments and capture the event to run some other logic and change the UI. I have gotten the LinkButton to display properly and it renders as a simple "" tag with the ID that is assigned. However clicking on the link does nothing and it appears that the normal "href='...javascript action...'" portion of the link is not being generated. I have a feeling this is all due to the fact that I am adding the control at the Day Render stage in the page life cycle. But if that was the case the control probably would not show up at all.

Any ideas as to why the click action is not being added yet the text and everything else are? Code is below.

Thanks for your time

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (Schedule != null)
        {
            var dayReq = from day in Schedule
                         where day.RequiredDate == e.Day.Date
                         where day.RequiredQty != 0
                         select day;

            if (dayReq.FirstOrDefault() != null)
            {
                //Open the Date
                e.Cell.Controls.Add(new LiteralControl("<br /><div class=\"auth-sched-req\">Req Qty: <strong>" + String.Format("{0:#,#.###}", dayReq.FirstOrDefault().RequiredQty) + "</strong><br />Prom Date: "));

                //Create a link button for the promise date
                LinkButton lb = new LinkButton();
                lb.ID = dayReq.FirstOrDefault().ItemId.ToString();
                lb.Text = dayReq.FirstOrDefault().RequiredDate.ToShortDateString();
                lb.CommandName = "ShowPromise";
                lb.CommandArgument = dayReq.FirstOrDefault().ItemId.ToString();
                lb.Command +=new CommandEventHandler(lb_Command);
                e.Cell.Controls.Add(lb);

                //Close the Date
                e.Cell.Controls.Add(new LiteralControl("</div>"));
            }
        }
    }

    protected void lb_Command(Object sender, CommandEventArgs e)
    {
        //Do some magic here
        Response.Write(e.CommandArgument.ToString());
    }
A: 

try this

<script type="text/javascript">
    function calendarClick(day) {
        document.getElementById('<%= hfValue.ClientID %>').value = day;
        document.getElementById('<%= ghostButton.ClientID %>').click();
    }
</script>
<asp:Button ID="ghostButton" runat="server" Style="display: none" OnClick="ghostButton_Click" />
<asp:HiddenField ID="hfValue" runat="server" />

code behind:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    // ...
    e.Cell.Attributes["onclick"] = string.Format("calendarClick({0})", dayReq.FirstOrDefault().ItemId.ToString());
    // ...
}
protected void ghostButton_Click(object sender, EventArgs e1)
{
    string value = hfValue.Value;
    // ...
}
y34h
This seems a lot like a work around. Yes this would probably get me what I want but why is the server not generating the link automatically like it is supposed to? I really would like to understand why what I have (based on MSDN docs) does not work.
Brent Pabst
(i believe) you cant wire up events during render. you have to do it before.
y34h