Showing posts with label calendar. Show all posts
Showing posts with label calendar. Show all posts

Wednesday, March 28, 2012

Difficulties using an asp:ImageButton and the PopupControlExtender

I have a textbox and an asp:ImageButton. I would like the Image button to be able to launch the calendar control using the PopupControlExtender and then insert the date into the textbox from selecting it on the calendar. Not sure that I am even close in my example. I am unsure as to what should be in the code behind(C#) as well. Any help would be appreciated.

<

atlas:ScriptManagerID="ScriptManager1"EnablePartialRendering="true"runat="server"></atlas:ScriptManager><asp:TextBoxID="DateTextBox"runat="server"></asp:TextBox><asp:ImageButtonID="ImageButton1"runat="server"ImageUrl="~/Images/Calendar.gif"/><asp:PanelID="Panel1"runat="server"CssClass="popupControl"><atlas:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><atlasToolkit:PopupControlExtenderID="PopupControlExtender1"runat="server"><atlasToolkit:PopupControlPropertiesTargetControlID="ImageButton1"PopupControlID="Panel1"Position="Bottom"/></atlasToolkit:PopupControlExtender><center><asp:CalendarID="Calendar1"runat="server"BackColor="White"BorderColor="#999999"CellPadding="1"DayNameFormat="Shortest"Font-Names="Verdana"Font-Size="8pt"ForeColor="Black"Width="160px"OnSelectionChanged="Calendar1_SelectionChanged"><SelectedDayStyleBackColor="#666666"Font-Bold="True"ForeColor="White"/><TodayDayStyleBackColor="#CCCCCC"ForeColor="Black"/><SelectorStyleBackColor="#CCCCCC"/><WeekendDayStyleBackColor="#FFFFCC"/><OtherMonthDayStyleForeColor="#808080"/><NextPrevStyleVerticalAlign="Bottom"/><DayHeaderStyleBackColor="#CCCCCC"Font-Bold="True"Font-Size="7pt"/><TitleStyleBackColor="#999999"BorderColor="Black"Font-Bold="True"/></asp:Calendar></center></ContentTemplate></atlas:UpdatePanel></asp:Panel>

PopupControl's natural behavior is to add the commit text to its TargetControlID. You want the text to go to a different control, so that's why it's a little more difficult. However, you should be able to get this to work thanks to PopupControl's extensibility. What should work is:

1. Set the CommitProperty property to something handy like "value". This will tell PopupControl to store the commit text in a "value" property on the TargetControlID (an image in your case).

2. Set the CommitScript property to some JavaScript code that reads the commit text from TargetControlID.value and sets it to the intended text box.

That's the general idea - if it doesn't work for you, please follow up here.


Hi David,

I tried the steps you mentioned. Everytime I click on the Imagebutton the calendar displays(flickers) and then disappears, however, when I right click the button the calendar stays open and i can choose a date and it appears in the textbox. What else am I doing wrong?

<

scriptlanguage="javascript"type="text/javascript">function getCalendarValue()

{

document.getElementById('DateTextBox').value = document.getElementById('ImageButton1').value;

} getCalendarValue()

</script>

</head>

<body>

<formid="form1"runat="server"><atlas:ScriptManagerID="ScriptManager1"EnablePartialRendering="true"runat="server"></atlas:ScriptManager><asp:TextBoxID="DateTextBox"runat="server"></asp:TextBox><asp:ImageButtonID="ImageButton1"runat="server"ImageUrl="~/Images/SmallCalendar.gif" />

<asp:PanelID="Panel1"runat="server"CssClass="popupControl"><atlas:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><atlasToolkit:PopupControlExtenderID="PopupControlExtender1"runat="server"><atlasToolkit:PopupControlPropertiesTargetControlID="ImageButton1"PopupControlID="Panel1"Position="Bottom"CommitProperty="value"CommitScript="getCalendarValue()"/></atlasToolkit:PopupControlExtender><center><asp:CalendarID="Calendar1"runat="server"BackColor="White"BorderColor="#999999"CellPadding="1"DayNameFormat="Shortest"Font-Names="Verdana"Font-Size="8pt"ForeColor="Black"Width="160px"OnSelectionChanged="Calendar1_SelectionChanged"><SelectedDayStyleBackColor="#666666"Font-Bold="True"ForeColor="White"/><TodayDayStyleBackColor="#CCCCCC"ForeColor="Black"/><SelectorStyleBackColor="#CCCCCC"/><WeekendDayStyleBackColor="#FFFFCC"/><OtherMonthDayStyleForeColor="#808080"/><NextPrevStyleVerticalAlign="Bottom"/><DayHeaderStyleBackColor="#CCCCCC"Font-Bold="True"Font-Size="7pt"/><TitleStyleBackColor="#999999"BorderColor="Black"Font-Bold="True"/></asp:Calendar></center></ContentTemplate></atlas:UpdatePanel></asp:Panel></form>
You were SO close! You're using an ImageButton which has submit semantics - it's the submit that's causing the flicker and messing stuff up. Change it to an Image instead, and it works great. Here's the code:
<%@. Page Language="C#" %><%@. Register Assembly="AtlasControlToolkit" Namespace="AtlasControlToolkit" TagPrefix="atlasToolkit" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Calendar1_SelectionChanged(object sender, EventArgs e) { PopupControlExtender1.Commit(Image1, Calendar1.SelectedDate.ToString()); }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title>Untitled Page</title> <script language="javascript" type="text/javascript"> function getCalendarValue() { document.getElementById('DateTextBox').value = document.getElementById('Image1').value; }// getCalendarValue() </script></head><body> <form id="form2" runat="server"> <atlas:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"> </atlas:ScriptManager> <asp:TextBox ID="DateTextBox" runat="server"></asp:TextBox> <asp:Image ID="Image1" runat="server" ImageUrl="~/ToggleButton/ToggleButton_Checked.gif" /> <asp:Panel ID="Panel1" runat="server" CssClass="popupControl"> <atlas:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <atlasToolkit:PopupControlExtender ID="PopupControlExtender1" runat="server"> <atlasToolkit:PopupControlProperties TargetControlID="Image1" PopupControlID="Panel1" Position="Bottom" CommitProperty="value" CommitScript="getCalendarValue()" /> </atlasToolkit:PopupControlExtender> <center> <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999" CellPadding="1" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Width="160px" OnSelectionChanged="Calendar1_SelectionChanged"> <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" /> <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" /> <SelectorStyle BackColor="#CCCCCC" /> <WeekendDayStyle BackColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#808080" /> <NextPrevStyle VerticalAlign="Bottom" /> <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" /> <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" /> </asp:Calendar> </center> </ContentTemplate> </atlas:UpdatePanel> </asp:Panel> </form></body></html>

Awesome! I changed it and it worked like a charm. Thanks David! The only other thing I noticed was that when the page loads, it seems like the calendar control shows for a brief second and disappears. How would I get around this?
Look at the .popupControl style used by the samples - it applies "visibility:hidden" to the relevant panel from the very beginning. That way the browser never tries to show it until it's popped.
Thanks for your help!! Genious!!!

Hi;

I followed your code.

Can you let me know why the document.getElementById can't find my contols? I had to look into the source of my page and find out that the name is of my text box isctl00$ContentPlaceHolder1$tbActivationDate instead oftbActivationDate?

the javascript function worked when I did this:

document.getElementById('ctl00$ContentPlaceHolder1$tbActivationDate').value = document.getElementById('ctl00_ContentPlaceHolder1_ImgActivationDate').value


The name of the control may have been ctl00$xxx, but the getElementById works off of the id which would have been ctl00_xxx. If you refer to the control by the '_' version, I this should will work fine.
Thanks, this worked like a charm! One question, though...

I was dynamically creating the javascript, like so:

sb.Append("function GetCalendar1Value(){"); sb.Append("document.getElementById('" + tbBeginDate.UniqueID +"').value = " +"document.getElementById('" + imgCal1.UniqueID +"').value;"); sb.Append("}");
However, this created a full name of "ctl00$ContentPlaceHolder1$imgCal1", which didn't work. I hardcoded it to "ctl00_ContentPlaceHolder1_imgCal1" and it works.

Why doesn't the UniqueID work for the <asp:image> control, or is there a better way of doing it? I guess it works as is, I'm just curious. Thanks!
Hi edmicman,

You may want to check out theClientID property. I think this is more along the lines of what you're looking for...

Thanks,
Ted
Thanks! But doesn't .ClientID only return the ASP.NET ID, i.e. "imgImage1" instead of the full name of the control within a master page, etc. ("ctl00_content... etc.)? It seems like I tried that before and it needed the full name or something. I'll try it again...

Thanks a lot, I was looking for this code and it works fine!


When I try to use this with MasterPages, it doesn't work anymore (yesterday I tried it on a single asp.net page for tests).

I have my JavaScript placed in the <head></head> part of my MasterPage (when I try another JavaScript placed on my MasterPage, it just works fine)

What am I doing wrong?


Hi Hopke,

I'm not sure what's going wrong with this. Perhaps you can post a small sample that still contains the problem so I can try to help.

Thanks,
Ted

Wednesday, March 21, 2012

Default date selection, like a high-lighted box for ajaxToolkit:CalendarExtender

Hi,

Can we do this? Right now when the textbox is clicked, a calendar does pop-up with the sentence at the bottom - Today is - today's date - is there a way to select the day in month, having a blue box around it?

Thanks.

Do you mean have a default date selected in the calendar when the click in the textbox? If so, then Yes. There is aSelectedDate property you can set. You can also change the color to whatever you want using theCssClass="ClassName" and the.ajax__calendar_active property. Seehttp://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx for more info. If it is something else that you are trying to accomplish, let me know.

Bebandit

Default current date in Calendar

When using the Calendar extender, is there a way to put a default current date in the textbox?

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.


I realized that I can assign in code behind but I don't want to do that, that's why I asked because I thought the calendar extender should allow me to do that easily. I can't do this, <%# DateTime.Now.ToShortDateString() %>, since this textbox is bind to a database.


I don't believe the calendar extender can do it automatically, but you could through JavaScript; though, you could also check the textbox and if nothing has been assigned after binding, then make the assignment.


bmains:

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.

Okay, I thought I can try something like this:

<asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("dateCreate", "{0:d}", DateTime.Now.ToShortDateString())%>' Width="80px"></asp:TextBox><br />   

But when compile it gave me this error:

Error 1 A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind. E:\Website\Outlook\admin\admOLissue.aspx 110

Any othe ideas?


Hello,

I'm not sure, but I don't think Bind supports the last parameter. If that is the case, you could create a public method that takes an object as such:

public function FormatDate(objDate As Object) As String
if (objDate is Nothing orelse objDate.Equals(DbNull.Value)) then
return DateTime.Now.ToShortDateString()
else
return CType(objDate, DateTime).ToShortDateString()
end if
end function

And in the textbox:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'


Okay, I got the following error when compiled:

Error 2 The name 'Bind' does not exist in the current context E:\Website\admOLissue.aspx 110

This error comes from this line:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'

By the way, I'm using C#.


Try using Eval instead.


If I use Eval instead of Bind, will it bind to the database if I click on the Update button?


Updates are not based on Bind or Eval statements, but on the value in textbox, not the value passed through Bind or Eval statements... If Bind doesn't work anyway, try to see if Eval will work in that context...


Okay, that seems to fix it.Thanks so much!


Hi there,

I have tried the statement with Eval and didn't work for me. When I click on edit and the field shows the correct date. But once i click on update it doesn't show anything. Is there any other possible way of doing this?

Thanks


I'd be able to help better if you posted the relevant code. Post the control you are doing this with, as well as the parent control that it is in, and how you are updating.


I think the current date ends up in the bottom of the calendar control and you can click on it to take today's date. I know this isn't great, but with a little bit of user training, it can work. I'd prefer to be able to specify today's date to be the default date as well, but since it's not available, this is the best option I could find and not have to rig it up programmatically.


bmains:

Try using Eval instead.

Not sure why, but it's not working. The value will not get updated in the database. Code in .aspx page:

<asp:TextBox ID="txtModDate" runat="server" Text='<%# defaultDate(Eval("modDate"))%>'></asp:TextBox>

Code behind:

1public string defaultDate(Object crntDate)2 {3string currentDate = Convert.ToDateTime(crntDate).ToShortDateString();4if (!String.IsNullOrEmpty(currentDate))5 {6return DateTime.Now.ToShortDateString();7 }8else9 {10return currentDate;11 }

Here is the popup error:

Cannot insert the value NULL into the column 'modDate', table "myDB.dbo.myTable'; column does not allow nulls. INSERT fails. The statement has been terminated.

Default Calendar view set at decades

Hi,

I'm using the Calendar control for a Date Of Birth field on my form. However I would prefer the calendar to open up on the decades screen so that members will be able to tell easily what they have to do. I have a feeling that if the calendar pops up displaying October 2007 for example, the users won't realise they can click on the title to go up a level. Whereas if they start on 2000-2009 its more easier to understand.

Thanks,

Curt.

Sorry to bump this thread, but does anyone know how to do this?


Hi CurtWRC,

My understanding of your issue is that you want to make the CalendarExtender to shown as "Year Mode" instead of "Day Mode". If I have misunderstood, please feel free to let me know.

Here is the sample which I switch the display mode on its OnClientShown event.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" Format="yyyy-MM-dd"OnClientShown="onCalendarShown"> </ajaxToolkit:CalendarExtender> <script type="text/javascript" language="javascript"> function onCalendarShown(sender,args){ sender._switchMode("years", true); } </script> </form></body></html>
Best regards,
Jonathan

Thatsexactly what I was after. Thanks Jonathan!