Checking Check Boxes

I've got a page that edits user permissions that I'm porting from PHP.
When the page loads, I need the site to load a checkbox list with the
name of the various items and the IDs as the value. This is easy.

However, I also need it to check off the selected items. This information is stored in a seperate database table. Using a JOIN I can write a query that will return the page id, the page name, and the permissions of the user fairly easily. However, I don't know what attribute to set in order for appropriate boxes to be checked when the page loads.

I'm posting this in the PHP migration page because doing this in PHP is really easy.

<?
while ($row = mysql_fetch_assoc($result)) {
    echo '<input type="checkbox" value="'.$row['page_id'].'"';
    if ($row['permissions'] == 1)
       echo " checked";
    echo '/>'.$row['page_name'].'<br>';
}
?>

0
prothrock
9/29/2005 3:58:38 PM
asp.net.migrating-from-php 286 articles. 0 followers. Follow

2 Replies
698 Views

Similar Articles

[PageSpeed] 55

It is nearly the same in .NET except you don't have to
worry about string writing.  It looks like you are using MySql, so
I think it is the OleDbXXX data objects you will use.  This is
assuming that your row["permissions"] is a bit (or boolean)
field.  You might have to change the item.Selected = *** line.

<asp:CheckBoxList id="CheckBoxList1" runat="server" />

In your page_load method:

// we only want to build the list the first time the page loads.
// you really, really should get in the habit of this, it will save many, many bugs.
if ( !Page.IsPostBack )
{
    //do you connection and query building

    using( IDataReader reader = Command.ExecuteReader( CommandBehavior.CloseConnection )
    {
       //this will continue to read all the rows returned by the query
       while( reader.Read() )
       {
          ListItem item = new ListItem();
          item.Text = reader["page_name"].ToString();
          item.Value = reader["page_id"].ToString();
          item.Selected = ( bool ) reader["permissions"];

          cbList.Items.Add( item );
       }
    }  
}

bill

0
billrob458
9/29/2005 5:06:15 PM
Actually, I'm using SQL Server, but I can do the DataReader thing. Thanks!
0
prothrock
9/29/2005 5:35:18 PM
Reply: