I would like to get the selected item index when user click the row in ListView. In MSDN, I just find, it needs to add a "Select" button for every row. How to select the row without button,just click the row.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Marked as answer by freshare on May 08, 2009 12:46 PM
freshare
Member
1 Points
2 Posts
How to get select item index in listview without clicking button?
May 05, 2009 01:11 PM|LINK
I would like to get the selected item index when user click the row in ListView. In MSDN, I just find, it needs to add a "Select" button for every row. How to select the row without button,just click the row.
Samu Zhang -...
All-Star
62163 Points
6101 Posts
Re: How to get select item index in listview without clicking button?
May 08, 2009 07:43 AM|LINK
Hi freshare ,
see my solution.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button btn = e.Item.FindControl("Button1") as Button;
string script = this.ClientScript.GetPostBackClientHyperlink(btn,"",true);
Panel p = e.Item.FindControl("Panel1") as Panel;
p.Attributes.Add("onclick",script);
}
}
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
}
<asp:ListView ID="ListView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" onitemdatabound="ListView1_ItemDataBound"
onselectedindexchanging="ListView1_SelectedIndexChanging">
<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">
id</th>
<th runat="server">
approved</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="idLabel" runat="server" Text='<%# Eval("id") %>' />
</td>
<td>
<asp:CheckBox ID="approvedCheckBox" runat="server"
Checked='<%# Eval("approved") %>' Enabled="false" />
</td>
</tr>
</SelectedItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" CommandName="Select" runat="server" Text="Button" Visible="false" />
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
</asp:Panel>
</td>
<td>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
sumitd
Star
12168 Points
2151 Posts
Re: How to get select item index in listview without clicking button?
May 08, 2009 07:58 AM|LINK
Refer below msdn article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindexchanged(VS.80).aspx
Visit: www.msblogdirectory.com
www.dotnetspeaks.com
freshare
Member
1 Points
2 Posts
Re: How to get select item index in listview without clicking button?
May 08, 2009 12:47 PM|LINK
Thanks to Samu Zhang.