我在ListView的每一行中都有一個ListView和DropDownList。您可以在該DropDownlist中對指派的人進行cahnge(DropDownList顯示來自MySql數據庫的數據)。在ListView的Dropdownlist上的selectedIndexChanged上獲取ListView項的值
更改人員時,我需要做一些數據庫操作。爲此,我需要知道在哪個ListView Row中人員已更改,我需要知道條目的值。
這裏是我的aspx文件:
<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>
這裏是我的代碼隱藏:
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");
}
我成功獲得該行的DataItemIndex我改變了DropDownList的價值,也DROPDOWNLIST正在觸發糾正onSelectedIndexChanged,但我需要的是標籤「customerId」的價值,我不知道如何得到這個值。
在此先感謝
嗨,感謝您的回答,但我確實使用了FindControl,就像您一樣。字符串customerID仍爲空。 – user1814545
@ user1814545:改爲設置「Text」屬性。相應地編輯我的答案。 –
對不起,那是我的錯。通過文本屬性,您的解決方案非常完美。非常感謝你。 – user1814545