.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

ListView DataBound

Posted By:      Posted Date: September 22, 2010    Points: 0   Category :ASP.Net

I am using ListView to display the Data.


I am getting some text or empty string from backend for a particular colummn.

If i got empty string i need to bind with an imageButton in that row.

and if i got a text i need to replace the imagebutton with text.

protected void lv_DataBound(object sender, EventArgs e)






Thanks in Advance.

View Complete Post

More Related Resource Links

Databound Hyperlinks in ListView. Need to open external sites


Good Evening,

This site is ASP.NET 4.0 connected to a SQL 2005 backend.  The database has a table that, among other things, lists the names and URIs of external sites for reference purposes.  A webform has a ListView control bound to an ObjectDataSource that pulls the data from this table.  The ItemTemplate and the AlternatingItemTemplate have been edited so that they contain this HyperLink:

                            Text="Route Map" 
                            NavigateUrl='<%# Eval("Maplink") %>' />

The page renders the data normally but the HyperLinks aren't working as intended.  For example, the datatable record might contain
But when the HyperLink is created in the ListView, it comes out as:
which, of course, fails.

How can I set up the HyperLink to route to the external site and ignore the local domain path?

Thanks for any suggestions. 


trying to select non-existent node in listview databound event icw xmldatasource


Protected Sub repLocationsToCompare_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles repLocationsToCompare.ItemDataBound

Dim enumerable As IEnumerable = System.Web.UI.XPathBinder.Select(e.Item.DataItem, "./str[@name=""thumb""]")

Now, it might be that the current dataitem does NOT contain the <str name="thumb"></str> node. So in that case when I do this:
Dim name As String = enumerable.Cast(Of XmlElement)().Select(Function(x) x.Attributes("name")).FirstOrDefault().Value
the enumerable throws an error of object reference not set to instance of object.

I tried testing it like this:
Dim enumerable As IEnumerable = System.Web.UI.XPathBinder.Select(e.Item.DataItem, "./str[@name=""thumb""]")
if enumerable isnot nothing then
Dim name As String = enumerable.Cast(Of XmlElement)().Select(Function(x) x.Attributes("name")).FirstOrDefault().Value
end if

But still I get the error.

How can I test if the node exists for the current dataitem?

Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView

Prior to ASP.NET 3.5, developers who needed to display a set of records could choose between the GridView, DataList, and Repeater controls. The GridView provides rich data features, but has a very boxy layout; the DataList and Repeater allow for a more flexible layout, but lack the "point and click" data features found in the GridView, DetailsView, and FormView controls. The ListView bridges the gap between layout flexibility and built-in data features.

This article, the first in a series of articles on the ListView and DataPager controls, looks at the ListView's many available templates and illustrates how to display data. Read on to learn more!

WPF Tutorial - Using The ListView, Part 1

Many of the controls in WPF have a downright dizzying array of capabilities and features, due in large part to the composibility of all the components. The ListView control is a great example of this - the possibilities are almost endless. This series of tutorials on the ListView will hopefully make the space of possible options seem not quite as daunting

How to listen to events within a listview



I've got a listview and I've put a button control in one of the columns, I'm trying to figure out how I have listen to the button click within a certain row, but can't see the button in the dropdown for the IDs I can generate events for.

I'd like to get the primary key of the row that's been clicked on and then run a sql statement against it.

Any help would be appreciated.

ListView DataPager with SQL-Statements that already return only paged data.



As far as I understand the ListView-Control and the DataPager-Control, they work like this: The ListView gets its datarows from its data source, then the DataPager causes the ListView to display only a subset of those rows.

So what, when I want to use custom databinding for the ListView, that *already* gets the certain subset from SQL-Server (for better performance). Can I and how can I use the DataPager then?

Thanx, S.

Cutting Edge: ListView Tips and Tricks


This month, use nested ListView controls to create hierarchical views of data and extend the eventing model of the ListView by deriving a custom ListView class.

Dino Esposito

MSDN Magazine April 2008

Programmatically Simulating the ListView's ItemCommand Event


I have a User Control that contains a ListView.  In the user control's code is this event handler:

    protected void listView1_ItemCommand(object source, ListViewCommandEventArgs e)
      Button_ClickHandler(source, e);

What's occurring is that I'm explicitly firing an event so that the parent web page can monitor this and take appropriate actions.  Thus when the user clicks on a different ListViewItem it triggers the population of associated data elsewhere on the web page.

This all works fine except for one thing: When I first load the control I'm pre-setting the first ListViewItem.  Unfortunately, just setting the ListView's SelectedIndex = 0 doesn't fire the aforementioned event handler.

So I started investigating how to call "listView1_ItemCommand" but couldn't figure out how to instantiate the "e" parameter.

Any ideas of how to resolve this?


Dropdownlist in ListView


I have a transaction table with a field called product. There is a limited number of choices for product so I also have a product table. I am showing the transactions in a ListView with a dropdownlist for Product. The field Product can be blank (all works well then). The user can leave the field blank or they can choose from the dropdownlist. I am running into a problem with the dropdownlist in the Edit Template. No matter what the product is in the transaction file, I get a page error of "ddlEditProduct has a SelectedValue which is invalid because it does not exist in the list of items". I have verified the product does exist in the product table.

My markup is:

<asp:DropDownList ID="ddlEditProduct" runat="server"
SelectedValue='<%# Bind("Product") %>' >
<asp:ListItem Selected="True"></asp:ListItem>


using 3.5 and C#

Custom Grid View Header - Grid view Row Created event vs Gridview row databound events ?


Hi Gurus ,

It happened to add an extra Gridveiw Header in row_databound event , It did worked fine on !Postback but disappered on Page.Postback . Quick google search guided me to move the event to Row_Created event and every thing is okay .

Can any expert post some pointers , differnces between grid row_created vs row_databount with some sample table data created dynamically  behaviour of both the events in !Postback and page.Postback .

Thanks a tonne in Advance and  any inputs will be valued and appreciated .

Manually coding controls such as gridview, listview, repeater, etc.


using vs2010 and .net 4

In my project I have a separate DAL built from class files that I'd like to use to store my code for CRUD operations against my database.

I'm having a hard time finding examples of how to code server controls like the gridview, listview, repeater etc., for CRUD operations manually, i.e., without a datasource control of some flavor.

I don't have any problem manually coding the referenced controls to get my data, but I do have problems when it comes to coding them to perform CRUD operations unless I use a datasource control. When I use the datasource controls CRUD operations seem to automagically appear out of nowhere after they're configured.

Can someone point me to some samples somewhere? Have I missed a section at MSDN?


Create databound nested ajax toolkit accordion programatically(In code Behind)


Can anyone guide me in the right direction or give an example of How I would accomplish this?


I have  this so far but kind of stuck on how to do it programtically if I'm using header and content templates 

I have a xmldatasource.. In a nutshell at what point in my code do I need to create a new accordion? does it need to be

in the item_databound handler?


Imports Microsoft.VisualBasic

Public Class AccordionTemplate
    Implements System.Web.UI.ITemplate

    Dim templateType As String

    Sub New(ByVal type As String)
        templateType = type
    End Sub

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
          Implements System.Web.UI.ITemplate.InstantiateIn

        Dim ph As New PlaceHolder()

        Select Case (templateType)
            Case "Header"
                Dim myLabel As Label = New Label()
                myLabel.Text = "Test"
            Case "Content"
                ph.Controls.Add(New LiteralControl("<p>hello</p>"))
        End Select
    End Sub

End Class



Partial Class Accordion
    Inherits System.Web.UI.Page


Rowlimit and PageFirstRow properties of listview as parameters to Listview Webpart


Hi ,

 I need to set the  Rowlimit  and PageFirstRow properties  of  listview  as  parameters to Listview Webpart

 And these parameters  will be passed from the custom provider webpart


name of the list will be provided in custom webpart's toolpane.

and i need to save this as list content type of list defs


Please provide me the guidance on this.



listview 3.5 bind dropdownlists edit item template VB.NET


I am trying to do this with many issues of data binding doubling the amount and not setting selectedValue on dropdownlist. I have read some posts and they help but I am missing a crucial piece. My problem is that my dropdownlist is doubling the amount of records for the dropdownlist. Instead of "YES;NO;SELECT A VALUE", I am getting "YES;NO;SELECT A VALUE;YES;NO;YES;NO". My database is correct with the UPDATE part but the rebind after update is failing horribly.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Session("ACTIVETAB").ToString() = Session("SUBCONTRACTSATABNO").ToString() Then
            End If
        End If
    End Sub

    Public Function loadlookups(ByVal id As String, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) As DataTable
        Select Case id
            Case "ddlCertReceived"
                dt = GetLookupTable("SubcontractsA", "Sub 1 Cert Received")
                Dim ddlCertReceived As DropDownList = _
                CType(e.Item.FindControl("ddlCertReceived"), DropDownList)
                ddlCertReceived.DataSource = dt
                ddlCertReceived.DataTextField = "lup_text"

Accessing Label Control In a listview using Javascript



I have a Button and Label nested inside a ListView.

When a User clicks the button i want the Label to display a specific value eg"Pass". Of course being a Listview the Label value will be relevant to the row the button was clicked on.

I want all this to happen on client side, but i am struggling to access the Label via my Javascript??

Any suggestions and examples Would be much appreciated..

2 Databound Dropdownlists... how to bound data to the 2nd one?


hi there,
I have a database with the following table:

Table Job {
    nvarchar(50) JobID,
    nnarchar(50) JobName,
    Bit/Bool     Active;

I'm trying to build an .aspx page with two dropdownlists:

  1. The first will display the JobIDs of all Jobs available in the Database (I have done this part and it's working)
  2. The second will display by default the value "Choose Job first".

  What I'm trying to do is: Once the user chooses the JobID (step 1), the page should reload and the 2nd dropdownlist should display the following:

  •  if the value of the field "Active" of the corresponding JobID is '1'(i.e, the Job is already active) -> then the Dropdownlist should display "Already active" and "Deactivate" values
  • if the value of the field "Active" of the corresponding JobID is '0'(i.e, the Job is inactive) -> then the Dropdownlist should display "Activate" and "Already Deactive" values.

This is my code:


<div class="formField JobSelect">
<asp:Label ID="lbl_J

Dynamic gallery using ListView and AJAX


I want to create a gallery where on top there will be a normal view of a selected image and below a horizontal listview in order to select an image. Both in separate ajax update panels.

When i debug i get the following error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" and highlights the "ImageButton imgbtn =(ImageButton)ListView1.Items[ListView1.SelectedIndex].FindControl("ImageButton2");" of my code behind file

Can anyone help me? Thank you in advance!

Here is my code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend