12 Answers, 1 is accepted
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
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;
onRadGrid1_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
checks.SelectedValue = DataBinder.Eval(item.DataItem, "RoleName").ToString();
I was not able to replicate the issue,the above code works fine at my end.
Thanks,
Princy
checks.SelectedValue = DataBinder.Eval(item.DataItem, "RoleName").ToString();
maybe we have to add on RadGrid1_ItemCreatedalso 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 ", "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
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
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
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