Events doesn't fire when using UpdatePanel - c#

What I did is, I have made 2 user controls. One control is inside the other.
NoW one control has a buttton and a data grid and datalist in it. When pressing button I am filling datagrid while datalist visiblity false. This is working fine. But now when I press some link button in datalist data, it should call item_command event but it is not calling.
I have also used a Updatepanel as a wrapper(all controls are inside it).
Please suggest what may be the reason for that.
Thanks

Set the enablePartialRendering to False of the ScriptManager like this
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false">

Related

Multiple web controls to trigger one Update Panel

I have three web controls that has to update a gridview inside an updatepanel.
The markup is like this:
<asp:DropDown></asp:DropDown>
<asp:Button></asp:Button>
<asp:DropDown></asp:DropDown>
<asp:UpdatePanel>
<asp:Gridview></asp:Gridview>
</asp:UpdatePanel>
I cannot put the updatepanel before the first dropdown because I have used the bootstrap datepicker and that does not behave properly if I put the updatepanel in the start.

asp.net panel loses viewstate on postback

I have a web page with an update panel in it.
Inside the update panel I have a panel with a user control like this:
<asp:UpdatePanel ID="updatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlFiles" runat="server" EnableViewState="true">
<files:FilesControl runat="server" ID="filesControl" />
</asp:Panel>
</ContentTemplate>
I have a list of checkboxes that whenever the user checks one of them I want to add another FilesControl to that panel
so I am doing it like this:
FilesControl files = (FilesControl)LoadControl("~/UserControls/FilesControl.ascx");
files.ID = XXX;
pnlFiles.Controls.Add(files);
But on every postback (every checkbox checked) the panel loses the last state and the controls added are wiped out, so the panel actually returns to its initial state every time and I can't ad more and more controls to it.
I enabled view state and it didn't help.
What am I missing out here ?
As requested, my comments as answer:
You need to add the control(s) to the page on every postback in Page_Init or Page_Load(at the latest) with the same ID as before.
The ID is unique, I generate it. I add the controls on the checkbox
checked change event chkCompare_CheckedChanged
While it is perfectly fine to add a control dynamically in an event, it is necessary to re-create this control on the subsequent postbacks. That must be done in Page_Load at the latest (better: Page_Init). So you need to store somewhere which controls you have already added to be able to re-create them. Maybe it is sufficient to store the control-count in the ViewState.
So I should store the controls I added in the ViewState object and
then re-add them to the panel on Page_Load ?
No, you shouldn't store the controls in the ViewState but the information that is necessary to re-create them. How do you know what you need to add? Of course you could also use a webdatabound control like Repeater, DataList or GridView where you only need to assign a DataSource and the persisting stuff is done automatically for you.
That is what is done with all controls even if you add them declaratively(on aspx). But those controls are re-created automatically by ASP.NET. All variables are disposed at the end of the page's life-cycle since the communication between the client and the server is stateless and disconnected.

Dynamics controls lost on postback

This old chestnut again.
My page is constructed as follows; I have a dropdownlist which is databound on first load. When the user selects a value from this, a postback is performed which then databinds a repeater control.
The ItemTemplate of this repeater control contains a placeholder control. In code behind in the ItemDataBound event of the repeater, I am adding two controls dynamically to this placeholder, a hiddenfield and a checkbox.
When the user clicks the save button, I then want to iterate over all those dynamically created hiddenfields and checkboxes and determine their values. However when the user clicks the save button, those controls no longer exist as shown in the page trace.
I know this is a lifecycle issue and the articles I've seen on this suggest using Init methods to dynamically create your controls but I can't because of the way my page works, e.g. the repeater control only appears and binds after a value is chosen from the dropdownlist.
What do I need to do to maintain the dynamic controls through the postback caused by clicking on the save button?
The problem is when you hit the save button probabily you dont re-bind the repeater and the controls you have added at run time withint the ItemDataBound event are not longer available(because they don't exist anymore)
Why don't you add those control at design time using the Eval function the set up the value of the hidden field?
You just don't create them dynamically just on the on selection change of the drop-down set visibility true or false for the repeater that will solve your problem.on post back you have to again create those control as they are Dynamically created.

Adding LinkButtons to DataGrid rows in ItemDataBound

I have a fairly standard DataGrid. It contains a few BoundColumns.
I'm overloading one of these columns to contain either text or a LinkButton depending on some characteristics of the bound item.
So.. in the ItemDataBound event for the Grid, I check a few things and add either a Label Control, or LinkButton Control to the proper Cell.
If I click on one of these LinkButtons, a postback occurs, but it does not call the method in the event handler of the button.
I have seen some people say that events will not work if you create controls AFTER the PreInit page event. Is that true? If so, there must be a way I can manually wire these up?
I have tried creating all of the buttons in PreInit and only adding them in ItemDataBound which unsurprisingly did not work.
I have heard others say the buttons and each parent control must have a unique ID. Is that true?
Rather than blindly swing away at this, I'd like to understand exactly why I can't do this.
Thanks
As far as I understand it, if you put a link button inside a DataGrid you need to use the RowCommand, i.e.
<asp:LinkButton ID="btnUpdate" CommandName="Something" CommandArgument='<%# Bind("something") %>' runat="server">Update</asp:LinkButton>
Then you need to bind the OnRowCommand event to a function in your DataGrid. This will pass the Command Argument that was given to link button and the name of the command in an Event Argument. You can use this to re-act appropriately to the event.

Popup with a gridview when a link is clicked

Could someone help me out in getting a popup with a gridview(with 2 columns) inside when a link is clicked. I need some help badly. Can someone please tel me how to start and the procedure. I will be thankful.
2 columns in the gridview include checkbox and Text. This should get populated from database and after modifying that. it should save it in the database!!
I really appreciate all your help!! Thanks!
If you have no time, and you want things to work "magically"; go download Ajax Control Toolkit;
Drop a link in your markup that would open the grid; name it MyLink
Define the two column'ed GridView inside a Panel.
<asp:Panel runat="server" ID="pnlGrid"><asp:GridView ...></asp:Panel>
Drop ModalPopupExtender control from Ajax Control Toolkit in your markup.
Add Panel's id in ModalPopupExtender's markup
<act:ModalPopupExtender ID="MyModalPopupExtender1" runat="server" TargetControlID="MyLink" PopupControlID="pnlGrid" style="display:none" OkControlID="MyOkButton"/>
Now clicking on the MyLink would open the Popup with GridView inside; dont forget to add GridView.EmptyDataText property, so that you'd know when there are no records to show.
Last, but not least, upon your page load, load the data from your database, and assign it your GridView.DataSource
This article is a good start.
Enjoy!
Assuming you are using asp.net webforms, the easiest way to do this is to use the Microsoft Ajax Toolkit. With it you can create your panel control with the gridview and extend it with the modal popup extender. You will then hook up the link's click event to show the modal popup control (and any other databinding you need to do).
Thus when you click on the link, the panel will be displayed with the gridview.
If you are using Web Forms, an option would be to use the Ajax Control Toolkit's ModalPopup control. This control is very easy to add to a web forms page. It will allow you to extend the link with the modal and load the gridview in a panel that the modal popup will then display.

Categories