Get values from selected item in listview using asp.net c#

16k views Asked by At

I've tried many many different ways of displaying values from listview to either textbox or just store it in session and use it later, but none of it works.

I was able to make it work for GridView, but i just can't make it work for ListView.

The idea is to get id of selected user (in gridview) and id of selected club (in listview)

After getting and storing both id's from selected rows, i can add club to another user.

Let's say i have table Clubs which has item: (4 columns, 1 row)

ID: 1 ClubName: Los Angeles Lakers City: Los Angeles State: California

When i press on the first row, it should highlight in a different color, and then store ID either to session or just textbox. Doesn't really matter as i'll decide that later. But can you guys please help me get ID ?

This is source code

<asp:ListView ID="ListView1" runat="server" DataKeyNames="KlubID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" style="margin-right: 324px" OnRowCreated="GridView1_RowCreated">
        <AlternatingItemTemplate>
            <tr style="background-color:#FFF8DC;">
                <td>
                    <asp:Label ID="KlubIDLabel" runat="server" Text='<%# Eval("KlubID") %>' />
                </td>
                <td>
                    <asp:Label ID="ImeKlubaLabel" runat="server" Text='<%# Eval("ImeKluba") %>' />
                </td>
                <td>
                    <asp:Label ID="KrajLabel" runat="server" Text='<%# Eval("Kraj") %>' />
                </td>
                <td>
                    <asp:Label ID="DrzavaLabel" runat="server" Text='<%# Eval("Drzava") %>' />
                </td>
                <td>
                    <asp:Label ID="MenedzerLabel" runat="server" Text='<%# Eval("Menedzer") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="background-color:#008A8C;color: #FFFFFF;">
                <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="KlubIDLabel1" runat="server" Text='<%# Eval("KlubID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ImeKlubaTextBox" runat="server" Text='<%# Bind("ImeKluba") %>' />
                </td>
                <td>
                    <asp:TextBox ID="KrajTextBox" runat="server" Text='<%# Bind("Kraj") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DrzavaTextBox" runat="server" Text='<%# Bind("Drzava") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MenedzerTextBox" runat="server" Text='<%# Bind("Menedzer") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                <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>&nbsp;</td>
                <td>
                    <asp:TextBox ID="ImeKlubaTextBox" runat="server" Text='<%# Bind("ImeKluba") %>' />
                </td>
                <td>
                    <asp:TextBox ID="KrajTextBox" runat="server" Text='<%# Bind("Kraj") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DrzavaTextBox" runat="server" Text='<%# Bind("Drzava") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MenedzerTextBox" runat="server" Text='<%# Bind("Menedzer") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr style="background-color:#DCDCDC;color: #000000;">
                <td>
                    <asp:Label ID="KlubIDLabel" runat="server" Text='<%# Eval("KlubID") %>' />
                </td>
                <td>
                    <asp:Label ID="ImeKlubaLabel" runat="server" Text='<%# Eval("ImeKluba") %>' />
                </td>
                <td>
                    <asp:Label ID="KrajLabel" runat="server" Text='<%# Eval("Kraj") %>' />
                </td>
                <td>
                    <asp:Label ID="DrzavaLabel" runat="server" Text='<%# Eval("Drzava") %>' />
                </td>
                <td>
                    <asp:Label ID="MenedzerLabel" runat="server" Text='<%# Eval("Menedzer") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <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" style="background-color:#DCDCDC;color: #000000;">
                                <th runat="server">KlubID</th>
                                <th runat="server">ImeKluba</th>
                                <th runat="server">Kraj</th>
                                <th runat="server">Drzava</th>
                                <th runat="server">Menedzer</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="KlubIDLabel" runat="server" Text='<%# Eval("KlubID") %>' />
                </td>
                <td>
                    <asp:Label ID="ImeKlubaLabel" runat="server" Text='<%# Eval("ImeKluba") %>' />
                </td>
                <td>
                    <asp:Label ID="KrajLabel" runat="server" Text='<%# Eval("Kraj") %>' />
                </td>
                <td>
                    <asp:Label ID="DrzavaLabel" runat="server" Text='<%# Eval("Drzava") %>' />
                </td>
                <td>
                    <asp:Label ID="MenedzerLabel" runat="server" Text='<%# Eval("Menedzer") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>

This is code - my latest, doesnt work

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.ListViewCommandEventArgs e)
{
    ListViewDataItem item1 = e.Item as ListViewDataItem;
    int findMe = item1.DisplayIndex;

    var route = (Label)item1.FindControl("ID");
    TextBox1.Text = route.Text;
} 

Any input would be appreciated, thank you!

PS, IS IT POSSIBLE TO HAVE TWO GRIDVIEW WHICH WOULD STORE DIFFERENT DATA?

I tried this before, but everytime i selected item in 2nd gridview, the item from 1st gridview was getting selected.

3

There are 3 answers

0
hamed On

this is the handler of GRidView Events not listView Events

GridView1_RowCreated()

Actually I have the same issue as you ,, and I found a way to get the id of selected raw ,,

     int id  =(int) ListView1.SelectedDataKey.Value;
// if the Id is
// the datakey which i think the  primary key""

and if you want to get the data of different column in the selected listView raw::

             ListViewItem item = ListView1.Items[ListView1.SelectedIndex];         

             WebControl c = (WebControl)item.FindControl("here type the lable id!!");
             Label la = (Label)c;//to get the text of the label by using c.Text;

because i am a beginner i convert two times ,, Of course there will be a better way << but what to do if no expert answer

2
hamed On

no need to convert it to webControl just ::

    ListViewItem item = ListView1.Items[ListView1.SelectedIndex];  
    Label c = (Label)item.FindControl("here type the lable id!!");

    //then use
   string result=c.text;// you get the value or the text of the Label ,,
0
Ahmed Yasser On

it's old I know .. for any one else search

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       ListViewItem Item = ((LinkButton)sender).NamingContainer as 
        ListViewItem;

        if (Item != null)
        {
            //for DataKeys
            int Klub_ID = (int)ListView1.DataKeys[Item.DataItemIndex] 
            ["KlubID"];

            //for any labl in your ListView
            Label lblImeKlubaLabel = 
             (Label)Item.FindControl("ImeKlubaLabel");
            Session["Duration"] = lblImeKlubaLabel.Text;


        }
     }