I am new to asp.net and even though I have been successfully developing some decent pages
that access backend SQL server using C# code behind, I have run into a problem that I need
some nudge in the right direction on.
I need to be able to display records from a SQL datasource (could be displayed using SQLDataSource method
OR using a dataReader OR using a gridview data fill) onto a page but I need other information displayed above
and below the returned record. Its best if I show an example:
<horizontal seperator line>
Customer ID
Customer Name
Customer Address
City, ST
COLUMNS FOR THE RETURNED RECORD DATA
THE ACTUAL RECORD DATA
OTHER DYNAMICALLY CALCULATED DATA BELOW THE RECORDS
<horizontal seperator line>
And this repeats for every record that is returned
For the life of me, I can't find determine what view, template, or even best data binder or
data source control it is that I need to use to make this happen.
Thanks for anyone's assistance in advance.
dell
If a datatable or gridview won't do, you can always use a Repeater or a Listview.
In the template, you can have all the controls you want in there.
You probably need a repeater similar to this:
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<hr/>
<asp:Label runat="server" ID="labelCustomerId" />
<div style="padding-left:150px">
<asp:Label runat="server" ID="labelCustomerName" />
<asp:Label runat="server" ID="labelCustomerAddress" />
<asp:Label runat="server" ID="labelCityState" />
</div>
<asp:GridView runat="server" ID="gridViewRecordData">
<!--define columns & footer calculations here -->
</asp:GridView>
<hr/>
</ItemTemplate>
</asp:Repeater>
You can use the same ItemTemplate if you chose a ListView
Repeated is the best control. but in repeater you need to write code for Paging, Sorting...etc functiontly.
If you don't want to code for paging, sorting then go for GridView.
Try a ListView control. It lets you use templates to define how records, groups of records, etc. show up. For example you could define a template for your customer contact info, then a template for your records and other data.
You can accomplish this easily with a ListView. This article has a display of product information that's a lot like your layout -- horizontal lines between repeated sections.
I also like Scott Gu's tutorial, though the data layout (catalog images) is a little different from yours. Still, there's some good screenshots, and it also demos a Linq to SQL data source, which is a popular choice.
Related
I want to display every individual record from table on my web page, if the records in table increases the number of div should also increase.
How do i achieve this using ASP.net.
Please if any one can guide me as i am new to ASP.Net
Thank you in advance
As stated earlier using MVC would be a nice approach but u will have to learn a bit to use it.
As per the current scenario you can use repeaters which are exactly designed for what you want to accomplish.
assuming your database looks something similar to this..
id int primary key
name nvarchar(50)
etc...
//suppose you have your database data in "dataFromDatabase"
now in your asp.net page just define a repeater like this
<div id="content">
<asp:repeater id="repeaterData" runat="server">
<HeaderTemplate>
<Table>
<tr>
<th>Id:</th>
<th>Name : </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<td><%#Eval("id")%></td>
<td><%#Eval("name")%></td>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</repeater>
</div>
and in your .aspx.cs page
bind the repeater with your datasource
repeaterData.DataSource = dataFromDatabase
repeaterData.DataBind();
thats all you need to do to get it up and running...
and using GridView would be even more simpler you just need to drag that from ToolBox to your aspx file and select your data Source visually and violla you are done... no coding required.....
although you will have to do some coding for the edit, delete and some fancy stuff...
Hope it helps.
It sounds like you're looking to implement a repeater class.
EDIT
Here is some sample code per your request from the link mentioned above:
<asp:Repeater
DataMember="string"
DataSource="string"
DataSourceID="string"
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnItemCommand="ItemCommand event handler"
OnItemCreated="ItemCreated event handler"
OnItemDataBound="ItemDataBound event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
Visible="True|False"
>
<AlternatingItemTemplate>
<!-- child controls -->
</AlternatingItemTemplate>
<FooterTemplate>
<!-- child controls -->
</FooterTemplate>
<HeaderTemplate>
<!-- child controls -->
</HeaderTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
<SeparatorTemplate>
<!-- child controls -->
</SeparatorTemplate>
</asp:Repeater>
You can use gridview or repeater to solve your.
Gridview basic example
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0
Repeater basic example
http://www.w3schools.com/aspnet/aspnet_repeater.asp
If you're completely new to ASP.NET, I'd suggest working with the ASP.NET MVC Framework instead of with WebForms. Ultimately it's a matter of taste, but the MVC framework has been designed with the deliberate goal of making it easy to write good quality code, so if you're starting fresh anyway you might as well learn to do it well from the beginning.
The best place to start is probably at the official site of ASP.NET MVC. They have a good "getting started"-guide, and lots and lots of tutorials on levels from absolute beginner to advanced web developing.
And a side note to all the WebForms fans out there: I'm definitely not saying you can't write good quality code with WebForms - however, if you look around the internet most examples you find are poorly constructed and full of antipatterns. Starting with ASP.NET MVC Framework yields a higher chance of the web resources you find demonstrating good practices, rather than quick hacks.
or:
for(int i= 0;i<10i++){
label.text="<div>"+something+"</div>"+label.text
}
What is the trick to implement the following interface in ASP.NET?
I think the screenshot should explain everything.
Both dropDownLists work on SQL tables. [Categories],[Items] bound with ID_category PK/FK
Number of rows variable (by default 1 row with "Add" button)
Choosing category changes only the content of the dropDownList next to it and the content stays the same when other rows get added/deleted
"Add" button saves current setup and adds new row with either "Select value" or eventually 1st category/1st item selected
How to store previously selected categories/items and the dropDown filtering by category, while adding, changing, deleting items or some other PostBack on website?
What I have tried so far is to use Repeater with DataSet, however I have encountered several different problems:
Was unable to preserve the relations between previous dropDowns
DropDowns resetting one another
2nd dropDown losing filtering when adding new row
I can post some code but since it doesn't fully work, perhaps a totally different approach would be better. Basically I started with this tutorial. The repeater currently looks like:
<repeater>
<itemtemplate>
<dropDown DataSource="categoriesDS"
value='<%# DataBinder.Eval(Container.DataItem, "Category") %>'.../>
<dropDown DataSource="itemsDS"
value='<%# DataBinder.Eval(Container.DataItem, "Item") %>'.../>
<button CommandName='<%# DataBinder.Eval(Container.DataItem, "Button") %>'
Text='<%# DataBinder.Eval(Container.DataItem, "Button") %>' .../>
</itemtemplate>
</repeater>
Thanks in advance for any suggestions or solutions and I hope it'll be helpful for others.
Ajax Cascading DropDownlist
It is very simple to use.
I have a list view binded to a data source.
How can I enable sorting by column?
So when clicking the column title once it sorts by ascending, click it again it sorts by descending.
Is there anything in the wizard that can be setup or it all needs to be done programatically?
If you are using a data source control then sorting can be implemented in the ListView control without having to write a single line of code - it is all handled automatically by the ListView and its data source control. Of course, in more advanced scenarios we may need to manually sort the data or programmatically.
In the following example, ListView will be sorted by ProductName and UnitPrice. simply add two LinkButtons in the ListView's LayoutTemplate with appropriate CommandName and CommandArgument property values.
<asp:ListView ID="ProductList" runat="server" DataSourceID="ProductDataSource">
<LayoutTemplate>
<h3>Product Listing</h3>
<asp:LinkButton runat="server" ID="SortByName" CommandName="Sort"
CommandArgument="ProductName">Sort by Name</asp:LinkButton>
| <asp:LinkButton runat="server" ID="SortByPrice" CommandName="Sort"
CommandArgument="UnitPrice">Sort by Price</asp:LinkButton>
<blockquote>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</blockquote>
</LayoutTemplate>
...
</asp:ListView>
If a user clicks the "Sort by Price" link twice in a row, the first time the products will be sorted in ascending order (from cheapest to most expensive), but the second time the sort order will reverse.
That depends on the DataSource you are using. If the Data Source allows sorting, it can be set up in the way described here http://msdn.microsoft.com/en-us/library/sa88ktts.aspx . Otherwise you have to handle sorting yourself programatically. Usually that is even easier than the automatical approach.
using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
using Northwind;
N00b here,
Trying to customize a asp.net gridview, and I'm stuck at this part...
In the ItemTemplate, I've got a text-box that has a class applied to it where on .keypress(Clientside JS), a asp:LinkButton appears next to it that's formatted like a Jquery UI icon. I'm stuck at this part. I know that I can "enable editing" for the gridview, but I want to make an edit button only appear on .keypress, which I’ve already achieved. The park I’m stuck at is trying to get the click of that asp:LinkButton to send an update command to the SQLserver to update that db entry accordingly . So far, I’ve got the textbox to be created dynamically for each db item in that column.
Is there a more practical way of doing this while achieving the same results? To be more specific, I'm not really looking for code, cause I'm trying to learn, but more for methods I should take a look at, or other tutorials that I missed on the web.
I would also appreciate some advice, thanks in advance: )
You can try using GridView_RowCommand event to fetch the Command event bubbled by Linkbutton.
You can use CommandName Property of the LinkButton and set it to Update.
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:linkbutton ID="Button1" text="TextBox1" runat="server" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
I looking forward to find a suitable asp.net DataBound control (C#) to implement in my application.
I wanted to create a exam page where each page show 10 questions, each question got a Label control and a radiobutton control to display the choices, the data to be bind into the DataBound control might be having multiple rows where each rows represent each question.
I found that DetailView control was quite suit with my requirement but I not able to set the page size.
Please help to give some suggestion and advises, thank you in advanced.
I would use a DataList or a ListView, because it will allow you to enter a template for each item. The reason I would choose these over a repeater is because you can use data keys, which will probably come in handy.
Here's a simple example of how you could implement a list of questions:
<asp:DataList ID="DataList1" runat="server" DataKeyField="QuestionID" ...>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Question")%>' />
<asp:RadioButton ID="RadioButton1" runat="server" Text="Yes" GroupName="QuestionAnswer" ... />
<asp:RadioButton ID="RadioButton2" runat="server" Text="No" GroupName="QuestionAnswer" ... />
</ItemTemplate>
</asp:DataList>
I would recommend to you to use the Repeater control since you can customize it's design very easily to suit your needs.
Here are two tutorials on how to use it:
http://www.w3schools.com/aspnet/aspnet_repeater.asp
http://www.learn-asp.net/asptutorials/Repeater.aspx
Update:
Repeater doesn't have pagination included so you would have to add it:
http://blog.divergencehosting.com/2009/03/25/adding-paging-repeater/
Other option would be to just use a GridView which has pagination included.