I'm trying to make sense of the following select element :
<select name="ctl00$ctl00$ContentPlaceHolder1$ContentItems$ddlResponseRange id="ctl00_ctl00_ContentPlaceHolder1_ContentItems_ddlResponseRange" class="Content" style="width:11%;">
This is all automatically generated code, correct( ie like ct100 ) ? What are the $ symbols doing - are they for expression bindings? FYI the ddl here means "drop-down list".
$ and _ are used as separators between the server-side ID of the control, ddlResponseRange, and the server-side IDs of its parent controls that are of type INamingContainer (typically <asp:content> controls).
A control by itself on a page, right inside a <form runat="server"> does not have attributes like that. But when you start using master pages, or data binding controls, then you are using controls that implement INamingContainer. This interface is like a marker, and it instructs the ASP.NET runtime to start adding the control's server-side ID to that of its child controls. This is necessary to guarantee unique name and ID attributes in the generated HTML.
Ultimately this infrastructure is what supports ASP.NET Web Forms post back mechanism and client-side scripting.
If you are using ASP.NET 4.0 or later, you can actually influence the way client-side IDs are generated by ASP.NET. See this article for different ways to configure the client-side IDs.
It's all just one big string. From the point of view of HTML, they're all valid characters to make up the value of an attribute, so in that sense it's "correct".
As for trying to use the id/name value yourself, or to rely on assumptions about the name/id based on that structure, I'd suggest avoiding that. I find it preferable to pretend that ASP will generate a random name/ID that I can't use, despite the fact that in reality it's just the IDs of all elements from that item up to the root. If you find yourself writing out ID values like that yourself, you're probably doing something wrong.
As for why it follows that convention instead of just actually assigning a random ID/Name (or none at all) is so that it can re-use it to correlate the HTML response to the structure of of items in memory. It's also important to ensure that IDs are unique per page, and by using this structure it means that APS ID values only need to be unique among sibling elements.
Related
Suppose I have a model with some string property.
Imagine also that this string property is actually a comma delimited list of values.
If I want to make a form to update values on my model it would be easy enough to call:
#Html.TextBoxFor(model => model.myCommaDelimitedProp, new { #class = "form-control", placeholder = "CommaDelimitedPropValue" })
However, that is not good enough for the intended application.
I would like to have a custom EditorFor() that could somehow take my property, use string parsing and next generate an array of text boxes to display the pre-existing values.
That would also be relatively trivial.
However, what I cannot seem to solve, mainly because I lack client side experience (js, jquery, angular, ...):
How could I make my editor such that there would be a small button so that I could dynamically add rows, fill them such that, upon form submission, I could string the new values onto the pre-existing string.
So specifically, what would any of you use to achieve this client side behaviour?
I just need some help to be put on the way...
You can achieve this with editor templates. There's a quick intro I threw together on my blog. The only additional thing you'll need is UIHint. Since you won't be able to rely on a specific C# type or DataType annotation to determine that this should be treated as a comma-delimited property. You can just explicitly tell Razor what template it should use. For example:
[UIHint("CommaDelimited")]
public string MyCommaDelimitedProperty { get; set; }
Which would correspond to the editor template: Views\Shared\EditorTemplates\CommaDelimited.cshtml. Once you set up that view how you like it. Then in your form you just call:
#Html.EditorFor(m => m.MyCommaDelimitedProperty)
EDIT
I'll leave my previous answer because it could still be helpful in terms of being able to generate a control for a specific type of thing. You actually may still need to use it to get the right set up on your field to make the JS work properly.
However, when it comes to the client-side handling of this, I figured there had to be something out there already to solve this problem. (Never do more work than you have to.) A cursory search turned up a little script called Tokenfield for Bootstrap. I'm not sure if you're using Bootstrap or not. If not, I also found jQuery Tokeninput and jquery.token-field. I'm sure there's others, as well.
this is the more or less the schema i want to generate my dynamic form on based on the fields above. i am going to add the direction , max size, default value and like wise some more fields in it. i am looking for recommended ways and methods in asp.net mvc for generating dynamic fields at runtime.
1) if i design my own engine for it then how? i am interested on that also but this is the last thing i am looking at. method to apply validation is very important in my scenario
2) any framework that may lessen the working time? or anything else?
I'll describe the generic approach, I don't want to code it for you.
Create meta class to describe each field (type, name, maxlength, null value handling, data source for combos, etc.)
Load the data from database and preprocess it
Populate the ViewBag with sanitized values
Create helper that will generated the control specific code
Html.ControlFor("Name", metadata);
Loop in view over the metadata collection.
which will generate textbox, combobox, etc.
Remeber that MVC form handling works over list of key-values, it's the Binder feature that converts it to objects. Saving data won't be difficult (dynamically created INSERT, UPDATE statement, ...).
So I just learnt about the Triplet class. I have no experience with ASP.NET, only the core .NET Framework.
Can someone explain to me where/why the Triplet class exists? Is it like a Tuple?
Yes, it's pretty much like Tuple from .NET 4.0, but dates back to .NET 1.0 and ASP.NET 1.0 in particular. It's primarily used in ViewState serialization:
The Page class contains a SavePageViewState(), which is invoked during the page life cycle's save view state stage. The SavePageViewState() method starts by creating a Triplet that contains the following three items:
The page's hash code. This hash code is used to ensure that the view state hasn't been #tampered with between postbacks. We'll talk more about view state hashing in the "View State and Security Implications" section.
The collective view state of the Page's control hierarchy.
An ArrayList of controls in the control hierarchy that need to be explicitly invoked by the page class during the raise postback event stage of the life cycle.
There's also its' younger brother called Pair.
There's absolutely no reason you should even bother about these classes or else an unholy untyped mess will ensue.
Sounds like it's got one more object than a Pair. You use it when you need to return exactly three items.
C# and Java are different from Python, which will convert multiple return values into a tuple.
A Triple does sound like a tuple to me - one that has exactly three items.
I'm currently developing a server control which should be configured via a list of key/value pairs, like this:
<MyControls:ContentRenderer ID="ContentRenderer1" runat="server">
<MyControls:Placeholders>
<MyControls:Placeholder key="ident1">Some text which replaces {ident1}</MyControls:Placeholder>
<MyControls:Placeholder key="ident2">Some text which replaces {ident2}</MyControls:Placeholder>
</MyControls:Placeholders>
<MyControls:ContentRenderer />
I want that property to be a dictionary so I can quickly retrieve placeholder mappings by their identifier. I know how to create a property which is persisted with the above markup by using the List< T > class but I'd like to have a hashmap-like datastructure.
I read a lot of stuff at msdn but I still have no clue what to do if you want full control over the way your control markup is parsed.
I don't think dictionaries are supported in this way. However, the way this is done, is instead of using List, use a custom list-based class, which also stores a dictionary mapping of the key/object.
HTH.
Does ASP.NET always apply the "ct100$..." prefixes to element IDs, or in some cases does it optimize this away if the element is guaranteed unique anyways.
Recently I have seen builds differing in the ID prefixes being applied, one having the prefixes and one not but both deriving from the same source.
Can anyone provide any more detail this, and on the workings of INamingContainers and ID generation?
The new ASP.NET 4 ClientIDMode property affects the client-side ID rendered. Setting the naming container control to Predictive is meant to cut down on this... Static takes the exact ID and renders to the client, which you have to be careful to ensure uniqueness.
When you use a naming container (a master page is also a naming container), it appends this longer ID to ensure uniqueness; with .NET 4, they thought a little more about this and added features like Predictive and Static to cut down on the lengths of the IDs.
HTH.