I've ran into some trouble with string manipulation with arrays.
I have this:
public string LB_Ward_Selected()
{
string SelectedWard = String.Join(",", LB_Ward.Items.Cast<ListItem>()
.Where(i => i.Selected));
string[] SelectedItems = SelectedWard.Split(',');
for (int i = 0; i < SelectedItems.Length; i++)
{
SelectedItems[i].Replace(SelectedItems[i].ToString(), "'" + SelectedItems[i].ToString() + "'").ToArray();
}
SelectedWard = string.Join(",", SelectedItems);
return SelectedWard;
}
I taking a multiple selection from an asp listbox which I collate as a CSV string, then split into an array then try to change (using Replace.) the separated strings in the array then output it back into a comma separated value string.
I can't see why it isn't working, any ideas?
HTML:
<asp:ListBox ID="LB_Ward" runat="server" DataSourceID="SQLWard" CssClass="btn btn-default" SelectionMode="Multiple"
DataTextField="WARD" DataValueField="WARD" AutoPostBack="True" AppendDataBoundItems="true" EnableViewState="false" OnSelectedIndexChanged="DD_area_SelectedIndexChanged">
<asp:ListItem Value="0">Select Ward</asp:ListItem>
</asp:ListBox>
<asp:SqlDataSource ID="SQLWard" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"
SelectCommand="SELECT DISTINCT [WARD] FROM [Borough] WHERE Borough =@BoroughFilter">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListBorough" PropertyName="SelectedValue"
Name="BoroughFilter" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
The method is called like this:
if (LB_Ward.SelectedIndex > 0)
{
ward = LB_Ward_Selected();
}
Which I know requires refining! :)