FormView not updating with control events - asp.net

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.

Related

How to update CheckBoxes on the client side after making changes on the server side?

I have a DropDownList and a CheckBox on my web form. After the DropDownList is clicked and this event is posted back to the server. DropDownList_SelectedIndexChanged event is called on the server side. Inside that event handler, I have CheckBox.Checked = true, But I couldn't make the page on the client side to reflect this change (CheckBox.Checked = true). How do I achieve this? Or am I in the wrong direction to use the DropDownList's event handler to update the CheckBox because the page firstly reloads and then DropDownList_SelectedIndexChanged is called?
Page load method:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.Items.Clear();
AddItemsToDropDownList();
}
}
DropDownList selected index changed event handler:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = this.DropDownList1.SelectedItem.Text;
CheckBox checkBox = GetCheckBoxToBeSetByText(selected);
checkBox.Checked = true;
}
OK. Found the issue. Actually there is nothing wrong with the code in my original post. But to make a smallest sample when I posted, I removed some "extra" code. The below is the "complete" code (OK, fine, I still removed some code). As you can see, I put the CheckBox into a static Dictionary. Each time the SelectedIndexChanged event handler is called, it's modifying the CheckBox in that static Dictionary, which means it's modifying the CheckBox object created from the last session? (still not clear here) Looks like each time when a postback message is received, a new set of CheckBox objects are created. Bear with me if this is known to everybody here already because I only have two days of experience on this web development thing up to today.
private static Dictionary<Environment, CheckBox> EnvironmentsCheckBoxes;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EnvironmentsCheckBoxes = new Dictionary<Environment,CheckBox>();
EnvironmentsCheckBoxes.Add(Environment.Dev1, this.Dev1_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Dev2, this.Dev2_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.QA, this.QA_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.QA2, this.QA2_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Demo, this.Demo_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.Prod, this.Prod_CheckBox);
EnvironmentsCheckBoxes.Add(Environment.UAT, this.UAT_CheckBox);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = this.DropDownList1.SelectedItem.Text;
if (selected == "Dev1")
{
EnvironmentsCheckBoxes[Environment.Dev1].Checked = true;
}
else if (selected == "Dev2")
{
...
}
...
}

how to disable a pageload property under certain circumstances

In .NET i have a gridview which has to display on the mainpage with default settings. So i put it in pageload code. In the same page i have a "List" button; people choose date etc. and when they press the button gridview loads data from another stored procedure. I put that code under List button event. But after people press List button and gridview comes with the needed data, if they press something else (because of page refresh itself) gridview turns back to default settings.
How do i keep the gridview with wanted data?
protected void Page_Load(object sender, EventArgs e)
{
opsbelgegridview.DataSource = DB.OpsHavuzGetir();
opsbelgegridview.DataBind();
protected void listelebtn_Click(object sender, EventArgs e)
{
opsbelgegridview.DataSource = DB.OpsHavuzDetayListeleBtn(tarih1.ToString("yyyy-MM-dd"), tarih2.ToString("yyyy-MM-dd"), durumdd.SelectedItem.Text.ToString(), islemtipdd.SelectedItem.Text.ToString());
opsbelgegridview.DataBind();
You can use IsPostBack() and only load the initial grid on the first load of the page. Then if the grid data changes it will stay.
private void Page_Load()
{
if (!IsPostBack)
{
opsbelgegridview.DataSource = DB.OpsHavuzGetir();
opsbelgegridview.DataBind();
}
}

Select all items in listbox when updated via UpdatePanel

protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
string id = ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;
if (id == cboGroup.UniqueID)
{
foreach (ListItem i in lstTest.Items)
i.Selected = true;
}
}
}
This code runs when my cboGroup causes my UpdatePanel to refresh which has the lstTest in it and the data inside of it gets updated, but it does NOT select them all. How can I make it so when my UpdatePanel is finished refreshing all elements of the list box that it refreshed get selected?
[edit] I'm noticing now that at this point what's in the listbox is the previous values and not the new values I would need. So this seems to be before the listbox is filled with data (which is via a SqlDataSource) so it's probably overwriting this.
I was able to put my selection code in the list box's DataBound() event.
protected void lstTest_DataBound(object sender, EventArgs e)
{
SelectAllTest();
}

When events fire, page starts from defaults

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.

How do i postback?

I have this web application which is supposed to move rows up and down on button click, but it does only when I close the webpage and re run my program from Visual Studio.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
DataView view = (DataView)SqlDataSource1.Select(
DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}
}
Is this all I need ?
You can use the CommandName to trigger the functionality of your button clicks.
Example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx
You need to use a button click event. You logic is currently in the page load event, that's why its only firing once
You could also remove the if postback which would also work but is not the recommended solution
protected void Page_Load(object sender, EventArgs e)
{
DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}

Resources