[英]Get Selected ValueMember of Selected Items in a textbox (separated with Commas) from MultiSelect ListBox?
[英]Get changes to multiselect listbox selected items
我有一個列表框數據綁定(如果不進行回發)和從數據庫中選擇的項(如果適用)。 如果我在列表框中選擇新項目並回發,我的foreach邏輯將始終僅看到原始選擇,而不看到更改。 一直在用我的頭在谷歌上尋找答案。 這是背后的代碼:
foreach (ListItem li in lsb.Items)
{
if (li.Selected)
{
try
{
[sql insert]
}
}
}
編輯:我應該添加列表框包含在和updatepanel
希望這些會給你帶來靈感。
protected void MyListBox_TextChanged(object sender, System.EventArgs e)
{
try
{
listObject = sender as ListBox;
if (listObjct ! = null && !CheckItemInDBorNot(listObject.Text))
{
// This item not exists in Db,
// excute sql insert it into Db.
Add(listOjbect.Text);
}
}
catch (Exception ex)
{
}
}
標記:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1" />
<asp:ListItem Value="2" />
<asp:ListItem Value="3" />
<asp:ListItem Value="4" />
<asp:ListItem Value="5" />
</asp:ListBox>
<asp:Button ID="btnPostback" runat="server" Text="Postback" OnClick="btnPostback_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
后台代碼:
protected void btnPostback_Click(object sender, System.EventArgs e)
{
for (int i = 0; i <= ListBox1.Items.Count - 1; i++) {
ListItem li = ListBox1.Items(i);
if (li.Selected) {
try {
string sValue = li.Text;
// Do insert here
} catch (Exception ex) {
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.