Repositioning Items In ASP.NET ListView - c#

I have a ListView control on my ASP.NET web form.
It is populated with a simple SELECT from SQL data source. One of the items returned by SELECT has a value "All".
I want that item to be the first item of the ListView.
I tried setting DisplayIndex and DataItemIndex to 0, but these properties are read-only. ORDER BY does not always work either.
Could someone please provide a solution in c# or SQL? Thank you.
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ListName" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="ListNameLabel" runat="server" Text='<%# Eval("ListName") %>' />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT [ListName] FROM [Lists]"></asp:SqlDataSource>

Ok, I designed an ugly solution for it. SELECT statement should look like this:
SELECT [ListName] FROM [Lists] WHERE [ListName] = 'All' UNION SELECT [ListName] FROM [Lists] WHERE [ListName] != 'All'

Related

my ASP.NET GridView does not bind correctly

I have an ASP.NET GridView supposed to exec a SQL Query.
The query is never executed so I strongly suspect there is a binding issue somewhere.
Here is the aspx code:
<asp:SqlDataSource ID="i_sdsGv" runat="server" ConnectionString="<%$ ConnectionStrings:XXX %>"
SelectCommand="INCOHERENT QUERY IN ORDER TO TRIGGER AN EXCEPTION"
SelectCommandType="Text"
<SelectParameters>
<asp:QueryStringParameter Name="MyTableId" QueryStringField="MyTableId" />
</SelectParameters>
</asp:SqlDataSource>
...
<asp:GridView ID="i_gv" runat="server" AutoGenerateColumns="False" DataKeyNames="MyTableId"
DataSourceID="i_sdsGv" OnRowDataBound="i_gvOptionChoix_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="i_tbName" runat="server" Text='<%# Bind("MyTableName") %>' />
</ItemTemplate>
</Columns>
<EmptyDataTemplate>
NO ROWS TO DISPLAY
</EmptyDataTemplate>
</asp:GridView>
I am 100% sure that if the query string is inexact, an exception is triggered.
In my case, no exception is triggered so the query is not executed.
I get the NO ROWS TO DISPLAY text.
The only obvious binding that I can think about is between the DataSource and the GridView. It does look OK to me.
So I would suspect an issue with :
DataKeyNames="MyTableId" ?
My i_gvOptionChoix_RowDataBound method is empty at the moment but I think it does not matter at this time?
What am I missing so the app actually attempts to execute the query?
Thx in advance for your kind assistance

Data not displaying from my database

It should be so freakin' easy, but why is my data not displaying? I even used the AccessDataSource wizard to configure the datasource and I still can't get it to show what is there. Here is my markup:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.mdb"
SelectCommand="SELECT ID, Destinations FROM [Destinations]">
</asp:AccessDataSource>
<asp:Label ID="CurrentDestLabel" runat="server" Text="What is currently displayed:" />
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Destinations") %>'/>
I don't get any errors or anything, just nothing displays for my "Label1". Any thoughts?
I am not quite sure, but I think you try to show a whole List in a single label...
perhaps your command need something like "Select top 1 Destinations FROM [Destinations]"
So you make sure, that you have only one Element selected...
Make sure to include any databound controls in a DetailsView, FormView, Gridview, etc...

How to load the data in asp:grid after providing filters?

I have to load the data in asp:grid after filtering from two asp:dropdown list. For example following is the dropdown lists:
<asp:DropDownList ID="ddlb" runat="server" AutoPostBack="True"
CssClass="ddlb" Width="100px" DataSourceID="SqlDataSource3"
DataTextField="MonthYearName" DataValueField="MonthYear"
onselectedindexchanged="Changed">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ProdDB %>" SelectCommand="# some sql query">
</asp:SqlDataSource>
<asp:DropDownList ID="asd" runat="server" AutoPostBack="True"
CssClass="ddlb" Width="100px" DataSourceID="SqlDataSource4"
DataTextField="MonthYearName" DataValueField="MonthYear"
onselectedindexchanged="SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ProdDB %>" SelectCommand="# some queries">
</asp:SqlDataSource>
Now, When i am loading the page. It shows the above defined dropdown list. What i want to know is "how should i load the data in aspx grid using the above two dropdown filter ?".
For example i choose A option from the first dropdown then it filter the result on the basis of it and the data will display on grid and so on...
Please tell me how should i connect the grid with these two dropdown list ?
How should i filter it ?
Place a grid and two drop down on the page.
Place an SQL Data Source
Provide two parameters to the sql datasource select command
Attached that sqldatasource to the gridview
Bind the two parameters of the SQLDataSource to the the two Dropdown boxes.

How to dynamically add any number of images to web form where the source string is in a database?

Hi I am trying to set up a page for users to view details of specific cars and photos relating to the cars. So far I am using a sql table to hold the path of the images and then assigning each path as a "ImageUrl" attribute to img tags that I have manually created.
My question is: Is there a way to have the img tags dynamically created and assign a "ImageUrl" accordingly depending on how many related entities are in the table because the number of pictures will change? If this is not possible could anyone provide any alternatives?
Types of scripts I am using in other parts of my web form are C#, javacript, sqlcommand. I am fairly new at this and my search only show how to assign the ImageUrl in the code behind and that is what I have done. Thank you ahead of time.
This is what I have for the img tags:
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Default.png" />
<asp:Image ID="Image2" runat="server" ImageUrl="~/Images/Default.png" />
And this is the code behind:
List<string> folder = new List<string>();
while (readerPhoto.Read())
{
folder.Add(readerPhoto["Folder"].ToString());
}
switch (folder.Count)
{
case 1:
Image1.ImageUrl = folder[0];
break;
case 2:
Image1.ImageUrl = folder[0];
Image2.ImageUrl = folder[1];
//and so fourth.......
08/07/12. New attempt code (the images are created dynamically but the "src" attribute is not assigned correctly):
.aspx
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CarsConnectionString %>"
SelectCommand="SELECT Folder FROM Cars INNER JOIN Photos ON Cars.SN = Photos.Cars_SN WHERE (Cars.SN = #SN)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="SN" Name="SN" QueryStringField="SN" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="lvPhotos" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Image ID="imgListImage" runat="server" ImageUrl='<% Eval("Folder") %>' />
</ItemTemplate>
</asp:ListView>
Html output a broken image icon and the script as:
<img id="MainContent_ListView1_imgListImage_0" src="<%%20Eval("Folder")%20%>" style="width: 80px; height: 80px; ">
You need to use the data binding markup syntax (note the #):
<ItemTemplate>
<asp:Image ID="imgListImage" runat="server" ImageUrl='<%# Eval("Folder") %>' />
</ItemTemplate>
EDIT: somehow i missed that you use sql table and not the file.
EDIT 2: fixed binding error
Basically you dont need any code behind at all. You need SqlDataSource and ListView and bind second to first one.
.aspx
<asp:SqlDataSource id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnString%>"
SelectCommand="SELECT * FROM Table">
</asp:SqlDataSource>
<asp:ListView ID="lvImages" runat="server" DataSource="SqlDataSource1">
<ItemTemplate>
<asp:Image ID="imgListImage" runat="server" ImageUrl='<%# Eval("Folder") %>' />
</ItemTemplate>
</asp:ListView>
ListView on msdn

Multiple Column Dropdown in Details View control

I have created a dropdown list in a DetailsView control
I want to make that dropdown have multiple columns. Is that possible? If So how?
I've done a lot of searching and cant seem to find the solution.
Below is the code for my dropdown template:
<asp:TemplateField HeaderText="Division" SortExpression="fDivisionID">
<EditItemTemplate>
<asp:DropDownList
ID="DivisionDropDownList"
runat="server"
DataSourceID="DivisionSqlDataSource"
DataTextField="DivisionName"
DataValueField="DivisionID"
Text='<%# Bind("fDivisionID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DivisionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
How do I add another column to that.. such as "Location"..
Thanks in advance
There are I think three ways actually.
Add two columns in your Select statement when fetching from database like below. or from here
Select Column1 + ' ' + Column2 From Yourtable
You can use third party controls.
Jquery Also supports this feature Here

Categories