find controls from dynamically created table - asp.net

i wrote a function to create dynamic table in code behind on selectedindexchanged of checkbox,
that is when user will check 2 checkboxex 2 tables will be generated with textboxes,
Then on button click i want insert values of these textboxes in database,
for that i want to find textbox using findcontrol,but i could not find it,
So i called same function of table creation on page load,
but then it shows error that textbox is having duplicate id
Plz tell me solution for this.
thanks

When you create dynamic controls, you need to recreate them on every postback.
The best place to do so is in the OnInit event handler. The problem with doing it in the SelectedIndexChanged event handler of the checkbox is that on postback they will not be re-created and can't be accessed.
See this article for details, and perhaps read up on the asp.net page life cycle too.

Add a div on the page with runat="Server" and give it an ID, div1 for example.
Add the table to this div but you should always write
div1.Controls.Clear();
at the first then add the table again to the div.

Related

Page does not render after SelectedIndexChanged event of dropdownlist in UpdatePanel

I have one dropdownlist (with static listitems). On SelectedIndexChanged event of this dropdownlist, I have three cases to check:
1) If the value is 'A', I need to bind another dropdownlist.
2) If value is 'B', I need to hide above another dropdownlist and instead show nothing in place of that i.e. ulitmately hide that particular div.
3) If values is 'C', I need to hide above div and in place of that, show a textbox.
Now, to prevent Page postback each time a value in dropdownlist is changed, I am using UpdatePanel control here.
I have tried using single updatepanel for both these dropdownlists, two separate udpatePanels for each of them.
With two separate updatePanels, I could succeed in calling the "SelectedIndexChanged" event of that dropdownlist, but once the event is called, the changes done in that code are not reflecting on the page.
i.e. if I hide a div when selected value is 'B', it still shows me, or in either case if it is 'A' and I bind another dropdown, it does not even render.
And, if I reload that page, the pervious changes get reflected. Can anybody please tell me what could be wrong here?
Also, please note that I want be able to postback the page programmatically, because that is the I am trying to prevent.
Also, I would like to notify here that I am using all these things in a WizrdStep of an asp.net wizard control.
Would be greatful for any help.
Thank you in advance.
The issue was not because of update panel or dropdownlist.
The problem was with the way my page was being rendered.
I have used url rewriting for my application and I was trying render this page using my customized form tag instead of built-in Html form tag.
That is why updatepanel was not working normally.
I have got it working now using normal Html Form tag and meanwhile I am trying to workout to handle this updatepanel tag as well with my customized form tag.
If I get it solved, I will submit the solution.
Thanks.

How to create custom pager's dynamic LinkButtons properly?

I'm creating a simple custom pager for repeater (or anything else, it doesn't actually matter because all I want from it is current page number). Pager has 3 public properties: PageSize, MaxResults and PageIndex.
First is set in markup, second is set on the page in Page_Load or EventHandlers when various buttons are clicked and repeater is databinding again. Third is decided by user when clicking on page numbers.
Page numbers are dynamically created LinkButtons with OnCommand set to method that launches my pager's OnPageChanged event to inform webpage that it need to reload data.
The problem is that in order to create correct number of page linkbuttons I need to know MaxResults. I will know it not sooner than in Page_Load and most probably even later. But as far as I know if I want my linkbuttons to fire events they need to be created before Page_Load when I don't know MaxResults...
Now I'm creating linkbuttons in Page_PreRender and event's don't launch. What is the solution for this? It looks like GridView's built in pager is also done with LinkButtons and it also can't know count of data before databinding but the events are firing.

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.

Why do I need to bind GridView on every postback to make click events from columns to work

Ok, i have a gridview and on page load, i dynamically add a column to it that contains a link control. this link control has a onclick event associated with it so that when its clicked, i want to do some processing. Now I have noticed that if I just bind the grid the first time (i.e. if(!IsPostBack) and have enableviewstate for the grid to be true, clicking the link in that column, doesnt trigger the onclick event. But if I bind the grid on every postback, everything seems to be working..does anyone know the reasoning behind it?
It happens because you're dynamically adding the column, so on every postback the column has to be created. What you may want to do is to look at Creating a Custom Column.
May be creating the row Id solves your problem. Check out this link and the post marked as answer.
http://forums.asp.net/p/1471128/3408069.aspx#3408069
Regards
Vinay

ASP.NET GridView - Editing Dynamic Template Columns

I have created a GridView whose columns are dynamically created based on my data source. I have implemented these columns by using the approach described here.Those columns display properly on the initial load. However, I need to implement commanding so that a user can edit / delete a row in the GridView.
At this point, I have implemented commanding as I would with a normal GridView. I have a TemplateField with an ItemTemplate that has LinkButton elements for edit and delete. The CommandName for each LinkButton is set to either Edit or Delete respectively.
Oddly, when a user clicks either the Edit or Delete link, the data in the GridView disappears. However, I have verified that I am in fact re-binding the data when one of these LinkButton elements is selected.
Can anyone provide some suggestions as to what the cause could be?
Thank you!
Here are good examples. You can figure out postback issue.
http://quickstarts.asp.net/QuickStartV20/aspnet/

Resources