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

Dynamic edit form

9 Answers 220 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dima
Top achievements
Rank 1
dima asked on 21 Sep 2009, 01:34 PM
hello,
I'm working with radgrid.
could you write any example for creating dynamic edit form template on ItemCreated or ItemDataBound?

thanks a lot!

9 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 Sep 2009, 01:54 PM
Hi Dima,

I have one suggestion on this, you can add a placeholder in the editform template and in the itemcreated event check whether the grid is in the edit mode and create the edit from controls dynamically and add to the place holder/panel.

-shinu
0
dima
Top achievements
Rank 1
answered on 21 Sep 2009, 02:00 PM
Thanks, Shinu
I'll try it
0
dima
Top achievements
Rank 1
answered on 22 Sep 2009, 09:52 AM
Hi,
I tryed create the edit from controls dynamically on itemcreated and I have a problem.
The code of adding is:

protected

 

void Grid1_ItemCreated(object sender, GridItemEventArgs e)

 

{

 

    if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))

 

    {

 

        GridEditFormItem editform = (GridEditFormItem)e.Item;

 

 

        Panel pnl = (Panel)editform.FindControl("pnlEdit");        
        pnl.Controls.Add(CreateEditFormTable(
));

 

    }

}

Also, I added update button:

 

ImageButton

 

btnUpdate = new ImageButton();

 

btnUpdate.ID =

"btnUpdate";

 

btnUpdate.CommandName =

"Update";
........

The problem is... when I press Update button I entered in Grid1_ItemCreated function again and it creates new editform....

Any suggestion?

P.S. sorry for my english :)

Thank you

 

0
Accepted
Princy
Top achievements
Rank 2
answered on 22 Sep 2009, 11:09 AM
Hello Dima,

The ItemCreated event will be fired after each page postback. I tried the following code to implement the same scenario and it worked as expected. Check with this code for differences and try it out:
aspx:
<EditFormSettings EditFormType="Template"
     <FormTemplate> 
         <asp:Panel ID="pnlEdit" runat="server"
            EditForm 
         </asp:Panel>                           
     </FormTemplate> 
</EditFormSettings> 

c#:
  protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode)) 
        { 
            GridEditFormItem editform = (GridEditFormItem)e.Item; 
            Panel pnl = (Panel)editform.FindControl("pnlEdit"); 
            pnl.Controls.Add(CreateEditFormTable()); 
        }         
    } 
 
    private Control CreateEditFormTable() 
    { 
        ImageButton btnUpdate = new ImageButton(); 
        btnUpdate.ID = "btnUpdate"
        btnUpdate.ImageUrl = "~/Images/Image1.bmp";         
        btnUpdate.CommandName = "Update"
        Table table = new Table(); 
        table.ID = "table1"
        TableRow row = new TableRow(); 
        table.Rows.Add(row); 
        TableCell cell = new TableCell(); 
        cell.Controls.Add(btnUpdate); 
        table.Rows[0].Cells.Add(cell); 
 
        return table;  
    } 

Hope this helps..
Princy.
0
dima
Top achievements
Rank 1
answered on 22 Sep 2009, 01:09 PM
Thank you, Princy !
Well working!
0
Jan Balas
Top achievements
Rank 1
answered on 20 Feb 2010, 09:50 PM
Hi Princy,
your code works great, but I have a problem accessing the generated control in InsertCommand . Do you have any idea how to get arround this problem?

protected void rg_InsertCommand(object source, GridCommandEventArgs e) 
    { 
        GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item; 
        Panel pnlEdit = gridEditFormItem.FindControl("pnlEdit"as Panel; 
 
 
        Control x = pnlEdit.FindControl("table1"); 
    }
0
Princy
Top achievements
Rank 2
answered on 22 Feb 2010, 07:27 AM
Hi,

Please make sure that you add the controls both in the Item Created and ItemDataBound event as shown below:

protected void RadGrid1_ItemDaatBound(object sender, GridItemEventArgs e)  
    {  
        if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))  
        {  
            GridEditFormItem editform = (GridEditFormItem)e.Item;  
            Panel pnl = (Panel)editform.FindControl("pnlEdit");  
            pnl.Controls.Add(CreateEditFormTable());  
        }          
    } 


Thanks,
Princy
0
Sunil Kaim
Top achievements
Rank 1
answered on 05 Mar 2013, 01:16 PM
Hi i m doing the same stuff,and my edit item template is generated as per my requirement but upon clicking on the update button nothing is happen and update command is not fired.
This is the code snippet of Grid's itemdatabound event is here
if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
            {
                GridEditFormItem editform = (GridEditFormItem)e.Item;
                Panel pnl = (Panel)editform.FindControl("pnlEdit");
                Label lbldesc = new Label();
                lbldesc.ID = "lblDesc";
                lbldesc.Text = "Description";
                TextBox txtdesc = new TextBox();
                txtdesc.ID = "txtDesc";
                txtdesc.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;
                txtdesc.Text = dataItem.Description;
                Table table = new Table();
                table.ID = "table1";
                TableRow row = new TableRow();
                table.Rows.Add(row);
                TableCell cell = new TableCell();
                cell.Controls.Add(lbldesc);
                table.Rows[0].Cells.Add(cell);
                cell = new TableCell();
                cell.Controls.Add(txtdesc);
                table.Rows[0].Cells.Add(cell);

                TableRow row1 = new TableRow();
                table.Rows.Add(row1);
                Label lblvalue = new Label();
                lblvalue.ID = "lblValue";
                lblvalue.Text = "Value";

                cell = new TableCell();
                cell.Controls.Add(lblvalue);
                table.Rows[1].Cells.Add(cell);

                if (dataItem.tbl_haa_Controls.key.Equals(ControlsKey.TextBox.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    TextBox txtValue = new TextBox();
                    txtValue.ID = "txtvalue";
                    txtValue.Text = dataItem.Value;
                    cell = new TableCell();
                    cell.Controls.Add(txtValue);
                    table.Rows[1].Cells.Add(cell);
                }


                if (dataItem.OptionValue != null)
                {

                    XmlDocument xm = new XmlDocument();
                    xm.LoadXml(dataItem.OptionValue);
                    XmlNodeList xNodeList = xm.DocumentElement.SelectNodes("Option");
                    var addrs = new List<KeyValuePair<string, string>>();

                    foreach (XmlNode xNode in xNodeList)
                    {
                        var xAddr = xNode.InnerText;
                        var xLatLng = xNode.InnerText;
                        addrs.Add(new KeyValuePair<string, string>(xAddr, xLatLng));
                    }

                    if (dataItem.tbl_haa_Controls.key.Equals(ControlsKey.DropDownList.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        DropDownList ddl = new DropDownList();
                        ddl.ID = "ddlvalue";
                        ddl.Width = Unit.Pixel(180);
                        ddl.DataSource = addrs;
                        ddl.DataTextField = "Key";
                        ddl.DataValueField = "Value";
                        ddl.DataBind();
                        cell = new TableCell();
                        cell.Width = Unit.Pixel(220);
                        cell.Controls.Add(ddl);
                        table.Rows[1].Cells.Add(cell);
                        ddl.Items.FindByText(dataItem.Value).Selected = true;

                    }

                    else if (dataItem.tbl_haa_Controls.key.Equals(ControlsKey.RadioButton.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        RadioButtonList btnlist = new RadioButtonList();
                        btnlist.RepeatDirection = RepeatDirection.Horizontal;
                        btnlist.ID = "btnvalue";
                        btnlist.DataSource = addrs;
                        btnlist.DataValueField = "Key";
                        btnlist.DataTextField = "Value";
                        btnlist.DataBind();
                        cell = new TableCell();
                        cell.Width = Unit.Pixel(150);
                        cell.Controls.Add(btnlist);
                        table.Rows[1].Cells.Add(cell);
                        btnlist.Items.FindByText(dataItem.Value).Selected = true;
                    }

                    else if (dataItem.tbl_haa_Controls.key.Equals(ControlsKey.CheckBox.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        CheckBoxList chklist = new CheckBoxList();
                        chklist.RepeatDirection = RepeatDirection.Vertical;
                        chklist.ID = "chkList";
                        chklist.DataSource = addrs;
                        chklist.DataValueField = "Key";
                        chklist.DataTextField = "Value";
                        chklist.DataBind();
                        cell = new TableCell();
                        cell.Width = Unit.Pixel(180);
                        cell.Controls.Add(chklist);
                        table.Rows[1].Cells.Add(cell);
                        var arrValue = dataItem.Value.Split(',');
                        for (int i = 0; i < arrValue.Count(); i++)
                        {
                            if (chklist.Items.FindByText(arrValue[i]) != null)
                                chklist.Items.FindByText(arrValue[i]).Selected = true;
                        }
                    }
                }

                Button btnupdate = new Button();               
                btnupdate.ID = "btnupdate";
                btnupdate.CommandName = "Update";
                btnupdate.Text = "Update";
                row1 = new TableRow();
                table.Rows.Add(row1);

                cell = new TableCell();
                cell.Controls.Add(btnupdate);
                table.Rows[2].Cells.Add(cell);

                Button btnCancel = new Button();
                btnCancel.ID = "btncancel";
                btnCancel.CommandName = "Cancel";
                btnCancel.Text = "Cancel";
                cell = new TableCell();
                cell.Controls.Add(btnCancel);
                table.Rows[2].Cells.Add(cell);
                pnl.Controls.Add(table);
            }
0
Shinu
Top achievements
Rank 2
answered on 06 Mar 2013, 05:20 AM
Hi,

Try adding the button in ItemCreated event.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            Button btnupdate = new Button();
           btnupdate.CommandName = "Update";
            btnupdate.Text = "Update";
             row1 = new TableRow();
             table.Rows.Add(row1);
        }
}

Thanks,
Shinu
Tags
Grid
Asked by
dima
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
dima
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Jan Balas
Top achievements
Rank 1
Sunil Kaim
Top achievements
Rank 1
Share this question
or