Is there any solution to controls repeated properties? - asp.net

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.

Related

Programmatically select/use ItemTemplate or AlternatingItemTemplate

I have a ASP.NET repeater, and i want to make use of it's ItemTemplate and AlternatingItemTemplate slightly different as how they are supposed to be used. Instead of having ASP.NET to switch between the 2 for every odd/even item, i want to choose the right template in the ItemDataBound event.
Does anyone know whether this is possible?
I saw that which template is being applied, comes from which ItemType the row/item has. So if anyone knows how to manipulate that value, that would be great as well.
Turns out that this is really not possible. The only option you have here, is to write your own version that inherits from ASP.NET repeater. Then, of course, you can do/overrule whatever you like to do with your templates.

How does ClientIDMode work?

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.

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.

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.

Custom Data-bound control help

I have never done this before and am very beginner in this one, so please treat me that way and help me. Obviously this is a complex topic for me at this stage.
I need to develop a custom data-bound control which will contain, few text-boxes, drop-down lists, labels, and other standard web-server controls. And, I want to be able to load this control from a web page on button click. When the user enters any data to any of its child controls, i should be able to save, and also retrieve when the data-bound control is loaded again. Also, the data in the child control should retain their value on postback.
Can someone help me getting into building this? What approach should i follow? Any books or artilces matching my requirement will be really helpful which i can read and help me develop this.
Here is an example of a custom data bound control: http://msdn.microsoft.com/en-us/library/ms366540.aspx
Just out of curiosity, using a FormView or DetailsView control wouldn't solve the problem you are experiencing? It has to be a custom control?

Resources