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

get data inside edit item template

11 Answers 768 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Troika
Top achievements
Rank 1
Troika asked on 19 Sep 2013, 02:29 PM
i have a radgrid but i cant get the textbox texto on update, so i will put the code that i'm using , what am i missing here.

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

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Sep 2013, 04:03 AM
Hi Troika,

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
0
Troika
Top achievements
Rank 1
answered on 20 Sep 2013, 08:12 AM
ok it worked like this: (i had used 2 lines because i cant find a way to get in one line)

  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


0
Troika
Top achievements
Rank 1
answered on 20 Sep 2013, 09:16 AM
code generated is:

<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

0
Princy
Top achievements
Rank 2
answered on 21 Sep 2013, 06:52 AM
Hi Troika,

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
0
Troika
Top achievements
Rank 1
answered on 21 Sep 2013, 09:19 AM
my updatecommand its equal to yours:

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);
 
                       }
                
               }
           }
       }
0
Troika
Top achievements
Rank 1
answered on 21 Sep 2013, 09:51 AM
due to the above code (previous post)

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

0
Princy
Top achievements
Rank 2
answered on 23 Sep 2013, 05:13 AM
Hi Troika,

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
0
Troika
Top achievements
Rank 1
answered on 23 Sep 2013, 05:41 PM
i'm using the checkboxlist on update command to read wich checks are marked but i always get   " Cannot find a cell bound to column name  'CBLRole " i'm creating that manually on item databound  check this: (always returns null)

      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 ','     
 
 
 
              }
 
 
  }


0
Konstantin Dikov
Telerik team
answered on 24 Sep 2013, 10:16 AM
Hello Troika,

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
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 01 Oct 2013, 08:15 AM
rhat is what i'm doing i'm populating it on item template and it just appears in edit mode, but i cant get the checkboxlist items checked it cant find the checkboxlist
0
Princy
Top achievements
Rank 2
answered on 01 Oct 2013, 12:40 PM
Hi Troika,

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
Tags
Grid
Asked by
Troika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Troika
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or