Let's say I have an aspx page with this calendar control:
<asp:Calendar ID="Calendar1" runat="server" SelectedDate="" ></asp:Calendar>
Is there anything I can put in for SelectedDate to make it use the current date by default, without having to use the code-behind?
If you are already doing databinding:
<asp:Calendar ID="Calendar1" runat="server" SelectedDate="<%# DateTime.Today %>" />
Will do it. This does require that somewhere you are doing a Page.DataBind() call (or a databind call on a parent control). If you are not doing that and you absolutely do not want any codebehind on the page, then you'll have to create a usercontrol that contains a calendar control and sets its selecteddate.
DateTime.Now will not work, use DateTime.Today instead.
I was trying to make the calendar selects a date by default and highlights it for the user.
However, i tried using all the options above but i only managed to set the calendar's selected date.
protected void Page_Load(object sender, EventArgs e)
Calendar1.SelectedDate = DateTime.Today;
}
the previous code did NOT highlight the selection, although it set the SelectedDate to today.
However, to select and highlight the following code will work properly.
protected void Page_Load(object sender, EventArgs e)
{
DateTime today = DateTime.Today;
Calendar1.TodaysDate = today;
Calendar1.SelectedDate = Calendar1.TodaysDate;
}
check this link: http://msdn.microsoft.com/en-us/library/8k0f6h1h(v=VS.85).aspx
Two ways of doing it.
Late binding
<asp:Calendar ID="planning" runat="server" SelectedDate="<%# DateTime.Now %>"></asp:Calendar>
Code behind way (Page_Load solution)
protected void Page_Load(object sender, EventArgs e)
{
BindCalendar();
}
private void BindCalendar()
{
planning.SelectedDate = DateTime.Today;
}
Altough, I strongly recommend to do it from a BindMyStuff way. Single entry point easier to debug. But since you seems to know your game, you're all set.
I have tried above with above code but not working ,Here is solution to set current date selected in asp.net calendar control
dtpStartDate.SelectedDate = Convert.ToDateTime(DateTime.Now.Date);
dtpStartDate.VisibleDate = Convert.ToDateTime(DateTime.Now.ToString());
I too had the same problem in VWD 2010 and, by chance, I had two controls. One was available in code behind and one wasn't accessible. I thought that the order of statements in the controls was causing the issue. I put 'runat' before 'SelectedDate' and that seemed to fix it. When I put 'runat' after 'SelectedDate' it still worked!
Unfortunately, I now don't know why it didn't work and haven't got the original that didn't work.
These now all work:-
<asp:Calendar ID="calDateFrom" SelectedDate="08/02/2011" SelectionMode="Day" runat="server"></asp:Calendar>
<asp:Calendar runat="server" SelectionMode="Day" SelectedDate="08/15/2011 12:00:00 AM" ID="Calendar1" VisibleDate="08/03/2011 12:00:00 AM"></asp:Calendar>
<asp:Calendar SelectionMode="Day" SelectedDate="08/31/2011 12:00:00 AM" runat="server" ID="calDateTo"></asp:Calendar>
Actually, I cannot get selected date in aspx. Here is the way to set selected date in codes:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DateTime dt = DateTime.Now.AddDays(-1);
Calendar1.VisibleDate = dt;
Calendar1.SelectedDate = dt;
Calendar1.TodaysDate = dt;
...
}
}
In above example, I need to set the default selected date to yesterday. The key point is to set TodayDate. Otherwise, the selected calendar date is always today.
Related
I am not an ASP coder at all. Just trying to helop a friend by adding dynamic copyright date to existing AbleCommerce footer. My current code causes no errors but also has no output:
StoreFooter.ascx
<asp:Literal runat="server" ID="thisYear"></asp:Literal>
StoreFooter.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
AdminLink.Visible = AbleContext.Current.User.IsAdmin;
MobileLinkPanel.Visible = Request.Browser.IsMobileDevice;
MobileStoreLink.NavigateUrl = NavigationHelper.GetMobileStoreUrl("~/Default.aspx?FSIntent=false");
int thisYearInt = LocaleHelper.LocalNow.Year;
thisYear.Text = thisYearInt.ToString();
}
Also tried setting date as DateTime.Now.Year. Can anyone see what I am missing? I also get no output if I assign a string to thisYear.Text.
Instead of Literal use Label control. Try
<asp:Label runat="server" ID="thisYear"></asp:Label>
I have a user control which I add on a page whenever user click on button. Following is the code to add control.
protected void Page_Init(object sender, EventArgs e)
{
if (Session["ControlCount"] != null)
{
for (int i = 1; i <= (int)Session["ControlCount"]; i++)
{
Control myUserControl = LoadControl("~/Controls/MessageControl.ascx");
divMessageControl.Controls.Add(myUserControl);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExpand_Click(object sender, EventArgs e)
{
int count = 0;
if (Session["ControlCount"] != null)
{
count = Convert.ToInt32(Session["ControlCount"]);
}
Control myUserControl = (Control)Page.LoadControl("~/Controls/MessageControl.ascx");
divMessageControl.Controls.Add(myUserControl);
Session["ControlCount"] = count + 1;
}
This control has ModalPopupExtender popup. When I add 2nd control on page it throws an error internally which i can see in firebug. How to make this popup id unique?
<asp:ModalPopupExtender ID="mpeReply" BehaviorID="mpeReply" runat="server" TargetControlID="btnReply"
PopupControlID="pnlReply" BackgroundCssClass="ModalPopupBG1">
</asp:ModalPopupExtender>
Sys.InvalidOperationException: Sys.InvalidOperationException: Two
components with the same id 'mpeReply' can't be added to the
application.
I have found the solution to this problem, as much as a lot of people have said, the simple solution is that your HTML is not properly formed - there is either an extra or missing closing tag to an element. Make sure that all your tags are properly closed and the problem should go away - struggled all day with this one!
I used this code to fix my problem, notice the ScriptMode is set to "Release"
<AjaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release">
</AjaxControlToolkit:ToolkitScriptManager>
I see a similar answer from this link:
http://www.advancesharp.com/questions/17658/sys-invalidoperationexception-two-components-with-the-same-id-xxx-can-t-be-added-to-the-application
Remove BehaviorID property from extender
Similar issue here. The solution for me was to change the Script Manager from a shortcut close tag to the full close tag , after adding the ScriptMode="Release" attribute:
Change:
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server" />
to:
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server></asp:ScriptManager>
We are running following javascript function:
function btn_AddToList_Click() {
var filePath = document.getElementById("FileUpload").value;
if(filePath.length > 0)
{
var opt = new Option(filePath,filePath);
var listBox = document.getElementById("ListBox");
listBox.options[listBox.options.length] = opt;
}
}
Function binding:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btn_AddToList.Attributes.Add("onclick", "btn_AddToList_Click(); return false;");
}
}
HTML:
asp:FileUpload ID="FileUpload" runat="server" Width="394px"
asp:ListBox ID="ListBox" runat="server" Width="394px"
asp:Button ID="btn_AddToList" runat="server" Enabled="true" Text="Add"
Issue is that value of "FileUpload" is not get cleared after we click "Add" button. Any help?
You can not set/clear the value of FileUpload control programmatically. That is a restriction for a security reason. Consider this if this restriction was not there, you could set the value of FileUpload control to some arbitrary file and upload it to your server. You won't be able to achieve this in current shape.
As a work around you can try to bring another textbox exactly on top of textbox part of FileUpload control. This way you will be give the same feeling what you are trying to achieve. But that is also not ideal and may not work properly.
I created an ASPX page with search controls to the left bound as controls for an AccessDataSource.
I want the data grid to be blank on the first calling of the page, but show the results for subsequent page loads.
I plan to achieve this by putting [pFirstRun] = False as my first WHERE condition with the parameter pFirstRun tied to the value isPostBack. How do I achieve this?
Alternatively, is there a better way to achieve this goal?
You can use the OnSelecting event of your datasource and so something like this for your code infront:
<asp:AccessDataSource ID="AccessDataSource1" runat="server" OnSelecting="AccessDataSource1_Selecting"/>
And something like this in your code-behind:
protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
{
e.Cancel = true;
}
}
you could put the code where you do the databind() on the datagrid within a
if (!Page.IsPostback){}
I have the following Page_Load function...
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.Data = GetXmlFromFile(Request.QueryString["file"]);
XmlDataSource1.DataBind();
Repeater1.DataBind();
}
The page in which this Page_Load resides is called by a parent page. Each time this Page_Load gets called the value of "file" in the query string will be different. So each time I will be receiving the XML contents from a different file which are inserted into the XmlDataSource and then bound against by the Repeater. Getting the XML data from the file works great but when I bind against the repeater that only works the first time through Page_Load. Each time after that when this code is executed the same results from the first XML file are displayed in the repeater.
What am I missing here. How can I get the XML data to be bound against the repeater on each page load instead of just the first one?
Tried creating a new XmlDataSource on each load?
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource source = new XmlDataSource();
source.ID = "MyDS";
source.Data = GetXmlFromFile(Request.QueryString["file"]);
source.DataBind();
Repeater1.DataSource = source;
Repeater1.DataBind();
}
I tried to duplicate the problem with a simple implementation that follows your main concept.
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["file"] != null)
{
XmlDataSource XmlDataSource1 = new XmlDataSource();
XmlDataSource1.DataFile = "~/App_Data/" + Request.QueryString["file"];
Repeater1.DataSource = XmlDataSource1;
Repeater1.DataBind();
}
}
Markup:
<asp:Repeater runat="server" ID="Repeater1" DataSourceID="XmlDataSource1">
<ItemTemplate>
<asp:Label runat="server" ID="lbl" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
This works when I browse to "Default.aspx?file=West.xml" where west.xml is an xml file in my App_Data folder. When I enter another existing xml file in the query string, it performs as expected. It's quick n' dirty, but maybe it will give you some idea when comparing it against your existing code.
On further investigation turns out the problem is a caching flag which is set to default true on XmlDataSource's. Other data source types like SqlDataSource do not have this flag enabled.
This is more fully outlined in the following article http://geekswithblogs.net/ranganh/archive/2006/04/28/76638.aspx
Upon disabling the caching feature everything works as expected.