display header of gridview even when results are null ASP.NET - asp.net

I have a GridView and it works fine. But when I have no result the header disapears. Is there a way to show it without code-behind?
I am using 3.5
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="Categorie"
DataValueField="Cat_ID"
AppendDataBoundItems="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Goed %>"
SelectCommand="SELECT * FROM [tbl_Cat]">
</asp:SqlDataSource>

update your sql query to return something in case of null.
if not exists (SELECT * FROM [tbl_Cat])
select ' ' as Categorie,' ' as catid
else
select SELECT * FROM [tbl_Cat]
will work on all framework verson
another option is to override databind method where you can check the datatable. if the table row count is 0 you can manually insert blank values and then databind.

Set the Below Property in gridview......
EmptyDataText="There are no crecords."
or set this template
<EmptyDataTemplate>
No data found!
</EmptyDataTemplate>

Set the ShowHeaderWhenEmpty property:
<asp:GridView runat="server" ShowHeaderWhenEmpty="true" ...

use property in gridview
ShowHeaderWhenEmpty="True"
You code with changes
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="Categorie"
DataValueField="Cat_ID"
ShowHeaderWhenEmpty="True"
AppendDataBoundItems="True">
for 3.5 follow this link
http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo.html

For .net 3.5 use
<EmptyDataTemplate>
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td colspan="2">No Data found..</td>
</tr>
</table>
</EmptyDataTemplate>
This would be the easiest way as far as I am concerned.

Related

aspnet web forms An item with the same key has already been added

I am using asp net 4.5.2 with model binding; I have a problem that whenever I try to use model binding on DropDownList It raises the error An item with the same key has already been added but if I change it to SQLDataSource the error goes away.
Thanks in advance
Update
Sample Code
<asp:ListView runat="server" ID="lvChairItem" DataKeyNames="ChairItemId" InsertItemPosition="FirstItem" OnItemCanceling="lv_ItemCanceling" OnItemUpdated="lv_ItemUpdated" OnItemEditing="lv_ItemEditing" ClientIDMode="AutoID" OnCallingDataMethods="ChairItem_CallingDataMethods" SelectMethod="GetByChairId" InsertMethod="InsertItem" UpdateMethod="UpdateItem" DeleteMethod="DeleteItem" ItemType="App.Model.ChairItem">
.............
<InsertItemTemplate>
<tr>
........
<td>
<asp:DropDownList ID="ddlRoomId" CssClass="" DataValueField="Value" DataTextField="Text" AppendDataBoundItems="true" Text='<%# BindItem.RoomId %>' Width="100%" OnCallingDataMethods="Room_CallingDataMethods" SelectMethod="GetAllForDDL" runat="server" EnableViewState="true">
<asp:ListItem Value="">--Select--</asp:ListItem>
</asp:DropDownList>
</td>
........
</tr>
</InsertItemTemplate>
.....................
</asp:ListView>
Then the issue is likely because you are populating it on page load without checking for Page.IsPostBack. You don't want to re-populate the controls on each postback. You do want to (often) allow ViewState. So I would re-enable, and wrap your populate method in (not page.ispostback).

Display multiple datas from same table from mysql using listview

I am using a listview to display datas from 3 tables in mysql.
My tables are,
theatredet(theaterid,theatername,locationid)
location(locationid,locationname)
screendet(screenname,theaterid,seatsavailable)
I want to display datas from screendet on the basis of theaterid from the table theatredet,i can only able to disply single data from screendet,there are multiple datas on that table with respect of theaterid.
My query is,
string query = "SELECT `theatredetails`.*,`Location`.`LocationName`,
`screendetails`.`ScreenName`,`screendetails`.`SeatsAvailable`
FROM `theatredetails`INNER JOIN `screendetails`
ON `screendetails`.`TheatreDetailsId` = `theatredetails`.`TheatreDetailsId`
INNER JOIN `location` ON `location`.`LocationId`=`theatredetails`.`LocationId`;";
My aspx
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Bind("ScreenName") %>'></asp:TextBox>
<asp:TextBox ID="TextBox7" runat="server" Text='<%#Bind("SeatsAvailable") %>'></asp:TextBox>
Assuming you have already tested your query on MySql and it is returning intended number of rows.
You can populate your listview using the following code (I am using SqlDataSource to bind the ListView, you can use any other method)
<asp:ListView ID="lstviewScreens" runat="server" DataSourceID="ScreensSqlDataSource" >
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" >
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;">
<td>
<strong>Theater Name:</strong><asp:Label ID="lblTheaterName" runat="server" Text='<%# Eval("theatername") %>' />
<br />
<strong>Screen Name:</strong><asp:Label ID="lblScreenName" runat="server" Text='<%# Eval("ScreenName") %>' />
<br />
<strong>Seats Available</strong><asp:Label ID="lblSeatsAvailable" runat="server" Text='<%# Eval("SeatsAvailable") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ScreensSqlDataSource" runat="server"
ConnectionString="Your connection String"
SelectCommand="SELECT theatredetails.*,Location.LocationName,
screendetails.ScreenName,screendetails.SeatsAvailable
FROM theatredetails INNER JOIN screendetails
ON screendetails.TheatreDetailsId = theatredetails.TheatreDetailsId
INNER JOIN location ON location.LocationId=theatredetails.LocationId">
</asp:SqlDataSource>
This will output your data in tabular format, however you can customize LayoutTemplate to change the generated html, even you can use GroupTemplate to group the data.

Calling Procedure in asp.net

I want to call a Procedure to populate drop down in a form. Right now I am using the code like this:
<td class="style4">Choose Category: <font color="red">*</font></td>
<td class="style3">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="cat1"
DataTextField="catname" DataValueField="catname">
</asp:DropDownList>
<asp:SqlDataSource ID="cat1" runat="server" ConnectionString="<%$ ConnectionStrings:billingdatabase%>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [catname] FROM [Category]">
</asp:SqlDataSource>
</td>
The Procedure:
GO
CREATE PROCEDURE [dbo].[spshowproducts]
AS
BEGIN
SELECT catname FROM Category
END
GO
Sorry, I'm not sure I understand your question fully - some more details would be appreciated.
If your User Defined Function is a Stored Procedure you should be able to use that as the basis for the data. Bind your DropDownList to an SqlDataSource, and set the source for the SqlDataSource as the name of the Stored Procedure rather than specifying raw SQL.
Try this:
<td class="style4">Choose Category: <font color="red">*</font></td>
<td class="style3">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="cat1"
DataTextField="catname" DataValueField="catname">
</asp:DropDownList>
<asp:SqlDataSource ID="cat1" runat="server"
ConnectionString="<%$ ConnectionStrings:billingdatabase%>"
ProviderName="System.Data.SqlClient"
SelectCommand="EXEC [dbo].[spshowproducts]"></asp:SqlDataSource>
</td>

Delete row from a programmatically created table

In an asp.net listview I'm putting a table
<ItemTemplate>
<tr>
<td style="width:90%"><asp:TextBox Enabled="false" ID="txtOverall" Text='<%#Eval("Overall") %>' runat="server"></asp:TextBox></td>
<td style="width:10%"><asp:Button ID="cmdDelete" OnClick="DeleteRow" ToolTip='<%#Eval("tooltip") %>' class="alert tiny button" Text="x" runat="server" /></td>
</tr>
</ItemTemplate>
Which basically is a text string and next to it I want a button that will delete the row that the button appears on. The table holds more fields than this but for simplicity this the basic structure. All of the data is entered programmatically. Does anyone know how I can remove a single row from the table?
Thanks,
Craig
You need to handle the delete command
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {
//your deleting code
}
and asp
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT * FROM Orders"
OnDeleting="OnRecordDeleting"
OnDeleted="OnRecordDeleted">
</asp:SqlDataSource>

SortExpression in ASP.NET GridView not working with <HeaderTemplate>

I have an ASP.NET GridView. Now I am adding the SortExpression property tothe <TemplateField> tags, to make specific columns sortable.
Now, one of the columns has some markup content to be added in the header. The problem is, SortExpression does not work if there is a <HeaderTemplate> tag in a <TemplateField>, you have to put it inside the HeaderText property of <TemplateField>. But, all the HTML content does not work properly if I dump it inside the HeaderText property.
<asp:TemplateField SortExpression="FK_TesterID" ItemStyle-Width="300px" FooterStyle-Width="300px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
Tester
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged" ToolTip="Bulk Assign Testers !" ></asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
So you can see, if I put the entire <HeaderTemplate> property inside a headertext, it doesn't work.
But I want to have both functionalities. Can anyone help?
Then you need to provide a control in your HeaderTemplate with CommandName="Sort", for example a LinkButton.
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px"
DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField="PK_ID"
Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged"
ToolTip="Bulk Assign Testers !">
</asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
This is a quite old thread I have stumbled over while trying to solve exactly that described problem but the solution provided here didn't worked for me. If you have a Sorting method defined for the GridView then
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
will call that method
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
dt.DefaultView.Sort = e.SortExpression;
but e.SortExpression will be null and no sorting is happening. You have to first pass the Column's name through the CommandArgument of the LinkButton. Only then it worked in my case!
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" CommandArgument="ColumnName" Text="Sort" />

Resources