English 中文(简体)
ASP.NET AJAX Toolkit CalendarExtender not working C#
原标题:

I have a folowing problem: I have a page where I need to create a lot of elements dynamically from the code behind. It obviously depends on what is passed from the database, but the number of elements can change, that s why I cannot do it static.

What I have at the moment, is:

I have statically created PANEL:

<asp:Panel ID="pFullInfo_lStartDateStr" runat="server"></asp:Panel>

Then in Code Behind, I m creating other Controls and add them to my Label. The Issue I have is that the CalendarExtender that should appear after clicking the iEditStartDateCalendar doesn t popup :( I can t see what I m doing wrong here ? Any help please ?!?!

// StartDate
Label lStartDateSite = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDateSite);

Label lStartDate = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDate);

ImageButton ibEditStartDate = new ImageButton();
ibEditStartDate.ID = "ibEditStartDate_" + this_site_id;
pFullInfo_lStartDateStr.Controls.Add(ibEditStartDate);

TextBox tbEditStartDate = new TextBox();
pFullInfo_lStartDateStr.Controls.Add(tbEditStartDate);

Image iEditStartDateCalendar = new Image();
iEditStartDateCalendar.ID = "iEditStartDateCalendar";
iEditStartDateCalendar.ImageUrl = "~/i/small/calendar.png";
iEditStartDateCalendar.ImageAlign = ImageAlign.AbsMiddle;
pFullInfo_lStartDateStr.Controls.Add(iEditStartDateCalendar);

CalendarExtender ceEditStartDate = new CalendarExtender();
ceEditStartDate.ID = "ceEditStartDate_" + this_site_id;
ceEditStartDate.PopupButtonID = iEditStartDateCalendar.UniqueID;
ceEditStartDate.TargetControlID = tbEditStartDate.UniqueID;
ceEditStartDate.PopupPosition = CalendarPosition.Right;
pFullInfo_lStartDateStr.Controls.Add(ceEditStartDate);
最佳回答

Normally, when adding the calendar extender in markup, you d just set the PopupButtonID and TargetControlID to the ID of those controls, not the UniqueID.

When adding things like Labels dynamically, you set the AssociatedControlID to the ID of the control, not the UniqueID/ClientID and the framework works it out at render time.

Also, most JavaScript libraries prefer you to use the actual id of the control, rather than the name attribute, so you should use ClientID instead.

问题回答

I found the solution. The problem was with the "UniqueID" I was passing. The correct solution is here:


// StartDate
Label lStartDateSite = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDateSite);

Label lStartDate = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDate);

ImageButton ibEditStartDate = new ImageButton();
ibEditStartDate.ID = "ibEditStartDate_" + this_site_id;
pFullInfo_lStartDateStr.Controls.Add(ibEditStartDate);

TextBox tbEditStartDate = new TextBox();
pFullInfo_lStartDateStr.Controls.Add(tbEditStartDate);
tbEditStartDate.ID = "tbEditStartDate_" + this_site_id;

Image iEditStartDateCalendar = new Image();
iEditStartDateCalendar.ID = "iEditStartDateCalendar";
iEditStartDateCalendar.ImageUrl = "~/i/small/calendar.png";
iEditStartDateCalendar.ImageAlign = ImageAlign.AbsMiddle;
pFullInfo_lStartDateStr.Controls.Add(iEditStartDateCalendar);

CalendarExtender ceEditStartDate = new CalendarExtender();
ceEditStartDate.ID = "ceEditStartDate_" + this_site_id;
ceEditStartDate.PopupButtonID = iEditStartDateCalendar.ID;
ceEditStartDate.TargetControlID = tbEditStartDate.ID;
ceEditStartDate.PopupPosition = CalendarPosition.Right;
pFullInfo_lStartDateStr.Controls.Add(ceEditStartDate);

So what I did basicaly, I assigned an ID s to the TextBox and Image which are been used for the calendar, and that worked :] Many thanks to Stack Overflow :]





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签