How can i create a ModalPopupExtender control dynamically from a server control? - c#

I have a composite server control that does quiet a number of things; one of which is to display a ModalPopup OnClick of a dynamically generated HtmlAnchor control.
I need to create this ModalPopupExtender control dynamically in my server control and trigger it from within.
I have been able to create it and trigger it from a button created at design time but not at runtime. This is as a result of the ID assign to the link is always not found by the ModalPopupExtender control.
I have tried assigning a static ID but no success. Can anyone help?

I figured it out. All i needed to do was recreate the HtmlAnchor control in the overridden CreateChildControls method on postback.
Thanks David for you concern.

Related

ajaxToolkit:AsyncFileUpload - Dynamic controls created in the AsyncFileUpload1_UploadComplete() is not rendering to page

I am using a ajaxToolkit:AsyncFileUpload that allows the user to upload a excel file. In the AsyncFileUpload1_UploadComplete() event, I am reading the excel data and creating excel structure using div and adding to the page. But these dynamically created controls are not being added to the page. I added a button in the updatepanel and checked. On button click event, I created same above controls. In this case, the controls are rendering to the page. The controls are not rendering in case of ajaxToolkit:AsyncFileUpload only. Can anybody please tell me what is the fix for this.
Add Button into UpdatePanel where you want to show new controls and make it invisible with css style.
In the AsyncFileUpload1_UploadComplete server-side method, save your data to Session state.
In the client-side OnClientUploadComplete event handler call __doPostBack function with hidden button's UniqueID first parameter value. Then make all stuff required to render dynamic content in that button's click handler.

Extending GridView with default buttons and update panel

I am trying to extend the ASP GridView control by adding some buttons above it. Such as Insert/Delete/Print etc. I must extend the GridView control because it is used at a lot of places in the project. The buttons and the control must be wrapped in an UpdatePanel so it can do partial updating. I am trying to do something like this:
UpdatePanel up = new UpdatePanel();
ImageButton newButton = new ImageButton();
newButton.ImageUrl = "../Images/new.gif";
up.ContentTemplateContainer.Controls.Add(newButton);
up.ContentTemplateContainer.Controls.Add(this);
Page.Form.Controls.Add(up);
The problem is I don't know where to put it, and I don't even know if this will work. If I put it at OnLoad/OnPreRender I get this:
"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
This error occures on the second add.
If I put it in CreateChildControls Page is not yet created.
Please give me some pointers.
In this situation, I'd probably build my own custom server control that inherits from GridView.
Here's a link to a MSDN article that should get you started: Walkthrough: Developing and Using a Custom Web Server Control
EDIT:
Since you're already creating the custom server control; I think this might be your problem line: Page.Form.Controls.Add(up)
Try using simply Controls.Add(up). What it looks like you're trying to do is add the update panel to the Page itself; when you should really be adding it to the controls collection of your extended GridView

override page PreInit inside Custom server control

I have custom server control contains templatefield when an out button clicked the field disappear , when can I rebind the control (inside its class) supposed that Control.Page.OnInit+=new EventHandler(rebind()) doesn't do any things ?
just rebind the control OnLoad() will solve the problem.

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.

creating dynamic controls using ajax on asp.net

I have button Add and Remove on my page.
Add button adds one checkbox, two textboxes and one dropdownlist to a new line on my page.
Remove button removes them.
I have this running nicely by following Joe Stagner's example.
Problem:
The controls that are created dynamically all need to fire the same event when checked (for checkboxes), also for selected index changes (for dropdownlists).
I have tried to add event handler when I create an object but it doesn't seem to fire?
you need to persist the dynamically created controls in some way [session, viewstate, etc.] for each page load. Recreate the dynamic controls and re-bind the events using delegates on each page load in preInit function.
I think you're probably running into the fact that your page, upon each page post, is being completely recreated - essentially the page has to duplicate what controls were on your page before it can attempt to feed postback (and events) to them. I think what you probably need to do is add code to your page_load which will re-create the dynamically created controls, with the same ids as they had, and register the event handler.
Sounds like you have a page life cycle issue.
For a dynamically created controls to fire events you should create them in the PreInit event of the page.
Here's a link to a cheat sheet for Asp.net page life cycle.
yeah,
like what all said, it is Life cycle issue.
when you load user controls dynamically you should always do the following.
Assign a unique ID for each User Control.
Reload the user controls on Page_Load or Page_Init Events.
and to make it all easier i suggest to abstract the loading to a function that you will call from Page_Load and Page_Init as mentioned before, this function will check if hte target user control was loaded and will load it again for you, to do that, you store the loaded user controls IDs in Session or viewstate.
hope this helps.
If you want to do it without auto post back you can remove the auto post back and throw and ASP button on there. Any runat server should fire off your dynamic event handlers.

Categories