Loading Different Gridview Dynamically on each Button Click - c#

I have a scenario in which a screen has 5 buttons, on click of each button a different grid view should be loaded.
eg: btn1 clicked grdv1 should be loaded,
btn2 clicked grdv2 should be loaded.
How can i achieve the above,
Condition is that the load on the page during each postback should be less or min.
I had done it using iFrame, loading separate aspx pages to iFrame on each btn click, but i cant get the control id's within the gridview loaded, as i need to do operations on the loaded grid.
Can some1 help me out with a simplest solution.
Thanks in advance.

If both the gridviews have same columns then on button click bind corresponding data. If the columns are different then On PageLoad method set all gridview's attribute i.e visible = "false" except the first one. In OnBtnClick_ForGvn make the corresponding gridview visible by gvn.visible = true;. Hope it helps.

Related

Must click a button twice to fire an event

I have a data grid that is created dynamically to display information retrieved from a database. The data grid consists of multiple bound columns and two button columns for each row, a view and a delete button.
Clicking either button calls the Page_Load() method with Page.IsPostBack as false.
if (!Page.IsPostBack)
{
this.bindForm(); // Populates drop down lists that the user can select
setAccess(false); // Sets access for the page
// Stuff commented out to avoid confusion
}
else
{
bindDynamicGrids(); // Create the data grid(s) and populate them
}
So when I first click the button the above code gets called with IsPostBack false, and the dynamic grids get bound. But, the button click event does not fire. When I click the button a second time, Page_Load is called with IsPostBack false, and after executing bindDynamicGrids() the button click event is fired. I cannot understand the difference between the first and second click.
I have read a few threads;
ASP.NET C#, need to press a button twice to make something happen
http://forums.asp.net/t/1783694.aspx?ASP+NET+Button+needs+to+be+clicked+twice
To attempt to understand the issue, but I must be missing something. From what I am gathering from the second link, a session variable may be getting set in the click event, which is also being set in Page_Load, and this is all an issue with ordering. If that is the case I am not seeing where it is happening.
When it is not a post back the bindForm() method is called, which populates all drop down lists. The edit click event populates those drop downs with the values from the row, but the click event is always a post back and the form has already been bound.
I have also considered having a script automatically double click one of the buttons anytime the user single clicks, but I have not been able to find an "OnClick" property for the column button. Any help that could be provided would be phenomenal.
The thing is, that the button click event happens after the Page_Load event meaning that the filtering does not get applied on the first postback. It has been updated on the second postback and you see the filtering.
You can try to move the code of your page_load event to OnPreRender so the reload happens after the button click event.
for more information look here: Button needs to be clicked twice
and here

AutoPostBack ONCE on pageload

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.

gridview click causing postback in usercontrol

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
}

User control not retaining property values

I have a very simple user control with 5 radio buttons on it, and 3 properties (ID, RatingSetID, and Rating). On the initial load of the page they are on, the code acquires data and places the data into the 3 properties. However, when the submit button is clicked the page reloads and the 3 properties get reset to 0, they are in the !IsPostBack as well. I know I am missing something simple.
Can anyone help?
The usercontrol might be reloaded when postback if you load this control on the fly
The user control get reload when the postback happens.
You can place it into an UpdatePanel it could solve your problem.

Wrong state in Server Controls with same ID's when dynamically adding UserControl to UpdatePanel

Here is what I am trying to do. I have a page with two link buttons and an updatepanel (the two linkbuttons trigger the updatepanel). I have two usercontrols which have labels with same ID's. When I click the first link button, I add the first usercontrol to the updatepanel and set the label value to datetime.now
when i click the second link button i add the second usercontrol but i see that the value of the label from the first control is set in the label in second user control. if the id's are different there is no problem - but in my case the usercontrols are being developed by different teams and I am integrating them in the way i mentioned - so they may have same ids.
i have browsed all over and tried various suggestions but i cannot get this to work, any help will be greatly appreciated.
Thanks
Job Samuel
Your problem has nothing to do with the UpdatePanel, actually.
Imagine what would happen if you had a 3rd LinkButton, which did nothing but postback. What would happen if you clicked the 1st LinkButton, the UserControl appeared, and then you clicked the 3rd one? What do you expect to see? If you think you will see the UserControl again, you are wrong. Dynamically created controls must be created every request, they don't stick around automatically. ViewState remembers the state of controls on the page, NOT what the controls themselves are in the first place -- thats what the markup in the aspx page does. Dynamically created controls obviously arent in the markup, so they arent automatically recreated.
You need to think of the lifecycle of a control as 'straddling' half way between two requests. It starts half way into one request and ends half way into the next. You need to save which user control is currently displayed in either a hidden field or a Page.ViewState value (not the user control itself mind you, just whatever information you need to figure it out), then reload that control from the page's OnLoad. If you do that -- the sequence will go like this:
(1) Click LinkButton1
(2) UserControl1 dynamically created
(3) Click LinkButton2
(4) Page.OnLoad reloads UserControl1
(5) UserControl1 loads its postback data and viewstate
(6) LinkButton2's click event fires
(7) Remove existing UserControl1 and add UserControl2 dynamically
(8) UserControl2 can have the same ID, since UserControl1 already 'consumed' its state.
I suggest you browse my series of articles on Understanding Dynamic Controls in ASP.NET:
http://weblogs.asp.net/infinitiesloop/
You have to set a different id for the label when you create the user control.

Categories

Resources