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

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?

Related

Getting form field names on Submit when form is user control

I am slowly, piece by piece learning what I am doing with ASP.NET
I've created the beginnings of my new web application, but I am often coming up against the issue that ASP.NET renames elements IDs or in the case of form fields, their names.
I have a form which is basically a sales system. It is essentially made up of two User Controls, one is a form for Customer Details (name, address etc) and the second is a form for the customer's purchases, it consists of a number lines dynamically created by Javascript created as you list the items the customer is purchasing.
Both these sections are User Controls because they are to be used for other areas of the system where this data will need to be recalled/re-entered.
When the USer Control is loaded, the field which contains the Customers' Name is renamed "m$mainContent$customerForm$name" I understand where this comes from, "m" is the ID of my Master Page, "mainContent" is the main Content Placeholder and "customerForm" is the name of the User Control.
In fact, in my case, this will always remain the same on all forms, so it is relative easy to overcome... but... suppose it wasn't
I can see there are ways I could deal with this with Javascript, but the form doesn't need an AJAX submit, a normal Post will do fine for this, so when I open up the recieving page I want to call Request.Form("name")% to save the customer's name into the database, but of course I really need Request.Form("m$mainContent$customerForm$name")%
How would I dynamically extract those prefixes from the posting form to ensure that if I rename anything or use it in a different scenario, the problem will not break it?
I am using .NET 2.0, so can't use Static Client.
This is really old but let's see if it's useful for you.
Getting this example I managed to get the value from a given field name without prefix.
I'm sure it is not the best solution, but it was useful for me. Let's say you want to recover the value of a field named "hfIdAviso", that comes in the POST as "ctl00$body$hfIdAviso":
var value = Request.Form.Cast<string>()
.Where(key => key.EndsWith("hfIdAviso"))
.ToDictionary(key => key, key => Request.Form[key])
.Values.FirstOrDefault();
That will return the value for that field.
Hope it helps.
If you include runat="server" in the declaration of your user control in the ASPX page, then you can access this control by just using the ID value of the control.
For example
In the ASPX:
<uc:MyUserControl ID="mycontrol1" runat="server" />
In the code behind (in C# syntax):
object foobar = mycontrol1.SelectedValue;
suppose you have a form inside user control that has a TextBox field with ID="txtfname".You have registered your user control in page1 and you want to POST form data to page2 :
in page1 you have user control:
<My:WebUserControl1 ID="myuc" runat="server" />
in page2 codebehind , you can get the value of user control form field (txtfname) this way :
string firstName = Request.Form["myuc$txtfname"];

ASP.NET Data-bound Dropdownlist that permits manual entry also?

I am not a .net programmer but I can write bits and pieces that I need, however, I have been having trouble working out the best way to achieve something for a website I'm altering.
I basically need to have a drop down list which is bound to a certain SQL call (already in place) but I also need the ability to manually type into the drop down list for items that do not exist. These items do not need to be entered into the SQL (I can handle the additional data separately) but I cannot find a way to type inside a drop down list while it is bound to a datasource.
Some entries in the datasource (SQL table) are used for one thing, but then a text string needs to be entered for optional entries which do not exist within this SQL table.
Does anyone know how I can achieve this or maybe another similar control can be used that offers this kind of functionality?
**Update to be more clear:
I need this at runtime. For instance, I do not need this to be within code I actually need the control to accept user input when someone is on the webpate.
Imagine a shopping list and all the items are within the drop down list then the user wants to add another item to the shopping list so they just type it into the drop down box and submit the page and its value will be used instead of an existing item.
As suggested by David, Telerik ComboBox provides that functionality.
Other ways, to use an ASP.NET and jQuery for implementing that functionality.
Using TextBox permits manual entry and using jQuery UI AutoComplete widget to bind required items.
jQuery Autocomplete and ASP.NET
TextBox AutoComplete with ASP.NET and jQuery UI
3 Different Approaches for Implementing the JQuery Autocomplete with ASP.NET

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.

What control should I use for showing "Featured Products" on my ASP.net Website?

I have various SQL tables and my use case is:
I'll choose 5 Products at random and they'll show in X control (don't know what to use here).
I know I'll have to use SQL and pull the product information using the ProductID as the hook to fish out every other information.
What Control should I use?
---If you want to show random product, then Adrotator is the best choice
---If you are getting product randomly from DB then Repeater or datalist could be the best choice
Depends how you want do display it.
Maybe a Repeater and ObjectDataSource will be useful, depending on your ORM.
Maybe a DataList, maybe a Repeater. explain more about what you want to show.
Two possibilities spring to my half-asleep mind.
the first would be to use a ListView or Repeater and have it themed out appropriately.
The second possibility would be to create a user control that displays the way you want, and then instantiate 5 instances on the web page.
I would make a user control that would take an array of ids as a property. Encapsulated in the user control would be everything you need to do to display your products. This would probably be an array of panels with images and labels.

Resources