Using a ListBox Using SelectionMode Multiple with a GridView

You've probably run into this before. You have a ListBox (Or CheckBoxList or whatever) that lists a number of options and you can select multiple items. You then want it to be used to update the content of a GridView. Most likely you tried setting up the select statement as something like this: "SELECT * FROM Table WHERE Value=@Value" or even "SELECT * FROM Table WHERE Value in (@Value)". With @Value pointing to the SelectedValue parameter of the ListBox. If you tried that you'll notice that it didn't work. Well I mean it will update the GridView but it will only show that you selected one item. So how do we fix this?

One of the things that I like about the way the classes like GridView, etc. were set up is that you can inherit from them. In our case we want to create an item that uses the ListBox object:

    public class OurListBox : ListBox
    {
        public OurListBox()
            : base()
        {
        }

        public string SelectedValueList
        {
            get
            {
                bool None = true;
                string Seperator = "";
                StringBuilder Builder = new StringBuilder();
                foreach (ListItem Item in Items)
                {
                    if (Item.Selected)
                    {
                        None = false;
                        Builder.Append(Seperator).Append(Item.Value);
                        Seperator = ",";
                    }
                }
                if (None)
                {
                    Builder.Append("0");
                }
                return Builder.ToString();
            }
        }
    }

Ok, so now we have something that gives us a comma delimited string of the selected values. That's great but it doesn't help us much as sending that to @Value is just going to come back with an error or not much at all... So what we need is an SQL function that can parse that string. Luckily I found one that I could simply copy from here. So we create the function, switch our ListBox to the OurListBox class that we just created, and switch our select statement to this: "SELECT * FROM Table where Value in (SELECT Value FROM dbo.Split(@Value, ','))". And our controlparameter pointing towards the SelectedValueList property... And suddenly we have a working solution. Oh and just so you have something to look at:

     <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT * FROM Table WHERE Value in (SELECT Value FROM dbo.Split(@Value, ','))">
        <SelectParameters>
            <asp:ControlParameter ControlID="ListBox1" DefaultValue="0" Name="Value" PropertyName="SelectedValueList" />
        </SelectParameters>
    </asp:SqlDataSource>
    <cc1:OurListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></cc1:OurListBox>

That's it. So hopefully this will help you out. Try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 1/22/2010 at 1:10 PM
Tags: , ,
Categories: ASP.Net | C#
Post Information: Permalink | Comments (5) | Post RSSRSS comment feed

How to Add an Error Message to a ValidationSummary Progrommatically

For a while now I had been annoyed at the fact that there is no easy way to add an error message to a ValidationSummary control... Or at least I didn't think that there was, but it turns out that it's quite simple:

            RequiredFieldValidator Validator = new RequiredFieldValidator();
            Validator.ErrorMessage = "This is your error message";
            Validator.ValidationGroup = "Group1";
            Validator.IsValid = false;
            Validator.Visible = false;
            Page.Form.Controls.Add(Validator);

The error message and the validation group needs to be changed but basically this just creates a required field validator. It then says that the validator isn't valid and makes it invisible. However since we have a ValidationSummary object, it will show up there (assuming we set up the validation group properly). That's all there is to it. Anyway, I hope this helps out someone so you're not banging your head against a wall like I was. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 7/21/2009 at 10:15 AM
Tags: , , ,
Categories: ASP.Net
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Using Session within an HTTP Handler

If you have ever created an HTTP handler, you've most likely run into the annoying fact that the session state is not accessible. In fact most of the time you're going to run into a null reference exception and will take you a couple of hours to figure out what the heck is going on. However hope is not lost as you can turn on the session state for a handler.

    public class MyHandler:IHttpHandler,IRequiresSessionState

Notice that last interface? That interface acts as a marker, letting the system know that the handler requires the session state for read and write access. We could just specify IReadOnlySessionState as well if we're only interested in reading the session state but not writing to it. So there you go, that's all that you need to do. So hopefully this helps someone out. Give it a try, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 6/23/2009 at 8:07 AM
Tags: , ,
Categories: ASP.Net
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed