H
Heron
Hi,
I have a listbox (lstItems) that I want to populate with the items in
another listbox (lstSelected) that's in a modalpopupextender. The update is
supposed to happen when I close the modalpopup so I tried to add a trigger
for the click event of the ok button but it seems this event is not fired
anymore on serverside when it's used as the OnOkControlID for the
modalpopupextender.
Anyone know how to slove this?
Best regards.
-----webform-----
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
..modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
}
..modalPopup {
/*background-color:#ffffdd;*/
background-color: #ffffff;
border-width:3px;
border-style:solid;
/*border-color:Gray;*/
border-color: #0079b9;
padding: 3px 3px 30px 3px;
width: 670px;
height: 110px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstItems" runat="server"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkBtnOk" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:LinkButton ID="lnkEdit" runat="server">Edit</asp:LinkButton>
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtender1"
runat="server"
DropShadow="true"
PopupControlID="pnlEdit"
TargetControlID="lnkEdit"
BackgroundCssClass="modalBackground"
OkControlID="lnkBtnOk"
CancelControlID="lnkBtnCancel" />
<aspanel ID="pnlEdit" runat="server" CssClass="modalPopup"
style="display:none;">
<div id="pnlHeader" style="text-align: right;">
<asp:LinkButton ID="lnkBtnOk" runat="server" Text="close"
OnClick="lnkBtnOk_Click"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server" Text="close"
style="display:none;"></asp:LinkButton>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div style="float: left;">
<asp:ListBox ID="lstAvailable" runat="server"></asp:ListBox>
</div>
<div style="float: left;">
<asp:Button ID="btnAdd" runat="server" Text=" > " ToolTip="Add"
CausesValidation="false" OnClick="btnAdd_Click" /><br />
<asp:Button ID="btnDel" runat="server" Text=" < " ToolTip="Remove"
CausesValidation="false" OnClick="btnDel_Click" />
</div>
<div style="float: left;">
<asp:ListBox ID="lstSelected" runat="server"></asp:ListBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</aspanel>
</form>
</body>
</html>
-----Code behind-----
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(lnkBtnOk);
if (!Page.IsPostBack)
{
lstAvailable.Items.Add("a");
lstAvailable.Items.Add("b");
lstAvailable.Items.Add("c");
lstAvailable.Items.Add("d");
lstAvailable.Items.Add("e");
lstAvailable.Items.Add("f");
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (lstAvailable.SelectedIndex > -1)
{
string value = lstAvailable.SelectedItem.Value;
RemoveItem(lstAvailable, value);
AddItem(lstSelected, value);
//AddItem(lstItems, value);
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
if (lstSelected.SelectedIndex > -1)
{
string value = lstSelected.SelectedItem.Value;
RemoveItem(lstSelected, value);
//RemoveItem(lstItems, value);
AddItem(lstAvailable, value);
}
}
protected void lnkBtnOk_Click(object sender, EventArgs e)
{
lstItems.Items.Clear();
foreach(ListItem li in lstSelected.Items)
lstItems.Items.Add(li.Value);
UpdatePanel2.Update();
}
private void RemoveItem(ListBox lst, string value)
{
ListItem o = lst.Items.FindByValue(value);
if (lst.Items.Count > 1)
{
if (lst.SelectedIndex == lst.Items.Count - 1)
lst.SelectedIndex--;
else
lst.SelectedIndex++;
}
else
lst.SelectedIndex = -1;
lst.Items.Remove(o);
}
private void AddItem(ListBox lst, string value)
{
lst.SelectedIndex = -1;
lst.Items.Add(value);
lst.SelectedIndex = lst.Items.IndexOf(lst.Items.FindByValue(value));
}
I have a listbox (lstItems) that I want to populate with the items in
another listbox (lstSelected) that's in a modalpopupextender. The update is
supposed to happen when I close the modalpopup so I tried to add a trigger
for the click event of the ok button but it seems this event is not fired
anymore on serverside when it's used as the OnOkControlID for the
modalpopupextender.
Anyone know how to slove this?
Best regards.
-----webform-----
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
..modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
}
..modalPopup {
/*background-color:#ffffdd;*/
background-color: #ffffff;
border-width:3px;
border-style:solid;
/*border-color:Gray;*/
border-color: #0079b9;
padding: 3px 3px 30px 3px;
width: 670px;
height: 110px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstItems" runat="server"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkBtnOk" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:LinkButton ID="lnkEdit" runat="server">Edit</asp:LinkButton>
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtender1"
runat="server"
DropShadow="true"
PopupControlID="pnlEdit"
TargetControlID="lnkEdit"
BackgroundCssClass="modalBackground"
OkControlID="lnkBtnOk"
CancelControlID="lnkBtnCancel" />
<aspanel ID="pnlEdit" runat="server" CssClass="modalPopup"
style="display:none;">
<div id="pnlHeader" style="text-align: right;">
<asp:LinkButton ID="lnkBtnOk" runat="server" Text="close"
OnClick="lnkBtnOk_Click"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server" Text="close"
style="display:none;"></asp:LinkButton>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div style="float: left;">
<asp:ListBox ID="lstAvailable" runat="server"></asp:ListBox>
</div>
<div style="float: left;">
<asp:Button ID="btnAdd" runat="server" Text=" > " ToolTip="Add"
CausesValidation="false" OnClick="btnAdd_Click" /><br />
<asp:Button ID="btnDel" runat="server" Text=" < " ToolTip="Remove"
CausesValidation="false" OnClick="btnDel_Click" />
</div>
<div style="float: left;">
<asp:ListBox ID="lstSelected" runat="server"></asp:ListBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</aspanel>
</form>
</body>
</html>
-----Code behind-----
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(lnkBtnOk);
if (!Page.IsPostBack)
{
lstAvailable.Items.Add("a");
lstAvailable.Items.Add("b");
lstAvailable.Items.Add("c");
lstAvailable.Items.Add("d");
lstAvailable.Items.Add("e");
lstAvailable.Items.Add("f");
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (lstAvailable.SelectedIndex > -1)
{
string value = lstAvailable.SelectedItem.Value;
RemoveItem(lstAvailable, value);
AddItem(lstSelected, value);
//AddItem(lstItems, value);
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
if (lstSelected.SelectedIndex > -1)
{
string value = lstSelected.SelectedItem.Value;
RemoveItem(lstSelected, value);
//RemoveItem(lstItems, value);
AddItem(lstAvailable, value);
}
}
protected void lnkBtnOk_Click(object sender, EventArgs e)
{
lstItems.Items.Clear();
foreach(ListItem li in lstSelected.Items)
lstItems.Items.Add(li.Value);
UpdatePanel2.Update();
}
private void RemoveItem(ListBox lst, string value)
{
ListItem o = lst.Items.FindByValue(value);
if (lst.Items.Count > 1)
{
if (lst.SelectedIndex == lst.Items.Count - 1)
lst.SelectedIndex--;
else
lst.SelectedIndex++;
}
else
lst.SelectedIndex = -1;
lst.Items.Remove(o);
}
private void AddItem(ListBox lst, string value)
{
lst.SelectedIndex = -1;
lst.Items.Add(value);
lst.SelectedIndex = lst.Items.IndexOf(lst.Items.FindByValue(value));
}