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

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

Programmatically Simulating the ListView's ItemCommand Event

Posted By:      Posted Date: August 23, 2010    Points: 0   Category :ASP.Net

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?


View Complete Post

More Related Resource Links

Adding client event handlers to an ASP.NET controls programmatically



I want to add client event handlers to ASP.NET controls (For example: GridView/Button ) programmatically to ASP.NET.

I am aware of "onclick" event handler, so my code would be

GridView1.Attributes.Add("onclick", "JSGridViewFunction()");

Button1.Attributes.Add("onclick", "JSButtonFunction()");

I want to know what are all the event handlers that are available for (or that can be added to)  ASP.NET controls.

For example, I want to add "onsort" event handler to ASP.NET GridView control.

But the following code fails

GridView1.Attributes.Add("onsort", "JSGridViewFunction()");

Please help.


listview with button in item template - how do I handle the click event?

Hello,Using a listview control for the first time... I need a button for each item, and of course I need to handle the click event for the buttons... upon clicking a particular button I of course need to know which item the button is associated with so the appropriate action can be taken...In VS, I can't select the button and see my list of available events in the properties window like is normally done for controls outside of templates like this... what do I need to do?

Drop Down Listbox SelectedIndexChanged Event not firing when I programmatically change selection

I have two drop down listboxes (DDL1 and DDL2).  When DDL1 has an index change, DDL2 then has a refresh of list items based off SQL Server SELECT from a table.  The database isn't the problem.  Everything works perfectly when the user selects a new item manually from DDL1.But in my code, I save the user data from the selection of DDL1 in a separate table.  And then upon rendering of the page (Page_PreRenderComplete), I do a SelectedIndex assignment of the value of what's saved in the db table.  That works just fine, it selects the DDL1 item, but DDL2 is unaffected, as if DDL1's SelectedIndexChanged event isn't called like it would be if manually changed.  How can I get around this?Thanks in advance. :)

How to update the "ListView.SelectedListViewItemCollection" programmatically in C# ?

How to modify the contents of "ListView.SelectedListViewItemCollection" programmatically ? Mouse clicks in a ListView are recorder in this collection. Can I access to the collection programmatically ? If YES, please provide me with some code samples. ListView.SelectedListViewItemCollection selectedItems = this.listViewFile.SelectedItems; Thanks

Programmatically Web part Event Handler Development

Dear Friends,   I am Manoj a fresher in Sharepoint, now I want to develop a program for Event Handler for a web part but In my office server have the share point only, but my system have Visual Studio I am unable to develop the application can I develop the Client System or not, If I can develop the client system then how possible can anyone say regarding this matter its very urgent for me. Advance thanks……………………

Programmatically generated AsyncFileUpload does not fire UploadedComplete event


The AsyncFileUpload control turns green, indicating that the attachment was successfully uploaded, but the designated method for handling the server side UploadedComplete event is never executed.

ListView, DataPager, LinqDataSource: LinqDataSource Selecting event fire twice


Dear all,

In my web app, I used LinqDataSource, ListView and DataPager (.NET 3.5 sp1) to implement search feature with paging. I have a button to trigger the search.

But In the first time, the Selecting event of LinqDataSource fire twice and I don't know why (I debugged my code very carefully). I don't use QueryString with DataPager and assign PageSize of DataPager in the first time of page load to prevent the ListView bind again (as in some instructions I found in forum)

Have anyone seen this problem before? And If possible, can you give me some instructions to try?

I can't post my code because it's quite large.

Thank you very much


Developer, CSC Vietnam


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?

Selection Changed event is not firing for listview



     In my listview i have two columns, once is check box and another one is normal column. if i select in normal column, listview's selection changed event is fring, but when i select check box(This checkbox is inside the datatemplate) listview's selection changed event is not firing. Can any one tell me how i can achieve it. If you are not understand, please give your email id, i will send my sample application to you.



Udayakumar S

Problem with ListView' SelectionChanged event


I have a listview (C#) with some items representing metadata objects. Near this listview I have a Panel where the structure of selected metadata object is shown.

When user selects one item I want to show my Panel (with structure of this item).

When user doesn't select any item or selects many items I want to hide my Panel.

It works but there are a lot of blinks during selection changes. I've prepared workaround to this issue by checking in SelectionChanged event whether the number of selected items changed (none -> one, one -> many, many -> one, one -> none). If the number changed so I show or hide the panel, in other cases I don't do anything.

But one blink is still here. When I just move one-item selection from one item to another then SelectionChanged occures 2 times (for previous selected item and then for next selected item). So my check works incorrectly in this case: one item selection changes to none selection (first SelectionChanged event) and panel becomes hidden, then none selection changes to one item selection (second SelectionChanged event) and panel becomes shown. So user sees blinks.

How can I detect whether user deselected all items at all or just moved the selection from one item to another.

Thanks a lot. Sorry for my English:)

What is equivalent of listview_ItemCreated event for WPF listview ?


What is the event for WPF listview itemcreted / iteminserted

(like normal listview_Itemcreated() )




listView - ItemDataBound event - problem casting to DataRowView to get to underlying data?


System.InvalidCastException was unhandled by user code
  Message="Unable to cast object of type 'WebSTAR.JobListItem' to type 'System.Data.DataRowView'."
       at WebSTAR.Default.ListViewRecentJobs_ItemDataBound(Object sender, ListViewItemEventArgs e) in C:\Users\djc\Documents\Visual Studio 2008\Projects\WebSTAR\WebSTAR\Default.aspx.cs:line 255
       at System.Web.UI.WebControls.ListView.OnItemDataBound(ListViewItemEventArgs e)

ListView ItemDataBound Event



I want to access a dropdown control value of each list item as soon as it is created. I tried with ItemCreated and ItemDataBound events but I am getting null contro when I try to access control as shown below:

ListViewDataItem item = (ListViewDataItem)e.Item;

int LstIndex = item.DataItemIndex;

RadComboBox ddlCategory = (RadComboBox)(listView1.Items[LstIndex].FindControl("rdComboCategory"));

Also, I get listview.item.count zero. IS there any event where I will get listitem count and list items?

ListView Control, MouseUp event fires twice, once MouseUp and then on MouseHover.


Greetings all,

using: VS2010, Frwk 4.0, Windows7 Ultimate N.

When the ListView's MultiSelect property is set to False, MouseUp is fired twice with one click.

The event is fired when the mouse button is up (which is normal), but then fired again when you move the mouse.

This bug has been reported in Microsoft Connect on June 29 2005,  here... and reported fixed. Its not.


Is there any kind of workaround for this?


thanks in advance for suggestions/ideas here.


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!

Export SharePoint List to Excel Spreadsheet Programmatically using C#

In SharePoint applications, Custom Lists are used to store business data and Document Libraries to store the documents. But for data manupulation and analysis, Microsoft Excel provides very rich features as compared to SharePoint Lists. That's why people still loves to work on Microsoft Excel Sheets.

Creating an Excel Spreadsheet programmatically using VB.NET

The Interoperability services make it very easy to work with COM Capable Applications such as Word and Excel. This article reveals using Excel from a managed application. Excel is the spreadsheet component of Microsoft Office 2000. The majority of Excel programmatic functionality is exposed through Automation via the type library Excel9.olb. The intention of this article is to express that a managed application can interrelate with Excel as a COM 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