Saving data with DynamicField Controls and other ASP.NET controls? - asp.net

I had a bunch of asp.net DynamicFields which took care of saving data for me? I had to switch one of the DynamicFields to a DropDownList and now it does not save the selected value? Do I have to override my save functionality or is there something I can set that will still make it save even thought it is not a DynamicField anymore?

you just need to create a custom Field template to house you dropdown list and then set that in the dynamic field using UIHint

Related

(New to ViewStates) What is the best way(s) to save a dynamically generated control into the viewstate?

I am creating a web application in Asp.net and I'm still fairly new. I'm just starting to wrap my head around the basics of the ViewState. In my application, people are searching through a database and I give them ways to narrow their search. When they have entered a valid search constraint (ex: date past 10/1/11) I dynamically add another set of controls allowing them to add another constraint. I want to save the contents of the previous constraint (a set of Controls) so that I can still have it on the webpage when they enter the next constraint.
If it makes any difference, one constraint set consists of a drop-down list of attributes, a few literal control, and one or two text fields depending on what attribute was chosen from the drop down list.
How would I go about this?
Thanks so much guys.
The easiest way to track viewstate for dynamic controls is to recreate the controls in OnInit and assign the same ID to the controls every time the page is posted back. If the controls are assigned the same ID each time they're created, when the ViewState is loaded, the controls will be repopulated.
protected override void OnInit(EventArgs e)
{
TextBox txt = new TextBox();
txt.ID = "txt1";
this.Controls.Add(txt);
}
EDIT
To make things easier, try using the DynamicControlsPlaceHolder. Just put the control on the page, and it will persist the controls and their values behind the scenes:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
Check out this link:
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
http://www.codeproject.com/KB/viewstate/retainingstate.aspx
ViewState for dynamic controls is still maintained by the ASP.NET framework. Just make sure you add them during init or preinit, because viewstate is loaded for every control between the init and load stages.

Problem With Multiple Data Bindings (Silverlight)

I am using silverlight to create an Intranet for managing chemical usage. All data is stored in a MS SQL database, and is retrieved by using a DomainService (RIA).
By dragging the database onto the form, Silverlight has created a DomainDataSource based on this table. By then creating a new record using the following code, I am now able to add new records.
Me.ChemicalApplicationDomainDataSource.DataView.Add(chemicalApplication)
However one of the controls I have on the screen is an AutoCompleteBox. I have bound this to the database, and the values display fine on here. When an item is selected on here, I want to populate the value of a textbox with values retrieved from a second datatable.
I have created an event on the AutoSelectedItemChanged, and added the following code:
Context.Load(Context.GetChemicalByNameQuery(AutoMaterialTradeName.Text))
I can then bind this to a datagrid.ItemsSource, and it shows the relevent record. But I cannot for the life of me get it to bind to a textbox. Textboxes dont have an ItemsSource, only a DataContext, but binding to this does not seem to display anything..
Any tips on how to achieve this?
You would need to bind it to the Text property on the TextBox for that to work. Additionally you would probably need to set the Path property on the binding to get the right property on your object to display.

ASP.NET Dynamic Data and uploading files

I am developing a small, internal-use only web application. Given its simple nature and intended audience, I decided that it might be a good opportunity to use a ASP.NET Dynamic Data project to get things up-and-running quickly. So far so good, except for one issue that has me reconsidering the whole plan:
I need to be able to upload files through the website. There is an entity in the model that represents an uploaded file. This entity has properties for the file's contents, the file name, and the file's content type. When uploading a file, all of these values are obtained from a single FileUpload control.
Since a FieldTemplate has a one-to-one association with an entity property, I decided that I needed to create a custom EntityTemplate for the File entity. At this point, I have created an "edit" template for the entity that has a FileUpload control. What I have not been able to figure out is, when the user clicks the 'Update' link, how do I get the data from the FileUpload control back into the entity and (ultimately) into the database.
Any advice or guidance is much appreciated.
you can add other values to the dictionary in the ExtractValues method of the FiedlTemplate, what you have to be aware of is that if those values also appear as rows or column in the page template then they will overwrite the values you added. I usually scaffold them as false and then only referance them in the custom field template.
Note you can access their initial values from the Row property in the OnDataBinding event, you can cast the property as the actual type or use an interface added via buddy classes.

How can i create a submitable form that contains dynamically added and removed controls

I am trying to create a form that is made up of controls with values that represent an entity with multiple child entities.
The form will represent a product with multiple properties where the user will then be able to create options with multiple properties which in turn be able to create multiple option-items with multiple properties.
My question is what is the best approach? Can i use ajax to avoid postbacks and having to rewrite the controls to the page? If i dynamically add the controls in the form of table rows or grid rows will the data/control values be available in the code-behind when i submit?
This is an age old question.. the last time i had to do this was .Net 2.0, pre-ajax (for me) and i was forced to recreate all the controls on each post back. thanks!
If I'm understanding your question correctly, you want to dynamically change form elements depending on some kind of selection criteria without refreshing the page? Javascript can do this for you. If your elements rely on data from somewhere, such as options for a select menu, then yes you will have to use an ajax request to populate those. If they are static options, then pure javascript will work for you. And though the source won't indicate your generated elements, they will indeed be available for a submit. I hope this helps.

How to create a custom ASP.NET control with automatic validation

I am building an ASP web site that contains a large section that represents a database front end. I need to build forms to manipulate the database and obviously there would be much repitition i.t.o the type of input field and rules pertaining to the field. For example there are many tables where I have take on varchar(n) fields that could be required or not.
I was thinking to build a set of custom controls that would contain a label naming the field, an edit control (text box for example) and validators for the edit control. The custom control should automatically get the field length and set the MaxLength property as well as determine if the field is a required one and ad a RequiredFieldValidator etc. So when I create the custom control I would do something like this:
<user:UserControls_TextFieldEdit LinqObjectType="Franchise" FieldName="FranchiseName" runat="server" />
There must be a way to achieve this but I haven't I been able to find any controls/libraries that do this. I don't know how to get the field information armed with only strings representing the Linq entity name and field name.
Any help/suggestions would be appreciated.
I am using ASP.NET 3.5 and Linq.
Thanks,
Gerhard
Is Dynamic Linq what you need?

Resources