I am Adding a calendar control to a view. I am using MVC project.
<asp:Calendar ID="Cal" runat="server"
onselectionchanged="C_SelectionChanged"></asp:Calendar>
<br />
Now, where should i declare the C_SelectionChanged method, When the user selects a date from the Calendar this method should get executed. Can someone tell me where and how to add this method ?
You can declare your C_SelectionChanged event on aspx page for this you have to code like this:
<script runat="server">
void C_SelectionChanged(Object sender, EventArgs e)
{
//code here
}
</script>
In a normal ASP.NET website you should set AutoPostBack="true"
webform.apsx
<asp:Calendar ID="Cal" runat="server" AutoPostBack="true"
onselectionchanged="CalSelectionChanged" />
and then add the method that the event will call
webform.aspx.cs
protected void CalSelectionChanged(object sender, EventArgs e)
{
//do some stuff
}
But in MVC it is all another story because you don't have postback, viewstate.
Normal server control are quite useless because they relay on them.
To implement a calendar in MVC try the jQuery UI datepicker.
There you can find a complete code example
Related
ASPX code :
<asp:Button ID="medicalSub" runat="server" ValidationGroup="medical" Text="Save" CausesValidation="false" UseSubmitBehavior="false" ClientIDMode="Static" OnClick="medicalSub_Click" />
ASPX.CS code :
protected void medicalSub_Click(object sender, EventArgs e)
{
console.writeline("hello");
}
Error
According to that stack trace, the problem is not calling the click method. Instead, it's failing trying to load viewstate for a dropdown list control. This is long before trying to handle any events.
Try this:
copy and paste this in your page load section of your code and check if that helps:
If(!IsPostBack)
{
}
Or the following will hep:
<%# Page so just add the rest => EnableEventValidation="false" %>
I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.
Why does my event react differently than a button click event? What do I need to do to get my event working?
This is an out of the box web project in VS2013. Here's the code to explain better:
Code Behind:
namespace WinReceiver
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
}
void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
{
labelVoltage.Text = "Reset";
UpdatePanel1.Update();
}
protected void Button1_Click(object sender, EventArgs e)
{
labelVoltage.Text = "Button Clicked";
UpdatePanel1.Update();
}
}
}
Default.aspx: (The site.master has the ScriptManager in it by default)
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
EDIT:
I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.
If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).
However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).
I have added a calender control to my Webform and on date selection change I have to display the selected date in a label control
<asp:Calendar ID="calendar1" runat="server"
onselectionchanged="calendar1_SelectionChanged" >
</asp:Calendar>
<asp:Label ID="lblInfo" runat="server" Visible="true"
Text="<%#calendar1.SelectedDate.ToShortDateString()%>">
</asp:Label>
But this is not working? Do I need to call any methods in code behind?I dont understand why this is not working.
As noted in the comment section, In your markup add an eventhandler for OnSelectionChanged for the calendar control.
<asp:Calendar ID="calendar1" runat="server"
OnSelectionChanged="calendar1_SelectionChanged" >
</asp:Calendar>
and then in your code behind, handle the event
void calendar1_SelectionChanged(Object sender, EventArgs e)
{
lblInfo.Text= calendar1.SelectedDate.ToShortDateString();
}
As the SelectionChanged event of 'calendar1' is handled by 'calendar1_SelectionChanged',
So, at the code behind, the function should be --->
private void calendar1_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
lblInfo.Text= calendar1.SelectedDate.ToShortDateString();
}
The html markup renders for the first time, and it's done. But, events pertaining to those controls need to be handled in the 'code behind'
i am working with c# asp.net.I am trying to take an input from user and use it in another aspx page.This is my code in first page.
<asp:Button ID="btnProduct1Addtocart" runat="server" Text="Add to cart" postbackurl="~/CartPage.aspx"/>
And this is second page's code which is in codebehind.
Label lblProduct1Name = (Label)PreviousPage.FindControl("lblProduct1Name");
Label1.Text = lblProduct1Name.Text;
But i get an error like "NullReferenceException was unhandled by user code" which is in the "Label1.Text = lblProduct1Name.Text;" line.Thank you for your help.
you need to pass the value using query string like this:
<asp:Button ID="btnProduct1Addtocart" runat="server" Text="Add to cart" onclick="btnAddToCart_Clicked"/>
Code behind event:
private void btnAddToCart_Clicked(object sender, System.EventArgs e)
{
Response.Redirect("~/CartPage.aspx?Name=" +lblProduct1Name.Text);
}
On page load of CartPage.aspx do like this:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = Request.QueryString["Name"];
}
Also have a look at this article to get understanding of it.
the correct way of doing what you are trying to accomplish, is actually doing a POST to the CartPage.aspx page. Wrap your button with a <form action="~/CartPage.aspx" method="POST"> and use a hidden field to hold the product id, for example:
<asp:HidenField ID="productID" runat="server" value="<%= Product.ID %>" />
Other way is to simple pass that variable in the url, like:
Add to cart
in the CartPage.aspx just read the Request["productID"] for the product id that you are passing.
Note: Ease on the ASP.NET Controls, it will get extremely large quickly and will slow your pages, keep as much as HTML as possible.
I want to do page load every 5 sec so I have this in my head content
<meta http-equiv="refresh" content="5" />
Because of this the whole page refreshes. I just want to refresh the gridview and the graph that I have in the page. How can I do that without refreshing the whole page.
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePageMethods="true" EnablePartialRendering="True"
runat="server">
</asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="pie">
<canvas width="125" height="450" id="progress1">[No canvas support]</canvas>
<canvas id="pie1" position="relative" width="400" height="400">[No canvas support]</canvas>
</div>
<asp:Panel runat="server" ID="Panel_Users">
<asp:GridView ID="Grid_UserTable" runat="server"
OnRowDataBound="MyGrid_RowDataBound">
<Columns> </Columns>
</asp:GridView>
</asp:Panel>
<asp:Timer ID="TimerClickEvent" runat="server" Interval="4000" ontick="TimerClickEvent_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
protected DateTime LastUpdate
{
get
{
return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);
}
set
{
ViewState["LastUpdate"] = value;
}
}
protected void TimerClickEvent_Tick(object sender, EventArgs e)
{
if (LastUpdate.AddSeconds(5.0) < DateTime.Now)
{
UpdatePanel1.Update();
LastUpdate = DateTime.Now;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ToolkitScriptManager1.RegisterAsyncPostBackControl(TimerClickEvent);
if (!IsPostBack)
{
LastUpdate = DateTime.Now;
}}
Doesn't reload. where am I going wrong
Place the panel inside an UpdatePanel and use method updatePanelID.Update() to update the update the Panel containing GridView. See this for more details
Instead of button click event you can use a timer for that.
If you only want to refresh a region, there are 2 options:
load that into an <iframe> and let it do whatever it wants (via meta-refresh on the document inside the <iframe>
reload the implacted region via javascript, perhaps jQuery's .load() method, i.e. $('#id').load(url);
A meta-refresh on the entire page will, by necessity, reload the entire page.
For a built in asp.net solution, use an <asp:UpdatePanel> along with an <asp:Timer> control.
Set the update panel to update based on the Timers Tick event. This will then only update the content within the update panel.
HOWEVER, this is a quick and dirty solution (in my opinion) because even though the content on the page only updates within the update panel, the whole page is actually posted back each time, and anything within the Page_Load method will be processed again.
Another solution would be to use some javascript ajax, and only update what you need to update. A great javascript library which will help you with this is jQuery, and then use the AJAX features of this. This will allow you to reload on the areas that you need, and give you far greater control over what is happening.
I would forget about the AJAX Control Toolkit, and look more into jQuery Ajax and jQuery UI.