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

[Solved] access data from templateform in code behind

3 Answers 221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Santaji Garwe
Top achievements
Rank 1
Santaji Garwe asked on 30 Jun 2008, 11:43 AM
hi
i am trying to access data from template form control after firing update command but not able to access it in code behind
follwing is the code


----------------

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        ParameterCategoryList pclist = ParameterCategoryList.NewParameterCategoryList();
        pclist = ParameterCategoryList.GetParameterCategoryList(new Guid("4EF52DD9-6875-4ACE-A176-3AAACE78A502"), 23315);
        RadGrid1.DataSource = pclist;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
        {
            GridEditableItem geditem = (GridEditableItem)e.Item;
            geditem["CategoryId"].Controls.Clear();
            geditem["ShortName"].Controls.Clear();
            DropDownList DD = new DropDownList();
            CheckBoxList CL = new CheckBoxList();
            CL.ID = "CL";
            DD.ID = "DD";
            geditem["CategoryId"].Controls.Add(DD);
            geditem["ShortName"].Controls.Add(CL);
        }
    }
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem eItem = e.Item as GridEditableItem;
       
        string strtxt = eItem.GetDataKeyValue("ShortName").ToString();
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach (GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
        {
            if (item.IsInEditMode)
            {
                DropDownList DDL = (DropDownList)item["CategoryID"].FindControl("DD");
                CheckBoxList CBL = (CheckBoxList)item["ShortName"].FindControl("CL");
                CBL.DataSource = AvailableRecordStatusList.GetStatusList();
                CBL.DataTextField = "value";
                CBL.DataValueField = "key";
                CBL.DataBind();
                DDL.DataSource = AvailableRecordStatusList.GetStatusList();
                DDL.DataTextField = "value";
                DDL.DataValueField = "key";
                DDL.DataBind();
            }
        } 
    }
}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Jul 2008, 07:29 AM
Hello Santaji,

You can access controls in the FormTemplate as GridEditFormItem, as shown in the code snippet below.

ASPX:
<EditFormSettings> 
               <FormTemplate> 
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                    
                    <asp:Button ID="Button3" CommandName="Update" runat="server" Text="Update"/> 
               </FormTemplate> 
</EditFormSettings> 

CS:
  protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.Item is GridEditFormItem) 
        { 
            GridEditFormItem item = (GridEditFormItem)e.Item; 
            TextBox txtbx = (TextBox)item.FindControl("TextBox1"); 
             
        } 
    } 

Thanks
Princy.
0
Santaji Garwe
Top achievements
Rank 1
answered on 01 Jul 2008, 09:15 AM
but i am adding the controls dynamically in RadGrid1_ItemDataBound
event and then binding them to datasource in Pre render event
So i am not able to access them as mentioned by u .

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
        {
            GridEditableItem geditem = (GridEditableItem)e.Item;
            geditem["CategoryId"].Controls.Clear();
            geditem["ShortName"].Controls.Clear();
            DropDownList DD = new DropDownList();
            CheckBoxList CL = new CheckBoxList();
            CL.ID = "CL";
            DD.ID = "DD";
            geditem["CategoryId"].Controls.Add(DD);
            geditem["ShortName"].Controls.Add(CL);
        }
    }
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        if (e.Item is GridEditFormItem)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            DropDownList txtbx = (DropDownList)item.FindControl("DD");
            TextBox txtbxq = (TextBox)item.FindControl("TextBox1");

        }
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach (GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
        {
            if (item.IsInEditMode)
            {
                DropDownList DDL = (DropDownList)item["CategoryID"].FindControl("DD");
                CheckBoxList CBL = (CheckBoxList)item["ShortName"].FindControl("CL");
                CBL.DataSource = AvailableRecordStatusList.GetStatusList();
                CBL.DataTextField = "value";
                CBL.DataValueField = "key";
                CBL.DataBind();
                DDL.DataSource = AvailableRecordStatusList.GetStatusList();
                DDL.DataTextField = "value";
                DDL.DataValueField = "key";
                DDL.DataBind();
            }
        } 
0
Shinu
Top achievements
Rank 2
answered on 01 Jul 2008, 11:33 AM
Hi,

Try adding the control in the edit mode in the ItemCreated event and try binding the newly added control in the ItemDataBound event.

CS:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) 
        { 
            GridEditableItem edititem = (GridEditableItem)e.Item; 
            edititem["Project_Name"].Controls.Clear(); 
            DropDownList ddl = new DropDownList(); 
            
            ddl.ID = "DropDownList1"
            edititem["Project_Name"].Controls.Add(ddl); 
          } 
      }   
 


 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) 
        { 
            GridEditableItem editItem = (GridEditableItem)e.Item; 
            DropDownList ddl = (DropDownList)editItem["Project_Name"].Controls[0]; 
            // Bind the DropDown here 
        } 
        
    } 
 
 
 


Thanks
Shinu.
Tags
Grid
Asked by
Santaji Garwe
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Santaji Garwe
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or