Gridviews and checkboxes

Last post 11-09-2007 8:44 AM by Charles Asbornsen. 10 replies.

Sort Posts:

  • Gridviews and checkboxes

    11-08-2007, 5:56 AM

     Hi,

     I'm very new to all this, so please bear with me...

     
    I have followed an article by Scott Mitchell (http://www.asp.net/learn/data-access/tutorial-52-cs.aspx) to add checkboxes to a gridview.  That gridview shows all the user preferences available, and those preferences checked are saved to a user-preferences table.

    My problem is that when the page loads, I would like to pre-select the preferences previously checked by that user.  Is this possible?  I'm also wondering if I should be using a checkboxlist instead of a gridview.

     
    I'd be very grateful if anybody can point me in the right direction, even if it's just giving me the idea in plain english!  If anybody wants to volunteer some code, that would be even better!!

     
    Thanks,

     
    Neil
     

    Filed under: ,
  • Re: Gridviews and checkboxes

    11-08-2007, 9:22 AM

    Ah, young padawan.  When you design your grid, bind your checkboxes to the various preference fields and set your datasource equal to SELECT whatever FROM wherever WHERE user_name = whoever.  Then do a GridView1.DataBind() in GridView1.PreRender().

    And if you want to be able to scroll through all the users for your admin screen, if the admin login is used set your datasource to SELECT whatever FROM wherever and leave it at that.  Also, you'd want to set the template column for user_name to visible in that case, but you get good code sharing out of it.  Better than a checkbox list.

     

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridviews and checkboxes

    11-08-2007, 10:12 AM

    Hi Charlie,

    Thanks for the reply.  I think I may not have given enough information in my first post...  At present, the gridviews datasource is SELECT preference name, preference id FROM preferences.  That lists all available options.  I can't seem to specify a different data source for the checkboxes, which need to be based on something like SELECT preference id FROM userpreferences WHERE userid = @userid.

    I hope this makes sense, and that you might be able to help me further.  I'm also using the sqltableprofileprovider, if that's relevant.


    Thanks again,

    Neil
     

  • Re: Gridviews and checkboxes

    11-08-2007, 10:52 AM

    You;ll need to add a xref table to join your users to their selected preferences.  You'll also have to add a value field.  Lets think:

    UserTable

    UserId int
    UserName varchar
    + whatever else you may want to know

    PreferenceTable

    PrefId int
    PrefName varchar

    UserPreferenceXRef

    UserId int
    PreferenceId int
    PreferenceValue bit

    Datasource for your GV in user mode is SELECT p.PrefName, x.Value FROM PreferenceTable p
                                                              INNER JOIN UserPreferenceXRef x ON p.PreferenceId = x.PreferenceId
                                                              INNER JOIN UserTable u on x.UserId = u.UserId AND u.UserName = @CurrentUser

    I'm working on a stored proc that will build a horizontal datasource for your admin view.

     

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridviews and checkboxes

    11-08-2007, 11:21 AM

     Thanks for your help Charlie - When you say admin view, are you referring to the asp.net admin pages?  If so, I'm not using the admin function for this, but looking to build the page from scratch - is that possible?

     

    Thanks,

     

    Neil 

  • Re: Gridviews and checkboxes

    11-08-2007, 12:16 PM
    Answer

    Yeah, I'm working on flattening your table so that you get something like

                  Pref1   Pref2   Pref3   Pref4
    User1        1         0         0         1
    User2        0         1         1         1

    Maybe there's some smart SQL guy out there who already has that.  I'm trying to use a cursor to loop through the preferences... you know what, I need to make a temp table, insert the values into it and return that!

    So you set your GridView to autogenerate the columns and boom.

    Actually, I'll write a sp to generate a table-valued function that you can use as your datasource.  I know it seems like a lot of work but we just refactored a HUGE project here and we're checking into SourceSafe... and that's vvvveeeerrrrryyyyy ssssslllllloooooowwwwwwwwwwwwwww.....

    And we can't do anything until its all done and this is interesting.

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridviews and checkboxes

    11-08-2007, 12:32 PM

     Hi Charle,

     I really appreciate you taking the time to help me.  I'm not sure if the table you show in your last post is how the data will be presented or not.  If so, maybe it can be simpler.  The user preferences will only be visible to that user, so the end product will really just be a list of checkboxes. 

    If I've misunderstood, please ignore me...

     
    Thanks,

     
    Neil
     

  • Re: Gridviews and checkboxes

    11-08-2007, 12:39 PM

    Yeah, that's it.  Just select on the current user name and you'll get their preferences as a set of bits that you bind to the checkboxes.  I think you can just set autogenerate = true and the gridview will set the checkboxes for you.  Pretty sure anyway.  I haven't had the chance to do autogeneration - we're all about code generation here.

    This is interesting - an opportunity to use DSQL and there's no chance of SQL injection so no one out there can yell at me for recommending it!!! Devil

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridviews and checkboxes

    11-08-2007, 12:42 PM

    meeting the wife for lunch - bb1hr

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridviews and checkboxes

    11-09-2007, 8:20 AM

    Hi Charlie,

    I've managed to get what I need using a CheckBoxList control.  I hope you don't feel your time has been wasted.

    Thanks,

    Neil

     

  • Re: Gridviews and checkboxes

    11-09-2007, 8:44 AM

    OK, but how are you going to handle the admin screen?

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (11 items)
Microsoft Communities
Page view counter