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

checkboxlist

12 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Troika
Top achievements
Rank 1
Troika asked on 20 Oct 2013, 06:34 PM
i'm creating a checboxlist on edit radgrid but i cant find the checkboxlist it self. i cant understand where im missing it.

'm getting the data on need datasource event and then switch it to checklistbox

[deleted]

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Oct 2013, 07:20 AM
Hi ,

Please try the following code snippet.Create the checkboxList in ItemCreated and bind its value in ItemDataBound event

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowPaging="true"
OnItemDataBound="RadGrid1_ItemDataBound" AutoGenerateEditColumn="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCreated="RadGrid1_ItemCreated"
OnUpdateCommand="RadGrid1_UpdateCommand">
</telerik:RadGrid>

C#:
//Create the CheckBoxList in EditMode
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
    {
        if (col.DataType == typeof(string))
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (col.UniqueName == "ShipCountry")
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    TextBox txt = (TextBox)item[col.UniqueName].Controls[0];
                    txt.Visible = false;
                    CheckBoxList checks = new CheckBoxList();
                    checks.DataSourceID = "sql_ds_funcao";
                    checks.DataTextField = "ShipCountry";
                    checks.DataValueField = "ShipCountry";
                    checks.ID = "CBLRole";
                    checks.ClientIDMode = ClientIDMode.Static;
                    item[col.UniqueName].Controls.Add(checks);
                }
            }
        }
    }
}
   
//Binding the value of checkboxlist in Edit
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
    {
        if (col.DataType == typeof(string))
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (col.UniqueName == "ShipCountry")
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    CheckBoxList checks = (CheckBoxList)item.FindControl("CBLRole");
                    checks.SelectedValue = DataBinder.Eval(item.DataItem, "ShipCountry").ToString();
                    checks.DataSourceID ="sql_ds_funcao";
                    checks.DataTextField = "ShipCountry";
                    checks.DataValueField = "ShipCountry";                    
                }
            }
        }
    }
}
       
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    string values = string.Empty;
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem data = (GridEditableItem)e.Item;
        CheckBoxList chk = (CheckBoxList)data.FindControl("CBLRole");
        for (int i = 0; i < chk.Items.Count; i++)
        {
            if (chk.Items[i].Selected)
            {
                values = values + "," + chk.Items[i].Text.ToString();//Storing the selected values
            }
        }
        string selectedvalue = values.Trim(',');
    }
}

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 21 Oct 2013, 03:00 PM
ok it fixes all. so worked BUT i edit it on frist time  then i save it woreks and save but when i edit it on a second time it gives:

 Erro de runtime de JavaScript: Sys.WebForms.PageRequestManagerServerErrorException: 'CBLRole' has a invalid  because it dont exits on items list

parameter name: value.

its strange because i click on edit then appears the checkboxlist then i select the options , hit save, then edit again , select hit save and then error

i have added to your code the

// txt.Visible = false;

 on

RadGrid1_ItemDataBound



then on itemcreated this:

CheckBoxList chkl = (CheckBoxList)data1.FindControl("CBLRole");

int p = 0;

for (p = 0; p <= chkl.Items.Count - 1; p++)

{

if ((chkl.Items[p].Selected) && (Roles.IsUserInRole(chkl.Items[p].Value))) // verifica se o user está nessa role , se nao tiver adiciona a role ao user

{

Roles.AddUserToRole(user.ToString(), chkl.Items[p].Value);

Membership.UpdateUser(user);

}

}

when i add a new role to the user if i try to edit user again i get that error, by the way is possible to show this error in a window popup wich catch or something , now i see because i'm on debug

0
Troika
Top achievements
Rank 1
answered on 21 Oct 2013, 06:13 PM
i think the problema is in this line because it gets ", RoleName" insetad of just "RoleName"

checks.SelectedValue = DataBinder.Eval(item.DataItem, "RoleName").ToString();

0
Princy
Top achievements
Rank 2
answered on 22 Oct 2013, 07:04 AM
Hi Troika,

I was not able to replicate the issue,the above code works fine at my end.

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 22 Oct 2013, 07:44 AM
if you can take a look at http://screencast.com/t/1FIBqbVQmN the problema is in this line

checks.SelectedValue = DataBinder.Eval(item.DataItem, "RoleName").ToString();

 maybe we have to add on  RadGrid1_ItemCreated

also can be on this line

dr["Rolename"] = String.Join(", ", Roles.GetRolesForUser(mu.UserName));

 wich makes the role with ", "on grid view mode and when wents on edit mode it wont find a role wit the ", " 
0
Antonio Stoilkov
Telerik team
answered on 23 Oct 2013, 07:10 AM
Hi Troika,

Based on your description it is hard to determine the cause of the problem. I have observed the exception on the video but it is not in English and I could not understand it. In such cases we recommend assembling a sample project showing the unwanted behavior so we could debug it and advice you with the best possible solution. You could upload the project on a public sharing service and provide us a URL.

Additionally, you could disable the ajax by setting the EnableAjax property to false to RadAjaxManager and RadAjaxPanel controls in order to observe where exactly the server exception is thrown. Note that if the problem is in the line where you use the DataBinder you could try casting the item.DataItem value to its actual type which could be checked while debugging and access the RoleName from it.

Regards,
Antonio Stoilkov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Troika
Top achievements
Rank 1
answered on 23 Oct 2013, 07:57 AM
the error is 'CBLRole' has a SelectedValue invalid because it does not exist in the list of items.
Parameter name: value

and its because is passing "" in text property but i cant understand why is passing empty.

what you mean where should i put the try?

maybe this line is not Reading well from datasource.

DataBinder.Eval(item.DataItem, "RoleName").ToString();

the problema is when we provide a sample i need a database with login that i dot have a test database, i could provide team viewer access

0
Troika
Top achievements
Rank 1
answered on 23 Oct 2013, 09:34 AM
when i edit and save it pops that error above when i edit and do not save it says Object reference not set to an instance of an object. and the problem is on that line
0
Troika
Top achievements
Rank 1
answered on 24 Oct 2013, 08:49 AM
help on this please
0
Antonio Stoilkov
Telerik team
answered on 28 Oct 2013, 06:38 AM
Hello,

Your issue is not common and without providing a sample project we could not further investigate the problem and find the cause of the problem. Note that you could remove your database logic and replace it with a dummy DataTable object. Additionally, you could go through the steps for simplifying a project described in the article below to help you throught the process:

Regards,
Antonio Stoilkov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Troika
Top achievements
Rank 1
answered on 29 Oct 2013, 03:02 PM
the problema is the fris time edit it works but the next it says invalid selected value, i could provide team viewer access
0
Antonio Stoilkov
Telerik team
answered on 01 Nov 2013, 09:43 AM
Hi,

As described in the previous post we could not further investigate the issue without a sample runnable project showing the unwanted behavior. Additionally, only customers with DevCraft Ultimate license have a Remote Web Assistance provides as a service as shown in the link below.

Regards,
Antonio Stoilkov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Troika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Troika
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or