I have a checkbox that needs to update when the user changes the selected item in a GridView. I can programmatically assign the database value to the checkbox on page load. That works fine. But I can't get the checkbox to refresh inside my GridView_SelectedIndexChanged function.
radioBtnDownPmtBrwd.Checked = Convert.ToBoolean(lstBorrowerInfo.rbDwnPmtBrwd);
radioBtnEndorser.Checked = Convert.ToBoolean(lstBorrowerInfo.rbEndorser);
The code above works great on Page_Load. How do I trigger the screen to refresh when I change the checked/unchecked status from the code behind?
You need to rebind the GridView in the event handler code for the selected item changing. The GridView will only show what its DataSource says it has upon the most recent DataBind method call.
Putting this within an UpdatePanel will get rid of the blink refresh that occurs with standard ASP.NET WebForms postbacks, but UpdatePanels are not a panacea.
Related
Here is how my code is set up.
The webpage itself works like this:
You have a drop down that allows you to select between different values. Lets call it dropdown A. Depending on the value selected, a gridview gets generated.
How generation works:
When a item in the dropdown A gets selected, inside the selectedIndexChanged is a method call to a function that creates a DataTable. That datatable gets binded to the gridview inside selectIndexChanged.
When it gets bounded, onRowBoundEvent gets called, and this is where I add all the necessary controls with unique IDs.
There is a button called saved that looks at the data in gridview, and saves it.
Problem: When I press save, there are no controls in the gridview for me to find.
I can use findControl since I know all the ids, but how do I make the controls stick around?
If I bind it in the page_load, how do I know what gridview to generate since if I select a value from dropdown A, page_load still fires before I can get a selection value from dropdown A, so I can't make a simple conditional statement based on the dropdown value.
I can't show any code, sorry. But this is more of a conceptual question I have.
I was able to figure this out on my own.
PrePage_Load during the project lifecycle has access to controls. Dropdown A in my example can be accessed in PrePage_Load, allowing me to get the necessary values and set them before Page_Load starts.
For controls in the gridview, I made it so the gridview does not automatically load from viewstate, and I rebuilt the gridview myself during page_load.
I have a listview with a dropdown, with autopostback enabled.
The user can select yes - which postsback and brings up loads more dropdowns, no - which brings up a textbox, and blank - which does nothing.
As the original dropdownbox is databound it displays one of those values but at the moment always acts as if it was blank ie no other controls showing..
Can I make the page do ONE autopostback as soon as it's loaded without any user input to display the correct controls if it's a yes or no as opposed to blank?
try calling dropdownbox_selectedindexchanged(null,null) after binding in page load. it is just calling selectedindexchanged event programmatically. as far as i know you want to load controls based on dropdown selection when page first load and user haven't changed dropdown value. so this code will call it from code behind.
I have three user controls in one aspx page, where each user control is loading in each separate tabpanel.
Each usercontrol has gridivew and objectdatasource(which has selectmethods).
The gridview is binding data from the objectdatasource, on every postback.
I have some dropdowns, the selected values are passed to the objectdatasource on submit button click, which then populates the results in the gridview.
Now, if I click on any linkbutton on the gridview, the postback is happening and gridview is rebinding with null values, so no click event of linkbutton is happening.
Anybody can suggest me how to stop the postbacks for gridview. I tried placing the updatepanels but didnt help
You can control the flow of the program by checking IsPostback in Page_Load() and filling the grid only when it's supposed to be filled. You can also specify your UI elements to not postback as well, if certain ones are posting back when you don't want them to. Look into the AutoPostBack property.
If (IsPostBack == true) {
// do something, load that
}
Else {
// do something else, don't load that
}
Im working on asp.net with c#.
I have a gridview with templatefield columns, data comes from an sql database. I have linkbutton on the item template, the linkbutton calls the Rowediting event to enable the editing. This works fine on the first row. But when I click on any of the other rows nothing happens, the event never gets fires.
How can I solve this?
Thanks..
Most likely you are data-binding the grid in the Page_Load event. If this is the case, the ASP.NET Page Lifecycle is getting in your way. (Be sure to read the article in the link provided. Every .NET developer needs to know about the Page_Lifecycle. It explains a lot of behavior thaqt would otherwise cause confusion, such as this behavior.)
The Page_Load event happens on every postback - every button click, or any event that triggers the postback.
If this is the case, there are two possible options:
Move your data binding code to Page_Init
Put your data-binding in Page_Load inside an if(!Page.IsPostback) block.
In essence, the problem is that your page is data-binding on the first load.
Then the editing event is triggered by some client action, which triggers a postback. In this postback, Page_Load fires first, which re-binds the GridView, erasing all of the data that was associated with it on the previous load. So when the RowEditing event fires (control events always happen AFTER Page_Load) there's nothing for it to do. All references to the data as it existed before postback are gone.
If you move your binding code too Page_Init, you can get around this because the page will be bound, and then all of the Viewstate will be re-applied to it, restoring the data that was lost in the postback in the scenario above.
Every time I click an ASP button on my page to select an item in my dropdown list, it is only choosing the first item in the list. It is as if every time I click a button the page refreshes and all variables reset. How do you choose an item from a dropdown when using dynamic data from a database?
This sounds indicative of populating the DropDownList in Page_Load. Is that the case? (I'm assuming you're using WebForms.) If you're populating the control in Page_Load then you'll want to wrap it in a conditional:
if (!IsPostBack)
{
// populate your controls from data
}
Otherwise, they'll get re-populated with each postback. When you click a button or perform some other action on the page which initiates a postback, Page_Load is called before the event handler. So in effect, this is happening:
User navigates to the page
Page_Load clears and re-populates the DropDownList
User chooses an item in the DropDownList
User clicks a button
Page_Load clears and re-populates the DropDownList
Button handler gets the current selection from the DropDownList, which is the default.
What do you mean by "click ASP button"?
If you are talking about the downarrow to open the dropdown list, then you need to set AutoPostback options to false if you don't want that opening the dropdown list triggers postback (that is on the other side required if you have any events related to the opening of your list).