Using a TemplateField inside a web user control - asp.net

Is it possible to use a TemplateField (or any *Field from a GridView) inside a user control (ascx).
I have a complex TemplateField (item, edit, footer) that I would like to easily reuse.
Thanks.

As the question is worded, there is no way. What it sounds like you are doing (making the elements of a TemplateColumn reusable) then there are two ways that come to mind right off.
One, put your User Control into the ItemTemplate/FooterTemlate of the TemplateColumn.
Two, build the complex GridView Column as a standalone server control, which you can use in place of a TemplateColumn (or BoundColumn).

GridView is a bit top-heavy for a user control - you could create a custom GridView in a separate control library and simply reuse it that way.

Related

What is advantage of repeater control over grid view in asp.net C#?

I have read many articles regarding difference between gridview and repeater. I come to know that gridview pattern is fixed in and , where as repeater can provide customized HTML mark up. If I am not wrong, we can also customize HTML mark up by adding template field and placing table with customized design. In that tables we can place labels and other .net control and can get whatever we want. Then why to use repeater control.
I am confused in which scenario it can be preferred over gridview.
In simple words we can say performance of repeater is far better than gridview. If you need basic rendering for read only items then its better to use repeater and if you need events , pagination and editable controls then you should go for gridview. Simpler controls with less in built functionality are speedy. you can do implement all functionalities of grid view to repeater but you have to do it manually.
So it depends upon you requirements either you need repeater or gridview
This discussion will be helpfull for you
http://forums.asp.net/t/1072020.aspx

How can I make a repeater field editable?

I have a page with a repeater.. I have to make some fields of this one editable. I don't see how I can transform the repeater label into a textbox.. Can I use jquery to do that?
Have somebody make this kind of manipulation?
Thanks..
The Repeater control does not have an EditTemplate like many of the other data controls.
I would suggest having the edit fields either in a hidden Placeholder, then show this when clicking an edit button. This would involve the page posting back and then you having to show/hide the relevant parts in the ItemCommand handler.
Another way would be do add the edit fields/textboxs in a Panel control, then hide this through display: none;. Then you can change this to display: block; with some javascript. This will avoid the page PostBack.
This can be done in a Repeater, but a DataList control is more straightforward and is just as easy to use. There's an MSDN article on doing it in a Datalist control with full source code here: http://msdn.microsoft.com/en-us/library/bf5211wb(v=vs.71).aspx
Converting a repeater to a DataList is a much easier approach than having editable items in a Repeater.
HOWEVER
to answer your question directly, there IS a Codeproject sample here: http://www.codeproject.com/KB/aspnet/EditableRepeater.aspx
that shows how to use a Repeater with full edit functionality (including adding and deleting items).
To see the relevant code in the CodePlex article, search for the text "EditIndex". The relevant code-behind is always a few lines above and/or below this keyword.
It depends on how you want to do this:
using standart control probably you need GridView.
You can define the template for repeater and put TextBox there, then
on postback you will need to find dynamically created controls and
also you will need to take care to keep ID's of these controls the
same on postbacks.
And the other thing - you can replace label with the textBox using
jQuery and then update value via Ajax reques.
You decide what you need :) Anyway it's a lot of samples in the internet.

Aligning a asp:Button to to the right of asp:Wizard's Next Button

Coding Platform: ASP.NET C#
Controls Used: asp:Wizard and asp:Button
I have a asp:Wizard and when it renders it have Previous and Next Buttons
I would like to place a button to the right of Next Button.
Now that button is being rendered below asp:Wizard Control.
Any method to accomplish it?
Update: I am not using StartNavigationTemplate, StepNavigationTemplate and FinishNavigationTemplate
You could extend the Wizard control, but that's probably more effort than it's worth.
If you want to get a similar effect and you're not tied to the Wizard, you can abandon the Wizard control and use a MultiView instead. If you do, you need to control the flow programatically yourself (assign a variable to track the current index, create previous and next buttons, adjust the current index, etc).
It's a little more work, but you get better control. (It's like using a Repeater vs. a GridView. With a Gridview you can only do tables. With a Repeater, you can do tables, but it's a bit more work, or you can do something completely different.)
I like this article for explaining the similarity.

Custom CheckBoxList in ASP.NET

Since ASP.NET's CheckBoxList control does not allow itself to be validated with one of the standard validation controls (i.e., RequiredFieldValidator), I would like to create a UserControl that I can use in my project whenever I need a checkbox list that requires one or more boxes to be checked.
The standard CheckBoxList can be dragged onto a page, and then you can manually add <asp:ListItem> controls if you want. Is there any way I can create a UserControl that lets me manually (in the markup, not programmatically) insert ListItems from my page in a similar manner?
In other words, can I insert a UserControl onto a page, and then from the Designer view of the Page (i.e., not the designer view of the UserControl), can I manually add my ListItems like so:
<uc1:RequiredCheckBoxList>
<asp:ListItem Text="A" value="B"></asp:ListItem>
<asp:ListItem Text="X" value="Y"></asp:ListItem>
</uc1:RequiredCheckBoxList>
If a UserControl is not the appropriate choice for the end result I'm looking for, I'm open to other suggestions.
Please note that I am aware of the CustomValidator control (which is how I plan to validate within my UserControl). It's just a pain to write the same basic code each time I need one of these required checkbox lists, which is why I want to create a re-usable control.
An easier way to solve this problem is to use the standard CheckBoxList, but create a special server validator control specifically for the CheckBoxList. The following article shows you how to go about this - Creating Validator Controls for the CheckBox and CheckBoxList.

Repeater, ListView, DataList, DataGrid, GridView ... Which to choose?

So many different controls to choose from! What are best practices for determining which control to use for displaying data in ASP.NET?
It's really about what you trying to achieve
Gridview - Limited in design, works like an html table. More in built functionality like edit/update, page, sort. Lots of overhead.
DataGrid - Old version of the Gridview. A gridview is a super datagrid.
Datalist - more customisable version of the Gridview. Also has some overhead. More manual work as you have to design it yourself.
ListView - the new Datalist :). Almost a hybrid of the datalist and gridview where you can use paging and build in Gridview like functionality, but have the freedom of design. One of the new controls in this family
Repeater - Very light weight. No built in functionality like Headers, Footers. Has the least overhead.
Everyone else hit it: It Depends.
Now for some specific guidance (expanding upon WebDude's excellent answer above) ...
Does your design fit into a natural spreadsheet or grid view of the data? GridView.
Do you need to display a list or other formatted view of data, possibly with headers and footers, and probably with specific controls and/or formatting for each record of data? (EG, customized links, possibly LinkButtons, or specific edit controls?) Does this display specifically not fit naturally into a spreadsheet or grid view? ListView
If you meet all the criteria of ListView, but you would naturally fit in a grid, you may consider DataList.
I go for Repeater when I just need some basic data iterated with some custom design bits, no headers, no footers, nice and clean.
Markup View
Declaring the following sample code is possible for all 3( ListView, DataList , Repeater)
<asp:ListView runat="server" OnItemCommand="Unnamed1_ItemCommand">
<ItemTemplate> <%# Eval("Name")%> </ItemTemplate>
<asp:ListView>
in the following lists You can see the available templates and options for each of them and see the differences for yourself
ListView (note the edit,group,insert ,layout)
AlternatingltemTemplate
EditltemTemplate
EmptyDataTemplate
EmptyltemTemplate
GroupSeparatorTemplate
GroupTemplate
lnsertltemTemplate
ItemSeparatorTemplate
ItemTemplate
LayoutTemplate
SelectedltemTemplate
DataList (note the Style pairs)
AlternatingltemStyle
AlternatingltemTemplate
EditltemStyle
EditltemTemplate
FooterStyle
FooterTemplate
HeaderStyle
HeaderTemplate
ItemStyle
ItemTemplate
SelectedltemStyle
SelectedltemTemplate
SeparatorStyle
SeparatorTemplate
Repeater
AlternatingltemTemplate
FooterTemplate
HeaderTemplate
ItemTemplate
SeparatorTemplate
Code View (advanced view)
CompositeDataBoundControl:
look the following classes hierarchy (and related controls).
these controls hosts other asp.net controls in their templates to display bound-data to user
Some descriptions for better clarifications
The ListView Control
The ListView control also uses templates for the display of data. However, it supports many
additional templates that allow for more scenarios when working with your data. These templates include the LayoutTemplate,GroupTemplate,ItemSeparatorTemplate.
The ListView control (unlike DataList and Repeater) also implicitly supports the ability to
edit, insert, and delete data by using a data source control. You can define individual templates
for each of these scenarios.
The DataList Control
The DataList control works like the Repeater control. It repeats data for each row in your data set,
and it displays this data according to your defined template. However, it lays out the data defined
in the template within various HTML structures. This includes options for horizontal or vertical
layout, and it also allows you to set how the data should be repeated, as flow or table layout.
The DataList control does not automatically use a data source control to edit data. Instead,
it provides command events in which you can write your own code for these scenarios. To
enable these events, you add a Button control to one of the templates and set the button’s
CommandName property to the edit, delete, update, or cancel keyword. The appropriate
event is then raised by the DataList control.
The Repeater Control
The Repeater control also uses templates to define custom binding. However, it does not show data as individual records. Instead, it repeats the data rows as you specify in your template. This
allows you to create a single row of data and have it repeat across your page.
The Repeater control is a read-only template. That is, it supports only the ItemTemplate.
It does not implicitly support editing, insertion, and deletion. You should consider one of the
other controls if you need this functionality, otherwise you will have to code this yourself for
the Repeater control.
The above Descriptions are from MCTS Exam 70-515 Web Applications Development with Microsoft.NET Framework 4 book.
DataGrid is not even mentioned in this book and is replaced by popular GridViews and answered nicely by other users
It all comes down to how you want to layout your data.
If you need to control the layout (like tables versus CSS versus whatever), when use a Repeater or ListView. Between the two, ListView gives you a lot more events and built-in commands for editing, selecting, inserting. Additionally paging and grouping functionality. A Repeater is extremely simple, it repeats a layout with the data. Since you're building the layout by hand, Listview and Repeater require more code.
GridView is an updated DataGrid so there is hardly any reason to use DataGrid. GridView works really well when hooked up to standard ASP.NET datasources, but restricts you to a tabular layout with lots of layout rules. GridView requires less code since you're using a built-in layout.
Indeed! I've blogged on the differences between the ASP.NET 4.0 data tools. Basically, gridviews are the most powerful way to present tabular information, whereas ListView controls are for more complicated displays of repeated data. If I were giving advice to an ASP.NET newbie, I'd tell them to learn gridviews inside out and ignore the other controls to begin with.

Resources