This is a migrated thread and some comments may be shown as answers.

RadListBox checked attribute

6 Answers 153 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Lighthouse Developer
Top achievements
Rank 1
Lighthouse Developer asked on 07 May 2010, 09:31 AM
Hi. Here's my problem:

I'm trying to set some items in a ListBox to be checked after the ListBox is databound. My problem is that even though the loop is executed and Visual Studio reports each item I want as checked = true, it doesn't appear to be so. Any suggestions?

            
            rlbRoles.DataBind(); 
            stmt = 
                @"Select auth_Roles.Name as Name, auth_Roles.NameEL as NameEL from auth_Roles INNER JOIN auth_UserRoles on auth_UserRoles.RoleId = auth_Roles.RoleId INNER JOIN auth_Users on auth_UserRoles.UserId = auth_Users.UserId 
                    WHERE auth_Users.UserId = @UserId"
 
            dt = DbSql.ExecuteDataTable(stmt, 
                                        new List<SqlParameter>(new[] 
                                                                   { 
                                                                       new SqlParameter("@UserId"
                                                                                        DataBinder.Eval(sender, 
                                                                                                        "DataItem.ID")) 
                                                                   } 
                                            ) 
                ); 
 
            int i = 0; 
            foreach (DataRow r in dt.Rows) 
            { 
                rlbRoles.FindItemByValue(dt.Rows[i]["Name"].ToString()).Checked = true
                i++; 
            } 

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 May 2010, 12:13 PM
Hi,

Your code works good when I tried on my end. Could you try the code in DataBound event of RadListBox and see whether it is working fine?

Regards,
Princy.
0
Lighthouse Developer
Top achievements
Rank 1
answered on 07 May 2010, 12:15 PM
My problem is getting 
DataBinder.Eval(sender,
"DataItem.ID")

in DataBound...
0
Genady Sergeev
Telerik team
answered on 10 May 2010, 01:32 PM
Hello Lighthouse Developer,

You should e.Item instead of sender as first parameter for DataBinder.Eval. Example code:

protected void RadListBox1_ItemDataBound(object sender, RadListBoxItemEventArgs e)
    {
        string id = (string)DataBinder.Eval(e.Item, "DataItem['ID']");
    }


Best wishes,
Genady Sergeev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Lighthouse Developer
Top achievements
Rank 1
answered on 10 May 2010, 01:53 PM
I still can't get it because ID comes from the editform's datasource (Does that make sense?)  It's something like

<asp:TextBox id="txtPassQ" Text='<%# DataBinder.Eval( Container, "DataItem.PasswordQuestion") %>' runat="server" tabIndex=8>

on the aspx file. Any way to get that value? (DataItem.ID). It's not in the ListBox's DataSource...
0
Accepted
Genady Sergeev
Telerik team
answered on 14 May 2010, 08:36 AM
Hello Lighthouse Developer,

You can do the following:

1) Into your codebehing page create property ID of type int.
2) Hook on the ItemDataBound event of RadGrid and use the following code:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
      {
          ID = (int)DataBinder.Eval(e.Item.DataItem, "ID");
      }
  }

Then in the DataBound event you can use freely use it.

Greetings,
Genady Sergeev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Lighthouse Developer
Top achievements
Rank 1
answered on 17 May 2010, 10:00 AM
But I don't need to use it in the RadGrid. I need to use it in the code behind of the control that I use as an edit form.

EDIT: It's ok. I was able to solve it in a similar way. Thanks for pointing me in the right direction.
Tags
ListBox
Asked by
Lighthouse Developer
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lighthouse Developer
Top achievements
Rank 1
Genady Sergeev
Telerik team
Share this question
or