Is it possible to dynamically create an ItemTemplate for a repeater somehow?
I am trying to use a repeater since it allows the most control, but one of my requirements is making me reconsider.
I basically have a number of SQL queries that I do through a web service. Rather than having users type in the entire query I want them to be able to select "parts", "products", or "packages" via radio buttons, enter a search term in a text box, and some other info, and the page returns the results they want. I have this mostly done, the RadioButtons control logic, and I have the query set up to accept the input from the text box as a search term with wildcards. The only probelm is I am struggling with the repeater control. The problem is each one of the tables has a different number of columns and they have different names, so doing a
<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>
within the ItemTemplate is not possible(I don't know until bind time which one of the 3(possibly more in the future)templates to use)
I tried using a literal to pass in what I wanted based on logic in the codebehind, but I couldn't pass the inline functions, and I have been unable to put together how to do this based on an earlier question.
I have been reading the MSDN reference and it seems like if I learn the DataList control it will make things easier, but I'd rather not waste time on that if there's an easy way to do it with a repeater(which will also allow me more control)
Thank you
I may be off track here but I think an easy solution to the problem you're having would be to create multiple repeater controls with different items templates inside them and wrap them up in panels. That way depending on the user parameters you could simply databind your result to the proper repeater and set the other panels to invisible.
I was looking back through some old code for you to see how I've handled similar situations. Then I saw Jesse's answer right before I wrote my reply. Basically, I would tend to agree with Jesse there - that seems the most straightforward solution from what you've outlined.
So I don't know how much this helps, but one possibility would be to use MVC Templates. MVC is handy in that it can actually be used in a very limited sense (your entire app doesn't need to implement it, just the applicable page) and it's also pretty straightforward.
An introduction on how to do different templates within MVC: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
It might be possible to modify the template so that it changes based on the object that it's bound to - like how a WPF DataTemplateSelector works.
I thought I remember seeing something in MVC3 or MVC4 that you could create a Template based on a datatype (like you can in Silverlight) and it would automatically pick that Template, but I'm having trouble finding that now.
Related
I know how to create a simple XAML interface for a page, but I spend a lot of my time copying and pasting code since I have no idea how to create reusable XAML in a proper way, I know about styles but they don't quite fill the role. What I would prefer to do is to call them in the following way:
<CustomElement attribute1="bla" attribute2="{Binding somethingElse}"/>
Just like all the other GUI objects in the Silverlight framework.
Any help or hint would be useful.
Basically there are two options. Both solutions act like any other Silverlight control. They can be inserted at random places in pages like you would normally insert a Button or a StackPanel. The option of choice depends on the specific reuse scenario.
Create a UserControl. These define their own XAML layout and are very easy to create. Consider looking at ScottGu's tutorial.
Create a custom control. Custom controls also define their own XAML layout but through templating and styling. They are harder to create but support templating; this means other developers can decide that they will use the code behind your control, but specify a completely different layout. For more information look at Silverlight templating.
For a (much) better understanding, please look at this page comparing the features of UserControls and custom controls more in-depth.
I am working in a Visual Studio 2010 environment and I am looking for the most effective way to build a multicolumn list that will be displayed inside a jQuery Menu like:
I actually use a serverside DataList control that renders me a table with a RepeatColumns="3" attribute.
I would like to know the most efficient, frequently, most common technique to use for this purpose. I just look for guidelines :)
Thanks
You just need to render the content as show - it makes no difference whether it's inside a jQuery dropdown menu, or straight on a static page.
You can do this using a table or using CSS, depending on which you're more comfortable with. The DataList control renders - as you say - to a table, which presumably works fine. A lot of people will say that you should use straight CSS rather than a table, but for this example the content is fairly 'tabular' in nature so I don't see a problem with using a table.
If this is too hypothetical and needs to be somewhere else, please let me know.
I have a project that needs specific gridviews to appear on multiple pages. Instead of copying and pasting the gridviews on each of the pages I thought creating a user control for each specific gridview or create a page for each gridview and then use iframes would be my best options.
I have not used either extensively so I am looking to the SO community's experience, are there known problems with using user controls and/or iframes when it comes to:
validation
communication between user control/iframe and parent page
ajax/updatepanels containing user control/iframe
thanks in advance
Since a gridview is essentially a user control (that's very flexible), I would first explore doing this with neither of your options. If possible, use the standard gridview and let your data layer do most the work. However, assuming you already know that... but your requirements require one or the other of your options, here are things to consider.
An iframe is easy to implement but unless your need is really simplistic in terms of user interaction, the user control will be the most flexible. Another downside to an iframe is it's size (you're essentially loading two pages). The downside of user controls are the upfront time in building them.
Based on your criterion:
1. validation - can do with either option but you'll have more flexibility with a user control
2. communication between user control/iframe and parent page - much easier with user control unless query string parms will do the trick
3. ajax/updatepanels containing user control/iframe - again user control
Windows Form (in C#) - I need to create a custom listbox control for the following requirement:
There is a listbox with a long list of items. I want the user to be able to click in the list, and then start typing and have it automatically take them to the matching item (I call this "type ahead"). It needs to be able to do this for as many characters as they type that have a match.
This control should be really reusable without much changes.
What are things/points I should keep in mind while developing a reusable custom control? (if you provide a good pattern as a sample... will be more helpful)
In Real World scenario the first decision should be buy vs build. if your application is a serious one and not just for hobby and there is budget for it I would check some of those great controls collections like DevExpress or Telerik for WinForms. You would need a grid as well at some point, eventually, and surely your custom or framework one cannot compete theirs.
I refer to those libraries because i believe they already have a listbox with autocomplete as you described in your requirements. check the online demos on their websites...
Im looking to create a custom ASP.NET container control that will allow me to drag further controls into it within the VS designer.
The final HTML that im looking for is very simple..
<div id="panel1">
<div id="panel2">
</div>
<div id="panel2">
</div>
</div>
With additional controls being able to be dragged into panels 2 and 3.
Im sure its very simple but im struggling to find examples that will help.
Any pointers or ideas are appreciated!
Cheers
Stuart
I've done such things in the past, and yes, from my experience at that time there is not much documentation available. Even worse, at that time some of the documentation was incorrect or vague!
So, to save you all the headaches (ouch, it already starts to hurt when I just think of it :-P), here is some information you definitely need to know.
Basically all controls are for run-time use only. You can attach a ControlDesigner to a control with an attribute on the class definition, which the design time environment (VS.NET IDE) will load and use as a layer on top of your control.
Templates
Chris' suggestion to use Templates is in the right direction. Your control needs to store somewhere the 'content' of the div's, and Templates are the perfect solution. Make sure you get this part right at first. Note: template properties can behave weird if they have a set clause! Moreover, check the use of NotifyParentAttribute also.
When you've got the templates in place, and you can use declarative syntax in ASPX pages to add controls, and they render well, then you can start working on the designer.
For the designer you have 2 options; the easy and complex way.
Easy designer solution
Let's start with the easy way. The base ControlDesigner classes already provide a framework to show templates. You have probably already seen this in action, like in the GridView control and its template fields.
Check out the following MSDN article on creating a template control designer.
With this easy solution, you get an automatic implementation of the smart tag (the arrow to the right of a control during design-time), and can select a template to edit from a drop down list.
Complex designer solution
Now, if this is not really satisfying for you, and you would like to be able to edit controls just like a Panel control, then you have to dig deeper. So here is the complex solution using Control Designer Regions.
See the example in the example in the EditableDesignerRegion class.
What this example does, is overriding the CreateChildControls of the designer class. Remember I said the designer control is a layer on top of your run-time control? So this CreateChildControls method will run after your control's implementation. What you have to do, is mark a HTML element within your render output with a special designer region HTML attribute. In this way, the designer knows what part in your rendered control should be a region.
Now you have to instruct the IDE to assign a editor or viewer to your regions. You have to do this in the GetDesignTimeHtml(DesignerRegionCollection regions) method (notice the overloaded version of this method). As you can see, this method receives a collection of regions. You have to assign your editable of view regions to this collection. Important here - and this is the badly documented part - is that the order in this collection is very important. The value of the region attribute in your HTML, refers to the index in this collection.
So, now we have defined regions in our rendered output, assigned a editor or viewer to it. Next up is how to fill these regions and store the value from these regions back into our controls declaration.
These two actions are handled in GetEditableDesignerRegionContent and SetEditableDesignerRegionContent methods of the control designer. Here you see why it's important to name the regions that you've added to the collection in the GetDesignTimeHtml method. In these two methods you receive the region reference and by it's Name property you could determine which Template property of your control to read/write.
To read and write the template properties we use the magic of the ControlPersister and ControlParser. The persister creates an template instance from declarative ASP.NET (HTML) code. The parser does the job the other way around; creates plain HTML from a template instance.
In a nutshell
So it's up to you to decide whether the standard template editing framework is good enough for you. If you want to have fancy edit capabilities for both of your edit regions in your IDE, then you will have to implement the complex solution. Otherwise just stick with the simple implementation. The examples mentioned will help you a lot.
Here is a link to a MSDN article regarding what you are trying to do, unfortunently there is no VS designer support so it renders correctly from the server but not in the IDE.
How to: Create Templated ASP.NET User Controls
http://msdn.microsoft.com/en-us/library/36574bf6.aspx