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

[Solved] Reach RadTextBox inide the RadGrid after Postback

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hakan
Top achievements
Rank 1
Hakan asked on 30 May 2013, 12:53 PM
I am experiencing a problem about the postback logic on RadGrid.

I dynamically create and send columns to the RadGrid object. I put RadTextBoxes in the grid, and fill them with information. I also name the RadTextBoxes and I am able to reach themusing this code:

 foreach (GridDataItem dataItem in grdSource.MasterTableView.Items)
            {
                if (dataItem.ItemType == GridItemType.Item)
                {
                    foreach (GridTemplateColumn column in grdSource.Columns)
                    {
                        RadTextBox txtField = (RadTextBox)dataItem.FindControl("txtColumnName");
                        if (txtField != null)
                            txtField.Text =/*  Stuff I will do */;
                    }
                }
            }

This code works well, if I run it right after I fill the grid. 

But here is the problem: After all these done, I change something on the textbox, then I click some button and do a postback, If I put the same code to the click event of the button, then this code doesn't work. And my RadGrid lost all its data.

What should I do?

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 May 2013, 11:27 AM
Hello Hakan,

Here is a code i have tried,please check this. I guess you want to perform certain function on an external button click event.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" />

C#:
protected void Page_Init(object source, System.EventArgs e)
   {
       RadGrid RadGrid1 = new RadGrid();
       RadGrid1.ID = "RadGrid1";
       RadGrid1.DataSourceID = "SqlDataSource1";
       RadGrid1.AutoGenerateColumns = false;    
       GridBoundColumn boundColumn;
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "CustomerID";
       boundColumn.HeaderText = "CustomerID";
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "ContactName";
       boundColumn.HeaderText = "Contact Name";
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       string templateColumnName = "OrderID";
       GridTemplateColumn templateColumn = new GridTemplateColumn();
       templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
       templateColumn.HeaderText = templateColumnName;
       RadGrid1.MasterTableView.Columns.Add(templateColumn);
       this.PlaceHolder1.Controls.Add(RadGrid1);
   }
 
   private class MyTemplate : ITemplate
   {
       protected RadTextBox textbox;
       private string colname;
       public MyTemplate(string cName)
       {
           colname = cName;
       }
       public void InstantiateIn(System.Web.UI.Control container)
       {
           textbox = new RadTextBox();
           textbox.ID = "TextBox1";
           container.Controls.Add(textbox);
       }  
   }
   protected void Button1_Click1(object sender, EventArgs e)
   {
       RadGrid radgrid1 =(RadGrid)PlaceHolder1.FindControl("RadGrid1");
       foreach (GridDataItem dataItem in radgrid1.MasterTableView.Items)
       {
                  RadTextBox txtField = (RadTextBox)dataItem.FindControl("TextBox1");
                  if (txtField != null)
                  txtField.Text =/*  Stuff I will do */;
       }
   }

Thanks,
Princy
Tags
Grid
Asked by
Hakan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or