protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) { checkbox string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; // GridEditableItem data = (GridEditableItem)e.Item as GridEditableItem; // CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CBLRole");//Accessing the CheckBoxList //CheckBoxList checkedItems = data["CBLRole"].Controls[0] as CheckBoxList; //for (int i = 0; i < checkedItems.Items.Count; i++) //{ // if (checkedItems.Items[i].Selected) // { // values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values // } //} // string selectedvalue = values.Trim(',');//To trim off the last ',' TextBox txt = data.FindControl("txt_email") as TextBox;i have tried several ways as you can see in comente data line but i always get null. i have the radgrid inside a contenplace holder if that makes any change
11 Answers, 1 is accepted
Please try the following code snippet to access a textbox in Edit mode.
C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) { string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CheckBoxList1");//Accessing the Template CheckBoxList for (int i = 0; i < checkedItems.Items.Count; i++) { if (checkedItems.Items[i].Selected) { values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values } } string selectedvalue = values.Trim(',');//To trim off the last ',' //Access a boundcolumn in Edit mode TextBox txt = (TextBox)data["ColumnUniqueName"].Controls[0]; //Access a TextBox in EditItemTemplate of a GridTemplateColumn in Edit mode TextBox txttemplate = (TextBox)data.FindControl("TextBox1"); }Thanks,
Princy
string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; TextBox txt = (TextBox)data["Email"].Controls[0]; values = txt.Text;}i also NEED TO CHECK before save if the new input in a valid email (with regex or something) if wrong do noy update and popus up a exclamation mark or alertbox
what about cblroles???? that way i post wont work
i tried
CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CBLRole");//Accessing the CheckBoxList
then it went and get nullreferenceexception was unhandled by user code on for loop<table id="CBLRole">
<tbody><tr>
<td><input name="ctl00$ContentPlaceHolderMain$RadGrid1$ctl00$ctl11$CBLRole$CBLRole_0" id="CBLRole_0" type="checkbox" value="Administradores"><label for="CBLRole_0">Administradores</label></td>
</tr><tr>
<td><input name="ctl00$ContentPlaceHolderMain$RadGrid1$ctl00$ctl11$CBLRole$CBLRole_1" id="CBLRole_1" type="checkbox" value="Atletas"><label for="CBLRole_1">Atletas</label></td>
</tr><tr>
<td><input name="ctl00$ContentPlaceHolderMain$RadGrid1$ctl00$ctl11$CBLRole$CBLRole_2" id="CBLRole_2" type="checkbox" checked="checked" value="Utilizadores"><label for="CBLRole_2">Utilizadores</label></td>
</tr>
</tbody></table>
error:
Cannot find a cell bound to column name 'CBLRole'
aLSO haved tried
checks.ClientIDMode = ClientIDMode.Static;
but wont get the checkbox's selected
Please try the below code snippet.I was able to get the checked values.If this doesn't help,please give your full code snippet.
ASPX:
<telerik:GridTemplateColumn HeaderText="ShipCountry" DataField="ShipCountry"><ItemTemplate> <%# Eval("ShipCountry")%></ItemTemplate><EditItemTemplate> <asp:CheckBoxList ID="CheckBoxList1" runat="server" > </asp:CheckBoxList></EditItemTemplate></telerik:GridTemplateColumn>C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) { if (e.Item is GridEditableItem && e.Item.IsInEditMode) { //Binding values to checkboxlist GridEditableItem edit = (GridEditableItem)e.Item; CheckBoxList check = (CheckBoxList)edit.FindControl("CheckBoxList1"); check.DataSourceID = "SqlDataSource2"; check.DataTextField = "ShipCountry"; check.DataValueField = "ShipCountry"; } } protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) { string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CheckBoxList1");//Accessing the Template CheckBoxList for (int i = 0; i < checkedItems.Items.Count; i++) { if (checkedItems.Items[i].Selected) { values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values } } string selectedvalue = values.Trim(','); } }Thanks,
Princy
all my code:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns) { if (col.DataType == typeof(string)) { // get all fields as textbox except this: GridEditableItem item = (GridEditableItem)e.Item; TextBox txt = (TextBox)item[col.UniqueName].Controls[0]; string value = txt.Text; txt.Visible = true; // checkboxlist roles if (col.UniqueName == "RoleName") { CheckBoxList checks = new CheckBoxList(); checks.SelectedValue = value; checks.DataSourceID = "sql_ds_funcao"; checks.DataTextField = "RoleName"; checks.DataValueField = "RoleName"; checks.ID = "CBLRole"; txt.Visible = false; item[col.UniqueName].Controls.Add(checks); } } } }I cant get if the item is checked in editmode, because previous code transforms all in textbox
but if should avoid this field because is not of type string as I specify in frist If in code
<telerik:GridCheckBoxColumn DataField="Aproved" HeaderText="Aprovado"
UniqueName="Aprovado" DataType="System.Boolean" >
</telerik:GridCheckBoxColumn>
invalid cas exception was unhandled by user code
not posible to associate the type of object textbox to checkbox
I'm not clear about your requirement,below is the code to access a GridCheckBoxColumn,in ItemDataBound event and UpdateCommand event.
ASPX:
<telerik:GridCheckBoxColumn DataField="IsTrue" HeaderText="IsTrue" UniqueName="IsTrue" DataType="System.Boolean"></telerik:GridCheckBoxColumn>C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) { if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditableItem edit = (GridEditableItem)e.Item; DataRowView row = (DataRowView)edit.DataItem; CheckBox chk = (CheckBox)edit["IsTrue"].Controls[0]; //Access the GridCheckBoxColumn by its UniqueName string value = row["IsTrue"].ToString(); //Access the value } } protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) { if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; CheckBox checkedItems = (CheckBox)data["IsTrue"].Controls[0]; bool val = checkedItems.Checked; //Value of the CheckBox } }Thanks,
Princy
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) { foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns) { if (col.DataType == typeof(string)) { if (e.Item is GridEditableItem && e.Item.IsInEditMode) { //transforma todos os campos de edit mode em textbox a exepcao dos abaixo indicados e os directamente //declarados em design mode dentro da tag <columns> GridEditableItem item = (GridEditableItem)e.Item; TextBox txt = (TextBox)item[col.UniqueName].Controls[0]; string value = txt.Text; txt.Visible = true; // checkboxlist roles if (col.UniqueName == "RoleName") { CheckBoxList checks = new CheckBoxList(); checks.SelectedValue = value; checks.DataSourceID = "sql_ds_funcao"; checks.DataTextField = "RoleName"; checks.DataValueField = "RoleName"; checks.ID = "CBLRole"; txt.Visible = false; checks.ClientIDMode = ClientIDMode.Static; item[col.UniqueName].Controls.Add(checks); } }protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) {string values1 = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data1 = e.Item as GridEditFormItem; CheckBoxList checkedItems = (CheckBoxList)data1.FindControl("CBLRole");//Accessing the Template CheckBoxList for (int i = 0; i < checkedItems.Items.Count; i++) { if (checkedItems.Items[i].Selected) { values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values } } string selectedvalue = values.Trim(',');//To trim off the last ',' } }When you add the "CheckBoxList" in the "ItemDataBound" server-side event handler of the grid, the newly added control will not be present in the "Controls" collection in the "UpdateCommand" event handler. In order to be able to access the "CheckBoxList" control, you could move your logic from the "ItemDataBound" to the "ItemCreated" server-side event handler.
Nevertheless, I would recommend that you add the "CheckBoxList" control in the "EditItemTemplate" instead and populate it in the "ItemDataBound" event handler.
Hope that helps.
Regards,
Konstantin Dikov
Telerik
Please try the following code snippet.
C#:
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) { GridEditableItem item = (GridEditableItem)e.Item; TextBox txt = (TextBox)item[col.UniqueName].Controls[0];//Access the textbox in editmode string value = txt.Text; if (col.UniqueName == "ShipCountry") { txt.Visible = false; CheckBoxList checks = new CheckBoxList(); checks.DataSourceID = "SqlDataSource2"; checks.DataTextField = "ShipCountry"; checks.DataValueField = "ShipCountry"; checks.ID = "CBLRole"; item[col.UniqueName].Controls.Add(checks); } else { txt.Visible = true; } } } } }protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) { string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { //Binding values to checkboxlist GridEditableItem edit = (GridEditableItem)e.Item; string Id = edit.GetDataKeyValue("OrderID").ToString(); CheckBoxList check = (CheckBoxList)edit.FindControl("CBLRole"); check.DataSourceID = "SqlDataSource2"; check.DataTextField = "ShipCountry"; check.DataValueField = "ShipCountry"; check.SelectedValue = DataBinder.Eval(edit.DataItem, "ShipCountry").ToString(); check.DataBind(); } }protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) { string values = string.Empty; if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditFormItem data = e.Item as GridEditFormItem; CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CBLRole");//Accessing the Template CheckBoxList for (int i = 0; i < checkedItems.Items.Count; i++) { if (checkedItems.Items[i].Selected) { values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values } } string selectedvalue = values.Trim(','); } }Thanks,
Princy
