How does ClientIDMode work? - asp.net

I'm preparing to do an .NET exam and I came across this question. I have been breaking my head over this but I can't figure it out :s I tried all of this but didn't come up with a clear answer...
You are implementing an ASP.NET application that uses data-bound GridView controls in multiple pages. You add JavaScript code to periodically update specific types of data items in these GridView controls. You need to ensure that the JavaScript code can locate the HTML elements created for each row in these GridView controls, without needing to be changed if the controls are moved from one page to another. What should you do?
A. Replace the GridView control with a ListView control.
B. Set the ClientIDMode attribute to Predictable in the web.config file.
C. Set the ClientIDRowSuffix attribute of each unique GridView control to a different value.
D. Set the # OutputCache directives VaryByControl attribute to the ID of the GridView control."
This is what I came up with:
A. Since the structure of the GridView and the ListView is similar and
since they generate the same sort of IDs, I scratched this one.
B. I tested this, but it doesn't seem to affect the IDs :s I think I
would have to apply more settings than just this one to get some
effect.
C. This makes me wonder about what the question really says, this
makes all rows unique. But is that what is supposed to happen, since
you're not supposed to change code?
D. This is for caching, so has nothing to do with the question.
Can someone clear this out for me? Any ideas?

The question tells that ther can be data-bound GridView controls in multiple pages and also, if one gridview control is removed from one page to another page then it should work without any conflict with the already present gridview control.
so for th at we need to set GridView.ClientIDRowSuffix Property. So the answer is C.
Check this MSDN link.

Related

Is there any solution to controls repeated properties?

Is there any solution to controls repeated properties ?
An example would be that imagine you have 4 gridview but each of them have the same templates, same properties, same everything (except id of course). It happened to me several times to have more than one gridview that have the same properties.. so i will ask my question again... Is there any solution to controls repeated properties ? and how to apply your solution.
You could create a UserControl containing the control that you need to repeat and then the constructor for the UserControl to pass in the information that needs to be initialized.
It depends on what you want to do and how your controls are populated. If I understand very well, you have duplicate gridviews. If that is the case then you can create your gridview dynamically in the code behind and populate it there through a function as you can see here.
http://msdn.microsoft.com/en-us/magazine/cc163612.aspx
If not please provide more details and I would be able to help.

ASP.NET dynamic controls data exchange in postback

Please excuse me for a probably low quality of this question, since I'm not a web dev, so I possibly don't now some obvious things and don't know what to Google for. I think problem must have some simple solution, but I'm struggling with it for two days now, so I feel myself pretty stupid :-).
I have a custom control which is a set of checkboxes which are added dynamically based on a property which is set in OnLoad event of a page. I have two such controls on a page and second control items should be based on items selected in first control.
The problem is, I can't figure out, how to catch on autopostback which boxes were selected in first control before second contol is constructed to pass this data to it?
Take a look at this.
http://forums.asp.net/t/1440174.aspx
Since your building them dynamically, they are not as easy to find as webforms would like to be, if you added them to the page and wired up events and such.
Your going to look at the Request.Forms list, and search thru it for any controls you want.
I believe checkboxes are like radio buttons, they only return if they are checked, which is good, cause you want to know which ones were checked.
I've used same solution as in the accepted answer for this question: Dynamically Change User Control in ASP.Net , just need to assign an unique id for each dynamically created CheckBox in custom control. Not as clean solution as I want but at least it works.
You can save the data in the ViewState, QueryString or as Session before moving to the next page and you can do modifications based on it.

FormView or not?

I have an ASP.NET page with a Wizard control containing several pages of form fields. The data is collected and inserted to a database from the code behind page. I need to set this form up so you can not only insert, but edit a record as well. Since the form is long and complex, I would rather use the existing one and not make a duplicate one for editing, especially since I want to keep both forms exactly the same and any edits would have to be made to both. But it looks like this is what I need to do if I'm going to databind it. But this would also involve putting the Wizard inside of a FormView, and then I'd have to use FindControl to access any of the fields which would mean altering all my already-existing code (which of course would be time-consuming). So should I manually enter all the values from the code behind instead of databinding it? Which is better, to use a FormView and have duplicate forms (plus have to go in and redo the way I access the fields), or to do everything from the code behind?
I cheat in this circumstance. :)
Create each screen as 2 separate user controls
One for edit, and one for view
Then you get access to all you usual coding
Then embed the controls into the Wizard/FormView
I would suggest you to Go Ahead using FormView, as using DataBind control you have more control the functionality and layout Insert/Edit/View template. Since you have specified that your form is very complex and long, if you control from code behind you have to do lot of work to handle this in code behind and lot code required.
Since I have personal experience to develope very complex form using FormView and it was easy for me bind the Value in directly in formview instead if you assign/Get Values of each conrol in code behind and sometimes you have to hide.

Dynamically creating many instances of ASP.NET usercontrols: How do I create it, and where will my conflicts be?

I haven't seen this implemented before in ASP.NET, but am thinking about a UI that would look and act like this:
Conceptual Overview
A TabControl is loaded and the first tab contains a grid
When a row is double-clicked, a new tab is created with the record detail
The content of the tab/record detail is created by a usercontrol
Many tabs could be created, and therefore many instances of the usercontrol will be created
I know ASP.NET will rename my (runat="server") ID's for me, and that I can use jQuery or ASP.NET server-side code to work with the ID's... My concerns are:
How can I ask ASP.NET to generate a unique ID for each Nth instance of my usercontrol (to be rendered in a placeholder)
How do I actually create that extra instance of the control?
What else do I need to keep in mind?
Since I don't want postbacks I'm considering basing my implementation off of ComponentArt's Callback Control, and using ASP.net usercontrols to achieve this effect. This will allow me to do most things that require a postback, but won't refresh all the elements on a page... just the section that contains the user control. That being said, I'm not tied to a particular implementation.
You should look into the Page.LoadControl method. It works nicely and as far as I remember you put placeholders on your page and load the controls into the PlaceHolders, that's how you control the ids.
One thing that doesn't work out so well with this approach is when your control raises events that your Page object has to handle. If your control is selfcontained however you shouldn't have a problem.
This might help you get started:
http://www.codeproject.com/KB/aspnet/LoadingUSerControl.aspx

should every element with runat=server have an id attribute?

I have inherited an asp.net website to maintain
when looking at the aspx page, almost every element with runat=server don't have id attribute defined.
should I go through every element and add one?
Unless the control is inside a repeating control it should have a unique ID attribute assigned to it. Here is some MSDN documentation on how to add server controls to an ASPX page.
You only need to give them ids if you're going to reference them in your code. If you're not referencing them at all you might wonder why they are .net controls and not just html elements.
ASP will assign an unique ID if you don't. Sometimes I don't bother when I won't be manipulating it.
Speaking specifically to your situation of having inherited an app, it's not a pressing issue. ASP.NET will automatically generate IDs for them at runtime.
Generally, aiming to replace those controls with HTML elements (as someone else mentioned) is a good idea. Do be careful though. Just because an element doesn't have an ID doesn't mean it's not being referenced on the server side at runtime.
You don't need to set IDs to the controls you don't reference. ASP.NET will do it for you. That's it.
This does NOT automatically mean, they can be replaced with HTML controls. Having a control with runat="server" without having an ID set is perfectly reasonable.
e.g.:
<asp:ListView runat="server" DataSourceID="someDataSource">

Resources