Showing posts with label build. Show all posts
Showing posts with label build. Show all posts

Wednesday, March 28, 2012

Dynamic Control creation not persisting through PostBack

Hey guys,

I've done some searching on dynamic control creation in the forums, but none of them take me to an Atlas page. I'm trying to build a HR Notification form with some simple Atlas UpdatePanels. Basically, I want to dynamically add Labels and TextBoxes to this form. For instance, if you select 3 from the Number of Children DDL, I want 3 child forms to be created under it. I've been able to create the forms (at least theoretically, I just create "test" Labels right now). The problem I'm running into is being able to read from these dynamically created Controls.

When I submit the form as a regular PostBack, all of my created labels seem to be lost. I get null exceptions when I try to use FindControl. Here are some code snippets:

protected void ddlNumChildren_SelectedIndexChanged(object sender, EventArgs e) {int runNum = Convert.ToInt32(ddlNumChildren.SelectedValue.ToString());int count = 0;while (count < runNum) { Label text=new Label(); text.ID ="testLabel" + count; text.Text ="test"; phNumChildren.Controls.Add(text); count++; } }protected void btnSubmit_Click(object sender, EventArgs e) {int runNum = Convert.ToInt32(ddlNumChildren.SelectedValue.ToString());int count = 0;while (count < runNum) { Label lbltest = phNumChildren.FindControl("testLabel" + count)as Label; Response.Write(lbltest.Text); } }

I'm assuming I have to save this data before the postback, but I need a little help in how I can do this. Thanks for your time and any help you can provide.

The problem is that the controls do not exist when the page is recreated during postback. You have two options :
1) Recreate the page during the Init() event if you have atlas UpdatePanels or Page_Load() if not
2) try http://www.denisbauer.com/ASPNETControls.aspx
3) Write your own code to save all controls in ViewState and reload them during the Init() event

Dynamic CollapsiblePanel

I am surely a novice, but was wondering if it is possible to dynamically build a page of collapsible panels based on the values in a SQL table. The code size would be greatly reduced if it can be done.Just another forum that noone uses!

I'll be honest, this is a very vague question. And it seems as though you are fishing for free code. We aren't here to code projects for you. If you are looking for pointers, I'll gladly provide this.

The approach I would take is to use either a SqlDataSource or ObjectDataSource to retrieve the data. I like working with objects, so it tends to take more code up front to get the data from SQL and turn it into objects in my object model. But once done, I then use the ObjectDataSource to get the data ready for binding.

Then you can use any number of databound controls. One example might be the Repeater control. This is a very free form control that will let you provide any HTML per object (or row) returned from the datasource. In the ItemTemplate, you just define the CollapsiblePanelExtender as you would in any databound control.

If you get stuck, please post here with the code that is blocking you and I'll do what I can to help.


Got it working. The problem was with the page sub-class.

Wednesday, March 21, 2012

DropDownExtender question

Can I use the DropDownExtender to create a google suggest textbox? If not, is there another control i can use to do this, or do I have to build my own?

Thanks

sorry...what is google suggest textbox

http://www.google.com/webhp?complete=1&hl=en

The AutoCompleteExtender does what I want but I'm not sure how to send the textbox value to the webservice to sort the dataset. Here's my code...

 <asp:TextBox ID="txtTest" runat=server></asp:TextBox> <asp:AutoCompleteExtender ID=ace1 runat=server TargetControlID="txtTest" ServicePath="WebService.asmx" ServiceMethod="getZoneSuggestions" MinimumPrefixLength="1" />
[ScriptMethod] [WebMethod(EnableSession =true)]public string[] getZoneSuggestions(string test) {string[] result; result =new string[4]; result[0] ="atest1"; result[1] ="atest2"; result[2] ="btest2"; result[3] ="btest2";return result; }
When I try to pass the string into getZoneSuggestions nothing happens, but when I remove "string test" it will show all of my results.
 

I figured it out.

[ScriptMethod] [WebMethod(EnableSession =true)]public string[] getZoneSuggestions(String prefixText,int count) {string[] result; result =new string[4]; result[0] ="atest1"; result[1] ="atest2"; result[2] ="btest2"; result[3] ="btest2";return result; }