how to make a new button in ListView? - asp.net

I need to make an image button in the layout template of ListView,
when i click on the button it should open the insert template so i can insert a new record
here is my ListView:
<asp:ListView ID="LVCategories" runat="server" DataKeyNames="CatID" DataSourceID="CategoriesDS" EnableModelValidation="True">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
</td>
<td>
<asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionLabel" runat="server" Text='<%# Eval("CatDescription") %>' />
</td>
<td>
<asp:Label ID="CatTitleArLabel" runat="server" Text='<%# Eval("CatTitleAr") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionArLabel" runat="server" Text='<%# Eval("CatDescriptionAr") %>' />
</td>
<td>
<asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
</td>
<td>
<asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Eval("Published") %>' Enabled="false" />
</td>
<td>
<asp:Label ID="DisplayOrderLabel" runat="server" Text='<%# Eval("DisplayOrder") %>' />
</td>
<td>
<asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CatIDLabel1" runat="server" Text='<%# Eval("CatID") %>' />
</td>
<td>
<asp:TextBox ID="CatTitleTextBox" runat="server" Text='<%# Bind("CatTitle") %>' />
</td>
<td>
<asp:TextBox ID="CatDescriptionTextBox" runat="server" Text='<%# Bind("CatDescription") %>' />
</td>
<td>
<asp:TextBox ID="CatTitleArTextBox" runat="server" ext='<%# Bind("CatTitleAr") %>' />
</td>
<td>
<asp:TextBox ID="CatDescriptionArTextBox" runat="server" Text='<%# Bind("CatDescriptionAr") %>' />
</td>
<td>
<asp:TextBox ID="PictureIDTextBox" runat="server" Text='<%# Bind("PictureID") %>' />
</td>
<td>
<asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Bind("Published") %>' />
</td>
<td>
<asp:TextBox ID="DisplayOrderTextBox" runat="server" Text='<%# Bind("DisplayOrder") %>' />
</td>
<td>
<asp:TextBox ID="CreatedOnTextBox" runat="server" Text='<%# Bind("CreatedOn") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.
</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="CatIDTextBox" runat="server" Text='<%# Bind("CatID") %>' />
</td>
<td>
<asp:TextBox ID="CatTitleTextBox" runat="server" Text='<%# Bind("CatTitle") %>' />
</td>
<td>
<asp:TextBox ID="CatDescriptionTextBox" runat="server" Text='<%# Bind("CatDescription") %>' />
</td>
<td>
<asp:TextBox ID="CatTitleArTextBox" runat="server" Text='<%# Bind("CatTitleAr") %>' />
</td>
<td>
<asp:TextBox ID="CatDescriptionArTextBox" runat="server" Text='<%# Bind("CatDescriptionAr") %>' />
</td>
<td>
<asp:TextBox ID="PictureIDTextBox" runat="server" Text='<%# Bind("PictureID") %>' />
</td>
<td>
<asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Bind("Published") %>' />
</td>
<td>
<asp:TextBox ID="DisplayOrderTextBox" runat="server" Text='<%# Bind("DisplayOrder") %>' />
</td>
<td>
<asp:TextBox ID="CreatedOnTextBox" runat="server" Text='<%# Bind("CreatedOn") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
</td>
<td>
<asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionLabel" runat="server" Text='<%# Eval("CatDescription") %>' />
</td>
<td>
<asp:Label ID="CatTitleArLabel" runat="server" Text='<%# Eval("CatTitleAr") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionArLabel" runat="server" Text='<%# Eval("CatDescriptionAr") %>' />
</td>
<td>
<asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
</td>
<td>
<asp:CheckBox ID="PublishedCheckBox" runat="server" Checked='<%# Eval("Published") %>' Enabled="false" />
</td>
<td>
<asp:Label ID="DisplayOrderLabel" runat="server" Text='<%# Eval("DisplayOrder") %>' />
</td>
<td>
<asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
<asp:ImageButton ID="btnNew" runat="server" CommandName="New" />
<%--<asp:ImageButton ID="NewCatbtn" runat="server" CommandName="Insert" ImageUrl="~/ADMIN/themes/Img/New.png" AlternateText="Click Here To Add New" OnClick="OpenTheInsertTemplates"/>--%>
</th>
<th runat="server">CatTitle</th>
<th runat="server">CatDescription</th>
<th runat="server">CatTitleAr</th>
<th runat="server">atDescriptionAr</th>
<th runat="server">PictureID</th>
<th runat="server">Published</th>
<th runat="server">DisplayOrder</th>
<th runat="server">CreatedOn</th>
</tr>
<tr ID="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="CatIDLabel" runat="server" Text='<%# Eval("CatID") %>' />
</td>
<td>
<asp:Label ID="CatTitleLabel" runat="server" Text='<%# Eval("CatTitle") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionLabel" runat="server"
Text='<%# Eval("CatDescription") %>' />
</td>
<td>
<asp:Label ID="CatTitleArLabel" runat="server"
Text='<%# Eval("CatTitleAr") %>' />
</td>
<td>
<asp:Label ID="CatDescriptionArLabel" runat="server"
Text='<%# Eval("CatDescriptionAr") %>' />
</td>
<td>
<asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>' />
</td>
<td>
<asp:CheckBox ID="PublishedCheckBox" runat="server"
Checked='<%# Eval("Published") %>' Enabled="false" />
</td>
<td>
<asp:Label ID="DisplayOrderLabel" runat="server"
Text='<%# Eval("DisplayOrder") %>' />
</td>
<td>
<asp:Label ID="CreatedOnLabel" runat="server" Text='<%# Eval("CreatedOn") %>' />
</td>
</tr>
</SelectedItemTemplate>

<asp:Button runat="server" ID="cmdInsert" CommandName="Insert" Text="Insert" />
Protected Sub ListView1_ItemInserting (ByVal sender As Object, ByVal e as ListViewEditEventArgs)
' ... '
End Sub

this example has been solved like this :
ASPX file:
<th runat="server">
<asp:ImageButton ID="ImageButton1" runat="server" Text="new" CommandName="new" ImageUrl="~/ADMIN/themes/Img/DeleteBtn.png" />
</th>
the VB.net Code:
Sub deletedd(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
Try
If e.CommandName.Equals("Delete") Then
'this to take a value from any control
Dim Idlabel As Label = e.Item.FindControl("ImageIDLabel")
Session("ID") = Idlabel.Text()
End If
If e.CommandName.Equals("new") Then
'Other code
ListView1.InsertItemPosition = InsertItemPosition.FirstItem
End If
If e.CommandName.Equals("Cancel") Then
'Other code
ListView1.InsertItemPosition = InsertItemPosition.None
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Related

Validate CheckBox inside listview in asp.net not working if listview have more than one record

I want to validate at least one checkbox select when edit the listview
when the list view have more than one record the validation is not working but its work if the listview have one record.
I think my code not detect the edited record, when its more than one.
what is the problem with this code can some one help with it?
Protected Sub UpdateButton_Click(sender As Object, e As EventArgs)
'get data from chekced checkbox
Dim ChkValue As New List(Of String)()
For Each item As ListViewItem In ListView1.Items
Dim ck1 As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
Dim ck2 As CheckBox = DirectCast(item.FindControl("CheckBox2"), CheckBox)
Dim ck3 As CheckBox = DirectCast(item.FindControl("CheckBox3"), CheckBox)
Dim vl As RequiredFieldValidator = DirectCast(item.FindControl("RequiredFieldValidator1"), RequiredFieldValidator)
If ck1.Checked Or ck2.Checked Or ck3.Checked Then
vl.Enabled = False
sdstt.Update()
Else
vl.Enabled = True
End If
Next
End Sub
<asp:ListView ID="ListView1" runat="server" DataSourceID="sdstt" DataKeyNames="ID" >
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" CausesValidation="true" ValidationGroup="vgrpSaveContact" CommandName="Update" OnClick="UpdateButton_Click" runat="server" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
</td>
<td>
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<asp:RequiredFieldValidator Font-Size="6pt" ID="RequiredFieldValidator3" runat="server" ControlToValidate="nameTextBox"
ErrorMessage="first name" SetFocusOnError="true" ValidationGroup="vgrpSaveContact"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revOnlyAlphabetical" runat="server" Font-Size="6pt" ValidationGroup="vgrpSaveContact"
ValidationExpression="^([^0-9]+)$" ControlToValidate="nameTextBox"
ErrorMessage="Invalid Name" Font-Bold="true" ForeColor="Red"></asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' />
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("chk1") %>' />
</td>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("chk2") %>' />
</td>
<td>
<asp:CheckBox ID="chkCheckBox3" runat="server" Checked='<%# Bind("chk3") %>' />
<asp:TextBox ID="txtchk" runat="server" Visible="false" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGroup="vgrpSaveContact" Enabled="false" runat="server"
ControlToValidate="txtchk" ErrorMessage="RequiredFieldValidator">
</asp:RequiredFieldValidator>
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
</td>
<td>
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>' />
</td>
<td>
<asp:CheckBox ID="chkCheckBox" runat="server" Checked='<%# Eval("chk") %>' Enabled="false" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server"></th>
<th runat="server">ID</th>
<th runat="server">name</th>
<th runat="server">age</th>
<th runat="server">chk</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>

Showing same table to different roles with different options

Currently I just post the same code in different pages for each role, a repeater with a table like this one but each role has different options to do. I tried to place in loginview separator but didn't work, i don't know how to iterate through login view controls and the code just started getting dirtier more than just doing visible = false / true. Is there a more effective way to do this than just making new pages?
<asp:Repeater ID="rptArtigos" runat="server" OnItemCommand="rptArtigos_OnItemCommand" OnItemDataBound="rptArtigos_OnItemDataBound">
<HeaderTemplate>
<table id="tblArtigos" class="table table-bordered dataTable text-center">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Ref. Cliente</th>
<th>Ref. Interna</th>
<th>Nome</th>
<th>Estado</th>
<th>Válido Logística</th>
<th>Data Criação</th>
<th></th>
<th></th>
<th id="cell1" runat="server"></th>
<th></th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td>
<asp:Label ID="lblIdArtigo" runat="server" Text='<%# Eval("IdArtigo") %>' />
</td>
<td>
<asp:Label ID="lblRefCliente" runat="server" Text='<%# Eval("ReferenciaCliente") %>' />
</td>
<td>
<asp:Label ID="lblRefInterna" runat="server" Text='<%# Eval("ReferenciaInterna") %>' />
</td>
<td>
<asp:LinkButton ID="lkbtnNome" runat="server" Text='<%# Eval("Nome") %>' CommandName="Ver" CommandArgument='<%# Eval("IdArtigo") %>'></asp:LinkButton>
</td>
<td>
<asp:Label ID="lblEstado" runat="server" Text='<%# Eval("EstadoArtigo") %>' />
</td>
<td>
<asp:Label ID="lblAprovadoLogistica" runat="server" Text='<%# Eval("AprovadoLogistica") %>' />
</td>
<td>
<asp:Label ID="lblDataCriacao" runat="server" Text='<%# Eval("DataCriacao") %>' />
</td>
<td class="text-center">
<asp:ImageButton ImageUrl="/Images/Icones/copy.png" Width="25" runat="server" CommandName="Copiar" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
<td class="text-center">
<asp:ImageButton ImageUrl="/Images/Buttons/edit.png" Width="25" runat="server" CommandName="Editar" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
<td class="text-center">
<asp:ImageButton ImageUrl="/Images/Buttons/success.png" Width="25" runat="server" OnClientClick="return false;" data-toggle="modal" data-target="#validate" CommandName="Validar" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
<td class="text-center">
<asp:ImageButton ImageUrl="/Images/Buttons/x-button.png" Width="25" runat="server" CommandName="Rejeitar" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

why does my asp.net listview not show latest inserted record?

I'm just trying to get accustomed to LVs and I just threw this one together. I was under the impression that the LV would handle the inserting and refreshing of the LV after the insert but it doesn't appear to be the case. I even added the LV.rebind() and still nothing..
I get the LV to refresh/rebind properly after the first insert but subsequent inserts does not produce a refreshed LV. I have confirmed that the records are inserted properly into the database.
Here is my code so far, do I
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductID" EnableViewState="true"
datasourceid="sqlDS" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<tr style="">
<td>
</td>
<td>
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:Label ID="CategoryLabel" runat="server" Text='<%# Eval("Category") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("Cost") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:Label ID="ProductIDLabel1" runat="server"
Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:TextBox ID="CategoryTextBox" runat="server"
Text='<%# Bind("Category") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
</td>
<td>
<asp:TextBox ID="CategoryTextBox" runat="server"
Text='<%# Bind("Category") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
</td>
<td>
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:Label ID="CategoryLabel" runat="server" Text='<%# Eval("Category") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("Cost") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
</th>
<th runat="server">
ProductID</th>
<th runat="server">
Category</th>
<th runat="server">
Description</th>
<th runat="server">
Cost</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="sqlDS" runat="server"
DataSourceMode="DataSet" EnableCaching="true"
ConnectionString="<%$ ConnectionStrings:ProjectDashboardConnectionString %>"
SelectCommand="usp_CafeteriaGetProducts" SelectCommandType="StoredProcedure"
FilterExpression="Category='A'"
InsertCommand="usp_CafeteriaInsertProducts" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Category" Type="String" />
<asp:Parameter Name="Cost" Type="Decimal" />
</InsertParameters>
</asp:SqlDataSource>
code beind:
Public Class WebForm3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ListView1_ItemInserted(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs) Handles ListView1.ItemInserted
sqlDS.Dispose()
sqlDS.DataBind()
ListView1.Dispose()
ListView1.DataSourceID = "sqlDS"
ListView1.DataBind()
End Sub
Protected Sub sqlProducts_ItemInserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles sqlDS.Inserting
''Do something
End Sub
Protected Sub sqlProducts_ItemInserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) Handles sqlDS.Inserted
''Do something
End Sub
End Class
any ideas?
Thanks.
It was because of the sqldatasource property EnableCaching being set to "true". Removed that property completely as the default is False.

ASP.NET and ListView control having LinkButton

I have 10 questions in my sql database. I get them and print them in Link form using LinkButton. Now i want to generate click event of every question separately.
How to generate OnClick event of LinkButton inside ListView. Please help in easy steps.
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="userid" >
<AlternatingItemTemplate>
<tr style="">
<td>
<%-- <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
--%>
<h3> <asp:LinkButton CommandArgument='<%# Eval("userid") %>' ID="LinkButton1" runat="server"><%# Eval("qt") %></asp:LinkButton> </h3>
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
</td>
<td>
<asp:Label ID="useridLabel1" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
</td>
<td>
<asp:TextBox ID="useridTextBox" runat="server" Text='<%# Bind("userid") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<h3> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("userid") %>' runat="server"><%# Eval("qt") %></asp:LinkButton> </h3>
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">qt</th>
<th runat="server">userid</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT [qt], [userid] FROM [java]"></asp:SqlDataSource>
ASPX:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="userid" onitemcommand="ListView1_ItemCommand" >
<AlternatingItemTemplate>
<tr style="">
<td>
<%-- <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
--%>
<h3> <asp:LinkButton CommandArgument='<%# Eval("userid") %>' ID="LinkButton1" runat="server"><%# Eval("qt") %></asp:LinkButton> </h3>
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
</td>
<td>
<asp:Label ID="useridLabel1" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
</td>
<td>
<asp:TextBox ID="useridTextBox" runat="server" Text='<%# Bind("userid") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<h3> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("userid") %>' runat="server"><%# Eval("qt") %></asp:LinkButton> </h3>
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">qt</th>
<th id="Th2" runat="server">userid</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
</td>
<td>
<asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
SelectCommand="SELECT [qt], [userid] FROM [java]"></asp:SqlDataSource>
Code Behind:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string userid = e.CommandArgument.ToString();
Response.Write("U:" + userid);
if (userid == "USER_3730")
{
Response.Redirect("bnbnbnbnbn.aspx");
}
if (userid == "USER_b163")
{
Response.Redirect("home.aspx");
}
}

AlternateTemplate is not working for Listview

I modified Listview as per my need,but now is not working, in means, its showing row with different color but repeating same data. And then coming next data, againg its repeating and going on.... Here I am lisitng my code.
for Example,
1
1 (Repeating, Different Color row)
2
2 (Repeating, Different Color row)
3
3 (Repeating, Different Color row)
.
.
.
.
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="uniqueno">
<ItemTemplate>
<tr style="background-color:#DCDCDC;color: #000000;" runat="server">
<td>
<asp:Label ID="policeNumLabel" runat="server" Text='<%# Eval("policeNum") %>' />
</td>
<td>
<asp:Label ID="DFirstNameLabel" runat="server" Text='<%# String.Format("{0} {1} {2}", Eval("DFirstName"),Eval("DMidName"),Eval("DSurName")) %>' />
</td>
<td>
<asp:Label ID="DLicenceNoLabel" runat="server"
Text='<%# Eval("DLicenceNo") %>' />
</td>
<td>
<asp:Label ID="ManufacturerLabel" runat="server"
Text='<%# Eval("Manufacturer") %>' />
</td>
<td>
<asp:Label ID="SerialNumLabel" runat="server" Text='<%# Eval("SerialNum") %>' />
</td>
<td>
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
</td>
<td>
<asp:Label ID="CalibreLabel" runat="server" Text='<%# Eval("Calibre") %>' />
</td>
<td>
<asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>' />
</td>
<td>
<asp:Label ID="ActionLabel" runat="server" Text='<%# Eval("Action") %>' />
</td>
<td>
<asp:Label ID="BarrelLengthLabel" runat="server"
Text='<%# Eval("BarrelLength") %>' />
</td>
<td>
<asp:Label ID="ReasonLabel" runat="server" Text='<%# Eval("Reason") %>' />
</td>
<td>
<asp:Label ID="OtherLabel" runat="server" Text='<%# Eval("Other") %>' />
</td>
<td>
<asp:Label ID="TransferPendingLabel" runat="server" Text='<%# Eval("TransferPending") %>' />
</td>
<td>
<asp:Label ID="OFirstNameLabel" runat="server"
Text='<%# String.Format("{0} {1} {2}", Eval("OFirstName"),Eval("OmidName"),Eval("OSurName")) %>' />
</td>
<td>
<asp:Label ID="O_LicenceNoLabel" runat="server"
Text='<%# Eval("O_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="DateAcqLabel" runat="server" Text='<%# Eval("DateAcq") %>' />
</td>
<td>
<asp:Label ID="Dis_FirstnameLabel" runat="server"
Text='<%# String.Format("{0} {1} {2}",Eval("Dis_Firstname"),Eval("Dis_Midname"),Eval("Dis_Surname")) %>' />
</td>
<td>
<asp:Label ID="Dis_LicenceNoLabel" runat="server"
Text='<%# Eval("Dis_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="Dis_DisposaldateLabe" runat="server"
Text='<%# Eval("Dis_Disposaldate") %>' />
</td>
<td>
<asp:Label ID="Dis_CommentLabel" runat="server"
Text='<%# Eval("Dis_Comment") %>' />
</td>
<td>
<asp:Label ID="PermittoAcqNoLabel" runat="server" Text='<%# Eval("PermittoAcqNo") %>' />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr id="Tr1" style="background-color:#DCDCDC;color: #000000;" runat="server">
<td>
<asp:Label ID="policeNumLabel" runat="server" Text='<%# Eval("policeNum") %>' />
</td>
<td>
<asp:Label ID="DFirstNameLabel" runat="server" Text='<%# String.Format("{0} {1} {2}", Eval("DFirstName"),Eval("DMidName"),Eval("DSurName")) %>' />
</td>
<td>
<asp:Label ID="DLicenceNoLabel" runat="server"
Text='<%# Eval("DLicenceNo") %>' />
</td>
<td>
<asp:Label ID="ManufacturerLabel" runat="server"
Text='<%# Eval("Manufacturer") %>' />
</td>
<td>
<asp:Label ID="SerialNumLabel" runat="server" Text='<%# Eval("SerialNum") %>' />
</td>
<td>
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
</td>
<td>
<asp:Label ID="CalibreLabel" runat="server" Text='<%# Eval("Calibre") %>' />
</td>
<td>
<asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>' />
</td>
<td>
<asp:Label ID="ActionLabel" runat="server" Text='<%# Eval("Action") %>' />
</td>
<td>
<asp:Label ID="BarrelLengthLabel" runat="server"
Text='<%# Eval("BarrelLength") %>' />
</td>
<td>
<asp:Label ID="ReasonLabel" runat="server" Text='<%# Eval("Reason") %>' />
</td>
<td>
<asp:Label ID="OtherLabel" runat="server" Text='<%# Eval("Other") %>' />
</td>
<td>
<asp:Label ID="TransferPendingLabel" runat="server" Text='<%# Eval("TransferPending") %>' />
</td>
<td>
<asp:Label ID="OFirstNameLabel" runat="server"
Text='<%# String.Format("{0} {1} {2}", Eval("OFirstName"),Eval("OmidName"),Eval("OSurName")) %>' />
</td>
<td>
<asp:Label ID="O_LicenceNoLabel" runat="server"
Text='<%# Eval("O_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="DateAcqLabel" runat="server" Text='<%# Eval("DateAcq") %>' />
</td>
<td>
<asp:Label ID="Dis_FirstnameLabel" runat="server"
Text='<%# String.Format("{0} {1} {2}",Eval("Dis_Firstname"),Eval("Dis_Midname"),Eval("Dis_Surname")) %>' />
</td>
<td>
<asp:Label ID="Dis_LicenceNoLabel" runat="server"
Text='<%# Eval("Dis_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="Dis_DisposaldateLabe" runat="server"
Text='<%# Eval("Dis_Disposaldate") %>' />
</td>
<td>
<asp:Label ID="Dis_CommentLabel" runat="server"
Text='<%# Eval("Dis_Comment") %>' />
</td>
<td>
<asp:Label ID="PermittoAcqNoLabel" runat="server" Text='<%# Eval("PermittoAcqNo") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>
No data found</td>
</tr>
</table>
</EmptyDataTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" >
<th colspan="3" runat="server">Dealer Info </th>
<th colspan="7" runat="server">Firearm Details </th>
<th colspan="3" runat="server">Reason </th>
<th colspan="3" runat="server">Acquisition Details </th>
<th colspan="5" runat="server">Disposal Details </th>
</tr>
<tr runat="server" style="background-color:#DCDCDC;color: #000000;">
<th runat="server">Number</th>
<th runat="server">Name</th>
<th runat="server">Licence #</th>
<th runat="server">Manufacturer</th>
<th runat="server">Serial #</th>
<th runat="server">Model</th>
<th runat="server">Calibre</th>
<th runat="server">Type</th>
<th runat="server">Action</th>
<th runat="server">Barrel Length</th>
<th runat="server">Reason</th>
<th runat="server">Other</th>
<th runat="server">Transfer Pending</th>
<th runat="server">Name</th>
<th runat="server">Licence #</th>
<th runat="server">Date of Acquired</th>
<th runat="server">Name</th>
<th runat="server">Licence #</th>
<th runat="server">Date of Disposal</th>
<th runat="server">Comment</th>
<th runat="server">Permit to Acquire No</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
<td>
<asp:Label ID="policeNumLabel" runat="server" Text='<%# Eval("policeNum") %>' />
</td>
<td>
<asp:Label ID="DFirstNameLabel" runat="server"
Text='<%# Eval("DFirstName") %>' />
</td>
<td>
<asp:Label ID="DMidNameLabel" runat="server" Text='<%# Eval("DMidName") %>' />
</td>
<td>
<asp:Label ID="DSurNameLabel" runat="server" Text='<%# Eval("DSurName") %>' />
</td>
<td>
<asp:Label ID="DLicenceNoLabel" runat="server"
Text='<%# Eval("DLicenceNo") %>' />
</td>
<td>
<asp:Label ID="Dis_DisposaldateLabel" runat="server"
Text='<%# Eval("Dis_Disposaldate") %>' />
</td>
<td>
<asp:Label ID="SerialNumLabel" runat="server" Text='<%# Eval("SerialNum") %>' />
</td>
<td>
<asp:Label ID="CalibreLabel" runat="server" Text='<%# Eval("Calibre") %>' />
</td>
<td>
<asp:Label ID="BarrelLengthLabel" runat="server"
Text='<%# Eval("BarrelLength") %>' />
</td>
<td>
<asp:Label ID="ManufacturerLabel" runat="server"
Text='<%# Eval("Manufacturer") %>' />
</td>
<td>
<asp:Label ID="OtherLabel" runat="server" Text='<%# Eval("Other") %>' />
</td>
<td>
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
</td>
<td>
<asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>' />
</td>
<td>
<asp:Label ID="ActionLabel" runat="server" Text='<%# Eval("Action") %>' />
</td>
<td>
<asp:Label ID="TransferPendingLabel" runat="server"
Text='<%# Eval("TransferPending") %>' />
</td>
<td>
<asp:Label ID="DateAcqLabel" runat="server" Text='<%# Eval("DateAcq") %>' />
</td>
<td>
<asp:Label ID="OFirstNameLabel" runat="server"
Text='<%# Eval("OFirstName") %>' />
</td>
<td>
<asp:Label ID="OmidNameLabel" runat="server" Text='<%# Eval("OmidName") %>' />
</td>
<td>
<asp:Label ID="OSurNameLabel" runat="server" Text='<%# Eval("OSurName") %>' />
</td>
<td>
<asp:Label ID="O_LicenceNoLabel" runat="server"
Text='<%# Eval("O_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="DNoLabel" runat="server" Text='<%# Eval("DNo") %>' />
</td>
<td>
<asp:Label ID="StreetLabel" runat="server" Text='<%# Eval("Street") %>' />
</td>
<td>
<asp:Label ID="column1Label" runat="server" Text='<%# Eval("column1") %>' />
</td>
<td>
<asp:Label ID="StateLabel" runat="server" Text='<%# Eval("State") %>' />
</td>
<td>
<asp:Label ID="PostCodeLabel" runat="server" Text='<%# Eval("PostCode") %>' />
</td>
<td>
<asp:Label ID="PermittoAcqNoLabel" runat="server"
Text='<%# Eval("PermittoAcqNo") %>' />
</td>
<td>
<asp:Label ID="Dis_FirstnameLabel" runat="server"
Text='<%# Eval("Dis_Firstname") %>' />
</td>
<td>
<asp:Label ID="Dis_MidnameLabel" runat="server"
Text='<%# Eval("Dis_Midname") %>' />
</td>
<td>
<asp:Label ID="Dis_SurnameLabel" runat="server"
Text='<%# Eval("Dis_Surname") %>' />
</td>
<td>
<asp:Label ID="Dis_LicenceNoLabel" runat="server"
Text='<%# Eval("Dis_LicenceNo") %>' />
</td>
<td>
<asp:Label ID="Dis_DnoLabel" runat="server" Text='<%# Eval("Dis_Dno") %>' />
</td>
<td>
<asp:Label ID="Dis_StreetLabel" runat="server"
Text='<%# Eval("Dis_Street") %>' />
</td>
<td>
<asp:Label ID="column2Label" runat="server" Text='<%# Eval("column2") %>' />
</td>
<td>
<asp:Label ID="Dis_stateLabel" runat="server" Text='<%# Eval("Dis_state") %>' />
</td>
<td>
<asp:Label ID="Dis_postcodeLabel" runat="server"
Text='<%# Eval("Dis_postcode") %>' />
</td>
<td>
<asp:Label ID="Dis_CommentLabel" runat="server"
Text='<%# Eval("Dis_Comment") %>' />
</td>
<td>
<asp:Label ID="ReasonLabel" runat="server" Text='<%# Eval("Reason") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
There was no fault with code....
After having lots of experiment, I realise that there something wrong with my Database, and that's what happened, there was duplicate existed which were removed and now listview is work fine.

Resources