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

Accessing Controls in Insert Form

5 Answers 247 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marlayna
Top achievements
Rank 1
Marlayna asked on 26 Jun 2008, 12:07 AM
Inside my RadGrid Edit Form I have a checkbox that toggles other fields in the form Enabled property. Following advice posted in another thread involving using the OnItenDataBound and OnPreRender, I am able to accomplish this with no issues when in edit mode; however, the control collections are empty when in insert mode.

protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)  
{  
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
  {  
     GridEditableItem item = e.Item as GridEditableItem;  
     CheckBox cb = (CheckBox)item["Toggler"].Controls[0];  
     cb.AutoPostBack = true;  
  }  

protected void Grid_PreRender(object sender, System.EventArgs e)  
{  
   foreach (GridEditFormItem item in rgrdList.MasterTableView.GetItems(GridItemType.EditFormItem))  
   {  
     if (item.IsInEditMode)  
     {  
        bool isToggled = ((CheckBox)item["Toggler"].Controls[0]).Checked;  
        ((TextBox)item["TextItem1"].Controls[0]).Enabled = isToggled;  
        ((TextBox)item["TextItem2"].Controls[0]).Enabled = isToggled;  
        ((TextBox)item["TextItem3"].Controls[0]).Enabled = isToggled;  
     }    
   }  

What am I missing? Any suggestions would be greatly appreciated.

Thank you,

Marlayna

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Jun 2008, 05:11 AM
Hi Marlayna,

Try accessing the controls in Insert mode as shown below.

CS:
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if((e.Item is GridEditFormInsertItem)&&(e.Item.OwnerTableView.IsItemInserted)) 
        { 
          GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;   
          CheckBox cb = (CheckBox)item["Toggler"].Controls[0];   
          cb.AutoPostBack = true;   
        } 
    } 


Thanks
Shinu.
0
Marlayna
Top achievements
Rank 1
answered on 26 Jun 2008, 04:41 PM
Hi Shinu,

Thank you, I tried it and no go. Item["Toggler"]'s control collection is still empty. Could it be that I am working with a really old version of the RadGrid(3.1.1.0)? If so, is there a hot fix I can use? I am constrained in being able to upgrade to the lastest.

Thank you,

Marlayna
0
Shinu
Top achievements
Rank 2
answered on 27 Jun 2008, 05:58 AM
Hi,

Have you set the proper ColumnUniqueName in the aspx? Sending your aspx will be more good.

Shinu.
0
Marlayna
Top achievements
Rank 1
answered on 04 Jul 2008, 11:09 PM
Hi Shinu,

Yes, I have set the columns UniqueName properties. Below is my aspx snippet of the RadGrid and the OnInit event handler. As you can see, I am dynamically creating the columns from an xml file in the OnInit. I have no other issues except that the control collection is empty when inserting.

The aspx snippet:
<rad:RadGrid ID="rgrdList" runat="server" EnableAJAX="true" EnableAsyncRequests="true"  AutoGenerateColumns="false"   
    OnInit="rgrdList_Init" OnNeedDataSource="rgrdList_NeedDataSource" OnPreRender="rgrdList_PreRender" 
    OnItemDataBound="rgrdList_ItemDataBound" OnItemCommand="rgrdList_ItemCommand" 
    OnInsertCommand="rgrdList_InsertCommand" OnUpdateCommand="rgrdList_UpdateCommand" OnDeleteCommand="rgrdList_DeleteCommand"   
    Width="1000px" Height="800px">  
  <MasterTableView AutoGenerateColumns="false" EditMode="EditForms" CommandItemDisplay="Top" ShowHeadersWhenNoRecords="true" > 
    <CommandItemTemplate> 
      <span><asp:LinkButton ID="btnInsert" runat="server" CommandName="InitInsert" Text="Add New Record" /></span>  
      <span style="margin-left:50px;"><asp:LinkButton ID="btnSave" runat="server" CommandName="Save" Text="Save All Changes" /></span>  
      <rad:RadWindowManager ID="RadWindowManager1" runat="server">  
        <Windows> 
     <rad:RadWindow ID="RadWindow1" runat="server" Title="List Management - Validation Failed" NavigateUrl="ListErrorMessage.aspx" Height="515px" Width="525" VisibleOnPageLoad="false" /> 
        </Windows> 
      </rad:RadWindowManager> 
    </CommandItemTemplate> 
  </MasterTableView> 
  <ClientSettings> 
    <Resizing AllowColumnResize="True" EnableRealTimeResize="True" ResizeGridOnColumnResize="True"></Resizing> 
    <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" ScrollHeight="500px"></Scrolling> 
    <Selecting AllowRowSelect="True"></Selecting> 
  </ClientSettings> 
</rad:RadGrid>       

The OnInit handler:
protected void rgrdList_Init(object sender, EventArgs e)  
{  
  if (ListFile == null) return;  
 
  rgrdList.MasterTableView.Columns.Clear();  
 
  GridBoundColumn col = new GridBoundColumn();  
  col.UniqueName = "RowIndex";  
  col.DataField = "RowIndex";  
  //some formatting & added GridTextBoxColumnEditor  
  rgrdList.MasterTableView.Columns.Add(col);  
    
  GridEditCommandColumn ecol = new GridEditCommandColumn();  
  ecol.EditText = "Edit";  
  ecol.InsertText = "Insert";  
  //some formatting  
  rgrdList.MasterTableView.Columns.Add(ecol);  
    
  XmlNode node = ListFile.SelectSingleNode("Grid/GridModel");  
 
  GridCheckBoxColumn ccol = new GridCheckBoxColumn();  
  ccol.UniqueName = "Toggler";  
  ccol.DataField = "Toggler";  
  //some formatting & added GridCheckBoxColumnEditor  
  rgrdList.MasterTableView.Columns.Add(ccol);  
 
  foreach (XmlNode item in node.ChildNodes)  
  {  
    if (item.Attributes["Title"] != null && !item.Attributes["Title"].Value.Equals(""))  
    {  
      col = new GridBoundColumn();  
      col.UniqueName = item.Name;  
      col.DataField = item.Name;  
      //some formatting & added GridTextBoxColumnEditor  
      rgrdList.MasterTableView.Columns.Add(col);  
    }  
  }  
 
  GridButtonColumn bcol = new GridButtonColumn();  
  bcol.UniqueName = "btnDelete";  
  bcol.ButtonType = GridButtonColumnType.LinkButton;  
  bcol.Text = "Delete";  
  bcol.CommandName = "Delete";  
  bcol.ConfirmText = "Are you sure you want to delete this record?";  
  //some formatting  
  rgrdList.MasterTableView.Columns.Add(bcol);  

Thank you,

Marlayna
0
Shinu
Top achievements
Rank 2
answered on 05 Jul 2008, 05:03 AM
Hi,

Try adding the columns to the column collection first and then set its properties in the PageLoad event.

CS:
private void Page_Load(object sender, System.EventArgs e) 
 if ( !IsPostBack ) 
 { 
   GridBoundColumn boundColumn; 
 
   //Important: first Add column to the collection 
   boundColumn = new GridBoundColumn(); 
   this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
 
   //Then set properties 
   boundColumn.DataField = "CustomerID"
   boundColumn.HeaderText = "CustomerID"
   boundColumn.UniqueName"CustomerID"
 } 


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