Programmatically select/use ItemTemplate or AlternatingItemTemplate - asp.net

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.

Related

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.

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.

ASP.NET three-level databinding

I have custom objects I am binding to using ObjectDataSource. I have three-level binding: a DropDownList (Department) that filters the next DropDownList (Category) that filters a GridView (Questions). Each ObjectDataSource binds to the previous control's SelectedValue (except the first one, of course).
Everything works fine only to the next level (Department to Category and Category to Questions). When I change the Department, the Category list gets updated correctly, but the Questions that show up are from the previously selected category.
How can I get this three-level binding to work correctly? I can't figure out whether I am missing something. If I have to, I could implement SelectedIndexChanged on the first list and manually force the update on the grid, but this is not ideal. Thanks for your help!
A bit more info: I don't have a default "select an item" option. That means that when I change the Department, the first Category is automatically selected. I was hoping the binding would be smart enough to trickle that all the way down. It was smart enough that I didn't have to do the if (!IsPostBack) { // Load data }.
I currently have implemented Department_SelectedIndexChanged() and simply done a Questions.DataSource = Questions.DataSource;. That seems to "refresh" everything properly. Is there a better way to do this?
Could you clear the grid instead until the second value is refreshed? Are you looking for an AjAX appraoch, or does this use postbacks?
There isn't an automatic solution that I'm aware of so you would need to do something like you mentioned because how else would the page know to refresh the grid?

Resources