Get value of ListView item in SelectedIndexChanged in dropdown in ListView
I have a ListView and DropDownList on each row of this ListView. You can link the designated person in this DropDownlist (DropDownList shows data from MySql database).
When changing a person, I need to perform some database operation. To do this, I need to know which ListView Row the person was modified on, and I need to know the value of the record.
Here is my aspx file:
<asp:ListView ID="manageAssigneesListView" runat="server" DataKeyNames="" OnItemDataBound="OnDataBound">
<ItemTemplate>
<div class="summaryRow">
<asp:Label ID="customerId" runat="server"><%# Eval("customerId") %> </asp:Label>
<div style="width:200px; margin: 0px 5px; float: left;"><%# Eval("lastName") %>, <%# Eval("firstName") %></div>
<div style="width:120px; margin: 0px 2px; float: left;">Nr.</div>
<div style="width:200px; margin-left: 50px; float: right;">
<asp:DropDownList runat="server" ID="selectAssignee" Width="196" AppendDataBoundItems="true" OnSelectedIndexChanged="selectAssignee_OnSelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</div>
<div class="clear"></div>
</div>
</ItemTemplate>
</asp:ListView>
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
Helper.doAuth(Session, Response);
if (!IsPostBack)
{
// load all customers without assignee
MySqlCommand cmd = new MySqlCommand();
MySqlConnection con = new MySqlConnection();
con.ConnectionString = Helper.CONNECTION_STRING;
cmd.CommandText = "SELECT customerId, lastName, firstName, assignee FROM customer WHERE assignee IS NULL ORDER BY customerId DESC";
cmd.Connection = con;
con.Open();
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
manageAssigneesListView.DataSource = ds;
manageAssigneesListView.DataBind();
con.Close();
}
}
protected void OnDataBound(object sender, ListViewItemEventArgs e)
{
DropDownList selectAssignee = (e.Item.FindControl("selectAssignee") as DropDownList);
MySqlCommand cmd = new MySqlCommand();
MySqlConnection con = new MySqlConnection();
con.ConnectionString = Helper.CONNECTION_STRING;
cmd.CommandText = "SELECT * FROM user where role = " + Helper.ROLE_SALESPERSON;
cmd.Connection = con;
con.Open();
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
selectAssignee.DataSource = ds;
selectAssignee.DataTextField = "emailAdress";
selectAssignee.DataBind();
con.Close();
}
protected void selectAssignee_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = (DropDownList)sender;
ListViewItem listView = (ListViewItem)dropDownList.NamingContainer;
var test = listView.DataItem;
string test2 = listView.FindControl("customerId").ToString();
int rowIndex = (int)listView.DataItemIndex;
Label lblMessage = (Label)listView.FindControl("customerId");
}
I was able to get the DataItemIndex from the string, I changed the value of the DropDownList, also the DropDownlist triggers the onSelectedIndexChanged fix, but I need the "customerId" Label value and I don't know how to get that value.
Thank you in advance
You need to use FindControl
to get a link to Label
and then use it. Property "Text":
ListViewItem item = (ListViewItem)dropDownList.NamingContainer;
Label lbltCustomerID = (Label) item.FindControl("customerId");
string customerID = lblCustomerID.Text;
By the way, ListViewItem.DataItem
always null
on the other side.
Edit
You have to set Text
labels, so instead of
<asp:Label ID="customerId" runat="server">
<%# Eval("customerId") %>
</asp:Label>
this is
<asp:Label ID="customerId" runat="server"
Text='<%# Eval("customerId") %>' >
</asp:Label>
try to get the label value directly from the ListView like
DropDownList dropDownList = (DropDownList)sender;
ListViewItem listView = (ListViewItem)dropDownList.NamingContainer;
var test = listView.DataItem;
string test2 = listView.FindControl("customerId").ToString();
int rowIndex = (int)listView.DataItemIndex;
Label label = (Label)manageAssigneesListView.Items[rowIndex ].Controls[0];
or
Label label = (Label)manageAssigneesListView.Items[rowIndex].FindControl("customerId");
and make sure the ListView doesn't bind every time on postback
for example, if the ListView loaded in the Load page, don't forget to add it to:
if (!IsPostBack)
{
//Load ListView
}
Even if he answered, my solution worked great for me:
protected void myDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = (DropDownList)sender;
ListViewDataItem listView = (ListViewDataItem)dropDownList.NamingContainer;
int rowIndex = listView.DataItemIndex;
//Some logic
}
Not that I am using ListViewDataItem to update Peasmaker solution