In my webpage I have two dropdown controls for namely ddlmonth and ddldays ( for month & days)
I write a method for current month in ddlmonth and current day in ddldays and i call that method in page load event it working fien but when i select (in ddlmonth ) different month and different day and insert into database the values inserted in database is current month and current day, actually i select different month and day
This is my code
This is the method for current date display in ddlmonth dropdwon control and ddlday
public void getMonth()
{
ddlmonth.SelectedIndex = DateTime.Now.Month -1;
ddldate.SelectedIndex = DateTime.Now.Day - 1;
}
I call this in page load
protected void Page_Load(object sender, EventArgs e)
{
getMonth();
}
If I call the getMonth method in postback
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
getMonth();
}
}
it not displaying the current month and date
without postback it is working
When I select different month and day and insert into database it is taking current month and day
Please help me
You can try to use Request.Form["ddlmonth"] and Request.Form["ddlday"] to retrieve the values.
Sorry if I misunderstood, but to "call the getMonth method in postback", you need check for ispostback = true, not false.
protected void Page_Load(object sender, EventArgs e)
{
if(isPostback)
{
getMonth();
}
}
I think u should use
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
getMonth();
}
}
The page is loaded and dropdowns showing Current date and month.
After that you choose different date and month and click some button to save that in database.
In the button event you write the insertion code and if required then there again call getMonth() method to load again the current date and month in the dropdowns. The date u chose in the dropdowns will be saved in DB. If required you call getMonth() method to load current date again as below.
protected void btnSave_Click(object sender, EventArgs e)
{
//-------- insertion code ----------------//
getMonth();
}
Related
I have a drop down list with names of countries and a list box with different types of season like summer, winter, etc., and both of them gets updated from a table in my database in the page_load event.
During the Page_Load event, my DropDownList and ListBox get updated with all the available countries and seasons present in the table in the database.
My ListBox changes dynamically after the country is selected in my DropDownList.
For example, if I choose USA, then season would change dynamically like fall, winter, summer. Right now it works perfectly.
The only thing I want is to add an "ALL" option at the top of my ListBox if a country is selected which has all the seasons.
For example, if I choose country "X" in my DropDownList and it has all the weathers present in my database, the ListBox would have an "ALL" option at the top. Basically the "ALL" option would pop up only when all the seasons during the Page_Load event matches the seasons after a country is selected.
What I have tried:
I tried to get the total count of the list box after my page_loaded for the first time like this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int countListBox = lb.Items.Count;
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (lb.Items.Count == countListBox)
{
lb.Items.Insert(0, "All");
}
}
I have an idea but I'm not sure if it's what you're asking exactly.
Try adding your variable outside of the Page_Load method, so it's accessible from all methods.
private int _countListBox;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// make sure the listbox is populated first.
_countListBox = lb.Items.Count;
}
}
Then
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (lb.Items.Count == _countListBox)
{
lb.Items.Insert(0, "All");
}
}
I've a Calendar on my webpage, and during the page_load event I'm setting the webpage to take today's date and load the data for today's date in the Gridview. Paging is allowed in the Gridview.
I also have a Calendar_Selectiondate event and when someone clicks on a date in the calendar, it will show data for that date. The date value is showed in a session variable. In this scenario when I click on the paging hyperlink 2, it will take me to the current day's second page instead of the selected day's second page. I know this is because it's going through the Page_Load event whenever I click on that hyperlink 2 and the date is getting set to Today's date instead of the selected date.
public partial class UKMail_UKMail7Day : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Calendar.SelectedDate = DateTime.Today;
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
//UKMail7DayGridView.DataSourceID = "UKMail7DayAllData";
//UKMail7DayGridView.DataBind();
}
protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
}
}
The events in your Page_Load execute no matter what triggers the postback. If you have code that should ONLY happen the FIRST time a page is loaded, put it within an if(!Page.IsPostback) block.
void Page_Load(object sender, EventArgs e)
{
// code that will execute on every postback, button click, etc.
if(!Page.IsPostback)
{
//code that will only execute the first time the page is loaded.
}
}
Strongly recommended reading: (Every ASP.NET developer should know this.) http://msdn.microsoft.com/en-us/library/ms178472.aspx
Edit using your updated code:
public partial class UKMail_UKMail7Day : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
Calendar.SelectedDate = DateTime.Today;
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
//UKMail7DayGridView.DataSourceID = "UKMail7DayAllData";
//UKMail7DayGridView.DataBind();
}
}
protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
Session["MailDate"] = Calendar.SelectedDate;
UKMail7DayGridView.DataSourceID = "UKMail7DaySelected";
UKMail7DayGridView.DataBind();
}
}
In the page load event, set this
if(Page.IsPostback)
return;
Do this before any of your other code so it wont be executed on postback. I hope I understood you correctly.
I want to customize the calendar control in such a way that calendar starts from current date.
E.g. Today is 29th February then it should show the first date 9th February in the calendar.
Is it possible to do with asp.net calendar control?
By default the <asp:Calendar /> control displays the current month.
This can be overriden using the VisibleDate property if you like.
Could you please clarify what you mean that it should start from todays date? What should happen to the rest of the days of the month?
If you want to grey out or disable the other days in the month, hook into the OnDayRender event which will run when each day is rendered and it gives you access to the date and the cell so you can do something like.
protected void Calendar1_OnDayRender(object sender, DayRenderEventArgs e) {
if(e.Day.Date < DateTime.Now) {
e.Cell.CssClass="disabled";
}
}
The other property you need to know of is the SelectedDate property. Setting this is the equivalent of clicking on that particular date.
You ca set the SelectedDate propery. Assuming your calendar name is MyCal, you can set the first month date in this way:
myCal.SelectedDate = New Date(DateTime.Now.Year, DateTime.Now.Month, 1)
ADDENDUM
Try with this code:
protected void Page_Load(object sender, EventArgs e)
{
MyCal.SelectedDate = DateTime.Now.Date;
}
protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Cell.Visible = false;
}
}
protected void MyCal_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
if (e.NewDate.Month == DateTime.Now.Date.Month - 1)
{
MyCal.SelectedDate = e.PreviousDate;
MyCal.VisibleDate = e.PreviousDate;
}
}
In this way you:
Set the current date
Hide the previous day
Disable previous month browsing
Time for my daily ASP.NET question.
One of my pages shows all of our customer information from a customer table. I want the user to choose whether to see all customer records, or select a specific record from a list. So, my webpage has two radio buttons (show all customers, show specific customer), a listbox (full of customer names), and a formview control. The problem I'm having is getting the formview to update when I change modes via radio buttons or listbox selection (see code below).
Can anyone provide me with some pointers on how to do what I'm trying to do?
protected void Page_Load(object sender, EventArgs e)
{
UpdatePage ();
}
protected void RadioButtonShowAll_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButtonShowSelected_CheckedChanged(object sender, EventArgs e)
{
}
protected void DropDownListCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonShowSelected.Checked = true;
UpdatePage ();
}
protected void UpdatePage ()
{
if (RadioButtonShowAll.Checked)
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] ORDER BY [Company]";
else
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] WHERE ([CustomerID] = #CustomerID) ORDER BY [Company]";
FormView1.DataBind();
}
First, you only have the SelectedIndexChanged event wired up... In that case, what happens when you change the drop down box is first, Page_Load() fires--which calls UpdatePage(). Then, the event fires, which calls UpdatePage() again. The second time probably doesn't do what you expect.
The fix is to only call UpdatePage() from Page_Load() the first time the page is loaded, but not from postbacks:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
UpdatePage();
}
Your page has to be updated on Client side, for it to show the new data. You'll need to use Javascript or AJAX and have some variable that keeps track of the need to refresh your page, this way you can send a request to update the page to the server when the Formview needs updating.
i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}