.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

ListBox with buttons - findout which one was pushed

Posted By:      Posted Date: October 27, 2010    Points: 0   Category :WPF



I have class like this :

public class A
  private readonly string _name;
  public A(string n)
    _name = n;
  public string ShowName
    get {return _name;}
  public void DoSmth()
    // Do something

I want to show lis of such objects in ListBox. Also I want button in each line of ListBox and the mechanism to determine wich button was clicked. If I can directly call DoSmth() it would be the best. But I would be very happy if I can even find out the index of this button :)


Now my XAML looks like this:

<UserControl x:Class="MyApp.ClientUI.Controls.MyControl"
 Height="Auto" Width="Auto">
    <StackPanel x:Name="sp" Orientation="Horizontal">
     <ListBox Grid.Row="0" x:Name="lstMy">
        <StackPanel Orientation="Horizontal">
         <TextBlock VerticalAlignment="Center" Margin="5" Foreground="White" FontSize="17" FontFamily="Calibri" Text="{Binding Path=ShowName}" /&g

View Complete Post

More Related Resource Links

WPF, MVVM, Commands from buttons within a Listbox ItemTemplate? (code included)

I want to capture button clicks from within a list item row. Below I have tried to boil the problem down as much as I can. First, here is the xaml <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="test2.MainWindow" x:Name="Window" Title="MainWindow">  <Grid x:Name="LayoutRoot">  <ListView Margin="148,74,0,202" HorizontalAlignment="Left" ItemsSource="{Binding ListData}">   <ListView.View>    <GridView>     <GridViewColumn Header="Item" DisplayMemberBinding="{Binding ItemName}" />     <GridViewColumn Header="Options" Width="200" >      <GridViewColumn.CellTemplate>       <DataTemplate>        <StackPanel Orientation="Horizontal">         <Button Content="Reject" Height="20" Width="80" Margin="5,2" Command="{Binding Commands}" CommandParameter="RejectItem" /> 

Gridview with Radio Buttons

As now, Radio buttons are HTML controls, I am not able to get it on server side. When user select any radio button and press submit, Control will not available at code behind, you can use "e.Item.FindControl" as it is not "runat='server'". I want ID "Primary Key" value for selected radio button. To achive this I took the help of Javascript. I wrote javascript function that sets the values of "Primary Key" in hidden field when user clicks on any radio button. Now when user clicks on Submit button, user can get selected value froim hidden field.

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous

Persist data in a dynamically populated ListBox in a user control


Hi All,

I am trying to persist data in a dynamically populated ListBox in a user control. Here are the details

I have a user control which contains a a listBox and button. On Page_Load of user control I am populating the ListBox control with some values. On Click of button, I am trying to fetch the values which I have selected in ListBox. Though this seems very simple and straight forward I am unable to fetch values.

My Page_Load Function

void Page_Load(object sender, EventArgs e)










My Click Function

Protected Void Button1_Click(object sender, EventArgs e)


       string strValue = listBox.SelectedValue



I have placed brake point at Page_Load and on click function, I am seeing that the moment the breakpoin

ActiveX and Visual Basic: Enhance the Display of Long Text Strings in a Combobox or Listbox


The combobox and listbox controls in Visual Basic have no built-in support for displaying text strings that are too long to fit in the visible area of either control. This article describes several techniques to improve the readability of long strings in each control. The first technique uses tooltips to display a combobox or listbox item. The other techniques, which use various Windows APIs, include adding a horizontal scrollbar for a listbox and determining the necessary width of the longest string given the font attributes of the text. The required steps are described for both comboboxes and listboxes.

John Calvert

MSDN Magazine December 2000

Buttons with Mouse-Over Behaviors - Redux

I recently posted some CSS and HTML snippets for a buttons collection I was using for a Menu style UI Scott Koon from www.LazyCoder.com posted a comment with a better way (thanks Scott). This way I don't have to set the CSS Class for each button. Just contain them in a div. Here is the [...] Read More......(read more)

setting scroll position for a ListBox


I have a ListBox control showing 10 rows.  I have 30 rows total in the listbox.  How do I programmatically adjust the scroll to show the last ten rows?

Some sources have suggested that I set the SelectedItem.  But I am already making selections (myListBox.Items[28].Selected = true, myListBox.Items[29].Selected = true, etc); the listbox scroll is positioned on Item[0] not the first selected item.  I have also tried setting myListBox.SelectedItem in addition to the multiple selection just mentioned but that does not have any affect either.


single form multiple buttons in MVC


In a single page  has two buttons , ddown and tempname.

I wrote two actionresult separetly each one for ddown and tempname.

I need one is post and another one is get method. so i gave like

1. Get Method


        [AcceptParameter(Name = "button", Value = "tempname")]
        public ActionResult ddown_tempname(string statusName, NewsLetterAdminViewModel viewModel)

return View();



2.Post Method


        [AcceptParameter(Name = "button", Value = "ddown")]
        public ActionResult ddown(string statusName, string button, NewsLetterAdminViewModel viewModel, FormCollection collection)

return View();



if i call the post method its worked well. but if i call the get method, the code does not execute. the brea

Filter a listbox with textbox


So i currently filter the listbox with some javascript i have found.
What it does is going to the item i type on the textbox.
It will not filter the listbox so it will contain the specific matches.
I was reading for autocomplete but i don't seem to find this 2 way usage.
I was also reading this article:
But i'm completely lost on how i can implement it.
Any ideas?

How to reference a listbox from a procedure


I have a listbox that gets loaded from a couple of different places on my web form.

I would like to put the code to load the listbox in one place, rather than have two copies of it.

The problem is, I can't figure out how to reference the listbox control successfully from within a procedure.

Would anyone know how to do this?

My code looks like this:

private static bool LoadSubscriptionListBox(System.Web.UI.Page Page)
        bool bReturn = false;

        // Clear the listbox

        // Load the listbox



Ajax.BeginForm with multiple buttons


can i have more than one AjaxOptions in Ajax.BeginForm targetting different controls?

Actually I want to exchange data between two listboxes placed in a single ajax.beginform.

How to do it..................help me.

Edin, New and Delete buttons in gridview?


Hi guys. I have gried view that shows me all info from the table i made. So my SelectCommand works fine. I'v add 3 additional buttons "new", "edit" and "delete" i'v made their query but still have mistake by clicking it in browser. I follow from the book Begginer to Novice 3.5 ASP C# but that's what it's shown. I know something missing. I'v check in the net but not found how to fixed, because i use SqlDataSource. Can you advise how to proceed from here? Here's what i did:

<asp:SqlDataSource ID="SqlDataSource5" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:ThisIsMyConnection%>"


              SelectCommand = "SELECT * FROM [Resident] WHERE (([FNAME] + ' ' + [LNAME] LIKE '%' + @FANDLNAME + '%'))"


              UpdateCommand = "UPDATE Resident SET FNAME=@FNAME, LNAME=@LNAME, MobileNum=@MobileNum, HomeNum=@HomeNum, OfficeNum=@OfficeNum, FaxNum=@FaxNum WHERE FNAME=@original_FNAME AND LNA

Series of Radio Buttons in MVC


Hi.  I'm fairly new to MVC and admittedly don't fully understand how data is passed between Views and Controllers.  That said, here is my issue.


I am working with C#.  I have a page which has a list of items.  Each item has a radio button set of three associated with it and there are 15 of these sets on the page.  Thus I have radio1, radio2....radio15, etc.  Each of these has value 0, 1, or 2 upon submit.  I can't seem to understand how to get the information out of these radio button sets from the controller without passing 15 different variables to the post method in the controller which is associated with the view; ie passing Int32 radio1, Int32 radio2,.., Int32 radio15, etc.

I tried using ViewData["radio"+i] in a loop to access the value of each radio button set, but this, apparently can only be used to pass data to the View from the Controller and not the other way around.

Any advice is appreciated.



jQuery modified ListBox not posting back modified values


Hi everyone,

I have an ASP.Net ListBox that I'm trying to populate via jQuery, using the following snippet:

        $("#MyList_btnAddAll").click(function(e) {
        $('#MyList_lstAll option').appendTo('#MyList_lstSelected');

The code has two ListBoxes in fact, one a "source" and the other a "destination".  As you can tell above the ListBoxes are MyList_lstAll and MyList_lstSelected.  These are rendered in the browser as <select> elements, as you'd expect.

The jQuery is working great, the items are moving from one ListBox to the other, the DOM is updated but when I submit this form (not using jQuery), the ListBoxes don't reflect this change on postback. 

I realize my modifications via jQuery aren't available in ViewState but I thought since the DOM was updated these changes would be part of the postback data?  But in the codebehind after postback the ListBox contents haven't changed.  Does anyone know what might be going on and what I can do about it?  Many thanks.

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