Dynamically generated content in a PlaceHolder inside of a FormView? - asp.net

Reaching out for some help here, as this has me stumped. Long story short, I've got a dynamic Table that is built from Functions, displayed in a Placeholder inside a FormView.
I have a 'save' button inside the EditItemTemplate of the FormView, CommandName="Save" - I have a Select Case (using VB here) and (almost) everything is working as expected.
However, when I pull my PlaceHolder in Code Behind, I'm showing no controls in the PlaceHolder.
Here's a brief rundown:
FormView ItemTemplate has a View_PlaceHolder that shows data from dynamically generated table correctly.
FormView EditItemTemplate has an Edit_PlaceHolder that loads the same Data from dynamically generated table into TextBox (works fine, same data is shown as expected)
When I click Save, my current code is:
Dim Edit_PlaceHolder as PlaceHolder = FormView1.FindControl("Edit_FV_PlaceHolder")
Dim EditTable as Table = Edit_PlaceHolder.FindControl("Edit_Plan_Holder")
Edit_PlaceHolder is not nothing, but has 0 controls in Controls.Count
Oh, one more bit of information - my Table is built and added to the PlaceHolder in the FormView.DataBound event.
What am I doing wrong?

You must repopulate and add the dynamically added table on PostBacks too, not just on the initial load. Check out the answer to this question for more detail.

Ok, so here's what was happening:
1) As NoAlias stated, need to keep the ID's. I used Page_Load to set a ViewState variable if I was in 'view' or 'edit' mode.
2) I used a separate BuildViewTable() method and BuildEditTable() method, and inside those called my separate class and constructed the table.
3) It's all working now :) Thanks, NoAlias!

Related

SqlDatasource Update query controlparameter not picking the new value

if you have not encountered this problem and I have to upload my code in order to explain it for you or for you to be able to debug it, then this question is not for you.
I know I can solve this problem by using code-behind, but I don't want to do it. Eventually I will have to do it if nothing works.
I am not using any databound control (gridview,Formview,Detailview etc). Everything is a general form control: textbox and ListBox. I am using their Text and SelectedValue properties to supply values to Updatequery's ControlParameters. Everything should be working as expected. I have played with the ViewState property of the texbox control and the sqldatasource control itself, to no avail. The stored procedure used for the update command is logging the values supplied from the ASP.Net side and amazingly it shows the old values of textbox that were there when the form loads and not the changes I make.
Whats going on here?
Thanks!
Seems like you are missing something in the asp.net life cycle.
Remove any DataBind calls to your sqldatasource on page Load.
If you are using DataBind on Load to populate those text and select controls, a databind will just overwrite any values you entered.
Don't be so shy to show your code, it helps a lot in providing good answers.

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.

find controls from dynamically created table

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.

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/

ASP.NET GridView problem

Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want

Resources