ASP.NET custom server control not retaining values after post back - asp.net

I have a custom server control (composite control having dynamically created dropdown boxes and textboxes). I have enabled AJAX in order to avoid page reload.The server control is used inside the ASP.NET webcontrol having few buttons which controls the visibility of the server control. Now I enter values in the dropdown box and texboxes and click on any other button. After this postback the last entered values are gone! The control is not remembering the values. Can anyone help me? How can I retain the values after post back?

Ensure that the controls are given the exact same ID after each postback. Also you might want to try initialising your dynamic controls (DropDownList and TextBox) during Page_Init if possible.

If your custom server control is create dynamically all the controls, then you need to recreate them on every post, and you need to create them before the Page_Load(), or else the asp.net cant know them to filled with the post data.
To solve this, if you can not create them before the Page_Load(), then you can fill them by your self with the posted data by using the Request.Form[YourCustomControl.UniqueID] for all controls.

You need to re-create on each postback, see This article

Related

How to load user controls dynamically in ASP.NET without postback

I have a ASP.Net(C#) page. When a user selects a value in dropdownlist, some new controls will be visible to him.
I am able to that now, but the page has to reload every time and state has to be maintained between postbacks.
Is there any way to do the same without reloading of the page in a easiest way possible without using ajax control toolkit?
Additional info: the data for the dropdownlist is coming from the database, and some places it has to be all server side so i can't use javascript .
You can try this idea: http://msdn.microsoft.com/en-us/library/ms178208(v=vs.100).aspx. I have not try it before but it seems logical.
you should go through updatepanel. Put all the controls inside a updatepanel which you want to show on selectedindexchanged event of the dropdownlist and write code behind on selectedindexchanged event of the dropdownlist accordingly. Hope this will help you...

Adding User controls dynamically

I am trying to add user controls once the page is loaded.In the page depending on the selection (dropdown or checkbox) the respective user control will be added.I know that if we need to add user controls I need to add them in the Page_InIt method.but I will know which user control to add only when the page is completely loaded and the user has made a selection.
The page has Update panel so there will be many Async postback's so I also need to look at how to persist the user-control once it is added to the page.
Any help will be grateful.
You can get user selection using the
Request.Form["YourControlID"].
Check this link to know about getting postback data from forms
Read Post Data submitted to ASP.Net Form
Also read this post about using dynamic controls in ASP.NET. It very helpfull.
TRULY UNDERSTANDING DYNAMIC CONTROLS

How to rebind datagrid in asp.net 1.1 without postback?

I need to have datagrid(3 columns) to which the values will be added using 3 textboxes and a button(Add) on the same page. If click on Add button the value should append to datagrid without page refresh/reload?
Please guide me with your approaches to this problem? I ran out of search.
Have you considered using Ajax on the client-side? I presume that clicking the "Add" button is also supposed to update something on the back-end, which means you must do a round-trip of some kind. Either via a postback, but since you don't want this, then the only other option is Ajax whereby you'll make a separate asynchronous post to the back-end that will not cause a page refresh, and on the client script, inject the correct elements to "update" the grid on-screen. You could get that working as a prototype and then use knockout to simplfy the process.
1) I have used iframe in my default aspx page.
2) Created a new aspx page "datagrid.aspx" and had my datagrid as the only control in that page and set the src attribute of iframe to "datagrid.aspx"
3) Datatable to be rendered in datagrid.aspx is stored in the session variable in page load of default.aspx.
4) In the pageload of datagrid.aspx datatable is retrieved and binded to the datagrid.
5) When an item to be added/updated/removed are sent through querystring parameters from default.aspx using onclick event (JS) of a html button by setting the iframe source attribute.
Hope this answer helps.
If its not clear, Please comment.

Dynamic Controls and Postback

I have a Panel. Now in that panel, i'm adding controls. It is getting added and displayed.The problem is when the page is posted back.I know the controls have to be binded again in the panel. But lets say if the user has entered some value in the dynamic created text box. Its is getting lost..
I have done this before by storing the data from the controls in session.
Every time you dynamically add a control, store the current data entered into the controls in session or viewstate for example, and then rebind on postback. Not the most elegant solution but it worked. I take it this is a webforms question?
Add dynamic control click
Save current form data
On page_load load the data from viewstate into the controls
Make sure that the controls get the same ID each time (for ex. by specifying an ID explicitly)
and you should add the dynamic controls on Page.Init so they can participate in the page's life-cycle.
There's an article on 4guysfromrolla.com, Dynamic Web Controls, Postbacks, and View State

How to retrieve values of dynamically created controls in code behind?

I intend to use Javascript to dynamically create a group of html elements (input boxes, select list, radio button, etc.) each time the user clicks "Add More" button.
I have found the Javascript that does that here:
http://viralpatel.net/blogs/2009/01/dynamic-add-textbox-input-button-radio-element-html-javascript.html
http://forums.asp.net/p/1611284/4118564.aspx
So how do I get the values of these dynamically created HTML elements from code-behind? Is there a JQuery way which is better?
Thank you.
I think you'd may be able to do it with a callback/async postback to your ASP.NET page to update the control tree as you update. Not sure how the ViewState is updated/affected by this.
Otherwise, because your messing with the ViewState, so you'll need to revert to accessing them by the Request.Form object.

Resources