Textbox Date Rendering Truncate Leading 0 part - ajaxToolkit CalendarExtender - asp.net

I have a textbox in a aspx page. The value for the textbox is set in code-behind. When I put a break-point the text value set is “06/03/2013”. But it renders as 6/3/2013 inside the textbox.
txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
//06/03/2013
How can I make it to render as 06/03/2013?
UPDATE
Note: Even if I use CultureInfo.InvariantCulture, this issue is not resolved.
Note: I found that the issue goes off if I remove the ajax calendar control. Is there a way to work it correct with ajax control?
ASP.NET Page
<%# Page Title="Recovery" Language="C#" MasterPageFile="~/SimplifiedMaster.Master"
AutoEventWireup="true" CodeBehind="Recovery.aspx.cs" Inherits="MyCompany.ServicesSupportSite.UI.Recovery" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="headContent" ContentPlaceHolderID="headContentPlaceholder" runat="server">
</asp:Content>
<asp:Content ID="detailContent" ContentPlaceHolderID="detailContentPlaceholder" runat="server">
<asp:ScriptManager ID="scrRecovery" runat="server" />
<div class="recoveryValueDiv">
<asp:TextBox ID="txtDate" runat="server" CssClass="recoveryDate" Width="100px" MaxLength="10"
TabIndex="2"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate"
CssClass="calendarStyle" />
</div>
</asp:Content>
<asp:Content ID="scriptContent" ContentPlaceHolderID="bottomScriptContentPlaceholder"
runat="server">
</asp:Content>

Use InvariantCulture to avoid culture issues and force / as separator:
txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Demo
Note that / has a special meaning: replace me with the current culture's date separator.
Using the InvariantCulture Property
Set the Format property of CalendarExtender:
<ajaxToolkit:CalendarExtender Format="MM/dd/yyyy"
ID="CalendarExtender1" runat="server"
TargetControlID="txtDate"
CssClass="calendarStyle" />
REFERENCES:
Calendar Extender Date Format
ASP.NET AJAX Calendar Extender – Tips and Tricks

Related

Button in UpdatePanel not firing Click event: RequiredFieldValidator the cause

I'm working on a page that consists of Telerik RadToolTip, an UpdatePanel that contains a Button, and a few user controls. There is also a Master Page, which is where the ScriptManager is located.
Previously, I had a RadAjaxManager on the Master Page and RadAjaxProxyManager. I removed them and began to implement standard Update Panels because of complexities within the User Controls.
Now, however, I can't seem to get any of this page to behave as expected. When I place a Button in the UpdatePanel, I expect a Partial Postback and I expect the Button's OnClick event to be fired. However, when I run this code with a breakpoint in the btnPostBack_Click event code, the breakpoint never gets hit.
If I cut and paste the same controls with the same settings into a new project, the breakpoint gets hit. Is there something in the environment of the site or could Telerik be interfering with the Button's Click event? I've removed all RadAjaxManager/Proxies.
<%# Page Title="" Language="C#" MasterPageFile="~/AV.Master" AutoEventWireup="true"
CodeBehind="AssetView.aspx.cs" Inherits="AV_ASP_UI.AssetPages.AssetView" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# Register TagPrefix="uc" TagName="AssetDiagram" Src="~/Controls/AssetDiagram.ascx" %>
<%# Register TagPrefix="uc" TagName="AssetInfo" Src="~/Controls/AssetInfo.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function DoPartialPostBack(command, id) {
document.getElementById("<%= hfldId.ClientID %>").value = id;
document.getElementById("<%= btnPostBack.ClientID %>").click();
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:HiddenField ID="hfldId" runat="server" />
<telerik:RadToolTip ID="RadToolTip1" runat="server" Modal="true" ShowEvent="FromCode"
Position="Center" RelativeTo="BrowserWindow" HideEvent="FromCode" TargetControlID="hfldFireToolTip">
</telerik:RadToolTip>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Button ID="btnPostBack" runat="server" Text="Button" style="visibility: hidden" OnClick="btnPostBack_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="pnlWorkArea" CssClass="pnlWorkArea" runat="server" Height="770px" onMouseDown="hideSVGTtip(event);">
<uc:AssetDiagram ID="ucAssetDiagram" runat="server" CanvasHeight="720" CanvasWidth="276" />
<uc:AssetInfo ID="ucAssetInfo" runat="server"/>
</asp:Panel>
</asp:Content>

CallendarExtender is not a known element

I have two pages with the same code for ´UpdatePanel, ScriptManager and CalendarExtender, in one of them, theCalendarExtender` is working fine, but in the other, it's giving me this error:
CalendarExtender is not a known Element
Here's my code on aspx
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolKit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<div id="conteudo" style="text-align: left">
<fieldset id="fieldset" style="width:730px; margin-left: -200px">
<legend style="text-align:center;"><b>Detalhes do Chamado</b></legend>
<div id="DetalhesChamado">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
//Here is a gridview
<asp:CalendarExtender runat="server"></asp:CalendarExtender>
But still giving the error...
--Update 2--
Now i'm getting this error:
The TargetControlID of 'CalendarExtender1' is not valid. A control with ID 'TxtPrevisao' could not be found.
My New code:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TxtPrevisao" runat="server" Width="115px"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format=" dd ,MMMM ,yyyy" TargetControlID="TxtPrevisao" PopupButtonID="TxtPrevisao" CssClass="MyCalendar">
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
Change the Tag-Prefix of the Ajax-Kit an see if it's working
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
And also:
<act:CalendarExtender runat="server"></act:CalendarExtender>
asp is used for Asp.Net components like the UpdatePanel
------------ Update 2 -------------------
To your new error, you can't assign the PopupButtonID to the textbox, you need a ImageButton
<asp:ImageButton ID="btnCalenderPopup" runat="server" ImageUrl="App_Themes/Standard/Img/calendar.png" CausesValidation="False" />
Still, I insist, you will need to assign the Tag-Prefix for the Took-Kit correctly and you will need to reference the assembly correctly as SanjeevRai said
You have to add reference of 'Ajaxtoolkit' dll into your project. It seems like it is not getting the proper reference of 'AjaxToolkit' DLL.
You can check references of your project by right clicking on website and select 'Property Pages' option. then it will show you all the references. from here you can check whether 'Ajaxtoolkit' reference exit in your project or not.
If Ajaxtoolkit reference does not exist in your project then you will have to add this reference first!
------------ Update 2 -------------------
After looking at your code I got the issue.
Actually you are using calenderextender and its TargetControlID textbox inside a gridview, so it is not getting the id of textbox.It will never identify the child control id directly on aspx page.
you need to attach 'TargetControlID' and 'PopupButtonID' at run time inside RowDataBound event of gridview.
Here is a sample of code that you can use in you aspx.cs file:
void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("TxtPrevisao");
CalendarExtender Calendar1 = (CalendarExtender)e.Row.FindControl("CalendarExtender1");
Calendar1.TargetControlID=txt.ID;
Calendar1.PopupButtonID=txt.ID;
}
}
And in .aspx page attach the rowdatabound event to grid and remove 'TargetControlID' and 'PopupButtonID' attributes from calenderextender i.e.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TxtPrevisao" runat="server" Width="115px"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format=" dd ,MMMM ,yyyy" CssClass="MyCalendar">
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>

Repeater ItemCommand doesn't work when linked to the page

EDIT: I've found what's causing the issue, but I don't know why and I don't know how to fix it. I'm using JQuery Mobile to theme my site and when I remove this line:
<div data-role="page" data-theme="a">
I can get the repeater to work properly. Does anyone know how I can keep my theme and get this repeater to work?
Alright I have a repeater like this:
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" EnableViewState="true">
<ItemTemplate>
<li>
<asp:LinkButton ID="Button1" runat="server" Text='<%# Container.DataItem %>' CommandName="Redirect" />
</li>
</ItemTemplate>
</asp:Repeater>
And the code behind it looks like this
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Redirect")
{
Session["contact"] = ((LinkButton)e.CommandSource).Text;
Response.Redirect("Contact_Details.aspx");
}
}
And if I navigate directly to this page (without being linked to it from another page) the repeater will fire the ItemCommand. But, if I have my login page redirect back to this page or just click on a link to this page from another page, the itemcommand is never triggered when clicking on the link buttons. Any clues as to why this is?
EDIT: The full code for this page is:
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Contacts.aspx.cs" Inherits="WebApplication3.Contacts" %>
<%# MasterType VirtualPath = "~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" EnableViewState="true">
<HeaderTemplate> <ul data-role='listview' data-theme='c' data-inset='true'>
<li data-role="list-divider"><center><h1>Contacts</h1></center></li></HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="Button1" runat="server" Text='<%# Container.DataItem %>' CommandName="Redirect" />
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</asp:Content>
In page load I connect to a webservice that connects to exchange and returns an array out. I do a quick array conversion to an arraylist then the rest of the code is:
//Converts the array grabbed from the webservice to an arraylist
ArrayList testList = ArrayList.Adapter(contactsList);
Repeater1.DataSource = testList;
Repeater1.DataBind();
Repeater1.ItemCommand += new RepeaterCommandEventHandler(Repeater1_ItemCommand);
}
So I figured out the line that was giving me an issue.
In my master page I had:
<div data-role="page" data-theme="a">
This line is for my JQuery Mobile Theme. When I commented it out, it broke the theme but the repeater worked. So I took a look into the JQuery Mobile docs and found that I could put rel="external" as an attribute to the link that links to this page. This removes the AJAX transitions and forces the page to refresh when clicked.
Not sure exactly why this was an issue, any gurus have an idea?

Asp.net web forms control in asp.net mvc

I'm trying to inject an asp.net classic web control to my asp.net mvc application. This control doesn't use view state or post back so it works fine.
But I can't plug a real-time provided value in its attribute. This code
<my:Control runat="server" Tag="<%: Model.ID %>" />
ends up with the tag value just set explicitly to "<%: Model.ID %>".
Is there a way to make it work?
I believe the syntax is as follows:
<my:Control runat="server" Tag="<%# Model.ID %>" />
The other gotcha is you must call .DataBind() on the Control at some point after the Control has been initialized. This probably means taping into the Page_Load or OnInit events of the Page.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script runat="server">
protected override void OnInit(EventArgs e)
{
this.Label1.DataBind();
base.OnInit(e);
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" />
</asp:Content>
Or, if you have access to the source, you could add a call to .DataBind() somewhere before the final Render. You would have to experiment with that.
Hope this helps.
Well, seems like the only way to do this is injecting code the control renders to the page directly. In my case the control renders a silverlight object with some javascript so I put it at the page as is.

Remembering active tab on refresh of ASPX page

I've got an ASPX page set up that loads and displays dynamic data from a local SQLite database. Since the data is being written to the database from a separate C# application, I've set up my ASPX page to refresh every 30 seconds when the database has flagged itself as actively receiving new data.
On my ASPX page, I've got a TabContainer with several different TabPanels that each represent a different view of the data. Now, when my page is being refreshed, the active tab panel is being reset to the one set in my ASPX page as the ActiveTabIndex.
I was wondering if there is an easy way to persist which tab is being remembered.
Thanks!
Edited to add code sample
MyPage.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPageFile.master" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager" runat="server" />
<asp:TabContainer ID="tabContainer" runat="server" ActiveTabIndex="1" CssClass="myTabStyle" Visible="true">
<asp:TabPanel ID="tab1" runat="server" HeaderText="Tab 1">
<ContentTemplate>
<p>Hello</p>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="tab2" runat="server" HeaderText="Tab 2">
<ContentTemplate>
<asp:PlaceHolder ID="tab2placeholder" runat="server" />
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="tab3" runat="server" HeaderText="Tab 3">
<ContentTemplate>
<asp:TabContainer ID="tab3content" runat="server" ActiveTabIndex="0" CssClass="myTabeStyle" />
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="tab4" runat="server" HeaderText="Tab 4">
<ContentTemplate>
<p>Blah.</p>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</asp:Content>
MyPage.aspx.cs
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
...
if (isInProgress)
{
Response.AddHeader("Refresh", "30");
}
LoadTab1();
LoadTab2();
LoadTab3();
LoadTab4();
...
}
}
Try putting UpdatePanels within in the TabPanels. If you post a code sample of your ASPX it would be easier to help.
If you are doing full postbacks all the time then save it in your ASP.NET session. e.g.
Session["ActiveTab"] = index;
Then to retrieve it:
int? activeTab = Session["ActiveTab"] as int?;
Otherwise using jQuery ajax to refresh the data, is in my experience a much better way to go than using UpdatePanels. But it looks like you are already committed to MS Ajax so go with Mike's recommendation to use an ajax timer instead of a refresh header.

Resources