View Full Version : only select one out of 4 listboxes
TimberChampagne
12-18-2008, 08:00 AM
Hi,
I have 4 listboxes. I want to set it as select only one out of the 4 listboxes.
How shall I go about doing it??
Please help!!
Freon22
12-18-2008, 05:58 PM
I don't think you can group listboxies if someone know how please post. But the only way that I know of to do what you want is with a postback.
So here you have four list Boxies, would would set the auto post back to true. Set the OnSelectedIndexChanged to call the methods in the codebehind page.
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="Testing 1" Text="Testing 1"></asp:ListItem>
<asp:ListItem Value="Testing 2" Text="Testing 2"></asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
<asp:ListItem Value="Testing 1" Text="Testing 1"></asp:ListItem>
<asp:ListItem Value="Testing 2" Text="Testing 2"></asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox3" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox3_SelectedIndexChanged">
<asp:ListItem Value="Testing 1" Text="Testing 1"></asp:ListItem>
<asp:ListItem Value="Testing 2" Text="Testing 2"></asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox4" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox4_SelectedIndexChanged">
<asp:ListItem Value="Testing 1" Text="Testing 1"></asp:ListItem>
<asp:ListItem Value="Testing 2" Text="Testing 2"></asp:ListItem>
</asp:ListBox>
In the code behind page you would put your methods. So when someone clicked a value it will check and see if any of the other list boxies have a value selected. If any other list boxies have a value selected then it will change it to null. Try it out it works fine but it does cause a postback whenever any of them are selected.
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox2.SelectedValue != null || ListBox3.SelectedValue != null || ListBox4.SelectedValue != null)
{
ListBox2.SelectedValue = null;
ListBox3.SelectedValue = null;
ListBox4.SelectedValue = null;
}
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedValue != null || ListBox3.SelectedValue != null || ListBox4.SelectedValue != null)
{
ListBox1.SelectedValue = null;
ListBox3.SelectedValue = null;
ListBox4.SelectedValue = null;
}
}
protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedValue != null || ListBox2.SelectedValue != null || ListBox4.SelectedValue != null)
{
ListBox1.SelectedValue = null;
ListBox2.SelectedValue = null;
ListBox4.SelectedValue = null;
}
}
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedValue != null || ListBox2.SelectedValue != null || ListBox3.SelectedValue != null)
{
ListBox1.SelectedValue = null;
ListBox2.SelectedValue = null;
ListBox3.SelectedValue = null;
}
}
Edit: One more thing you can instead of having 4 methods you can just put that all in one method and have each of the listboxies call the one method to run the check.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.