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.ToStri开发者_高级运维ng();
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());
}
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;
// ...
}
Check if AutoEventWireup is set to true i.e.
AutoEventWireup="true"
I ended up going another route with this request. None of the proposed solutions worked and I never could get it to work. One option we looked at was creating a custom table to build the control. We ended up changing where the information was changed and just used the calendar control as the display mechanism.
Sorry for the delay!
精彩评论