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

Default FormTemplate field value

1 Answer 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff Reinhardt
Top achievements
Rank 1
Jeff Reinhardt asked on 02 Jun 2010, 07:50 PM
I am using a FormTemplate for insert/update of items in a grid.

<telerik:RadComboBox ID="Location" Style="float: left; font-weight: bold;" skin="WebBlue" SelectedValue='<%# Bind( "Location" ) %>' runat="server" > 
    <Items> 
       <telerik:RadComboBoxItem Text="Fuquay" Value="Fuquay" /> 
       <telerik:RadComboBoxItem Text="Ogden" Value="Ogden" /> 
    </Items> 
</telerik:RadComboBox> 
And
<telerik:RadTextBox ID="EnteredBy" skin="WebBlue" MaxLength="50" Text='<%# Bind( "EnteredBy" ) %>' runat="server" /> 

I want to default these fields after the first items is added to the grid to the values entered during the first insert.

To capture the values I have added this in the ItemInserted method:

if (Session["EnteredBy"] == null)  
                {  
                    RadTextBox EnteredBy = (RadTextBox)e.Item.FindControl("EnteredBy");  
                    if (EnteredBy != null)  
                    {  
                        Session["EnteredBy"] = EnteredBy.Text;  
                    }  
                }  
                if (Session["Location"] == null)  
                {  
                    RadComboBox Location = (RadComboBox)e.Item.FindControl("Location");  
                    if (Location != null)  
                    {  
                        Session["Location"] = Location.SelectedValue;  
                    }  
                } 

This does capture the values perfectly.

I then added this to the ItemCreated method:

if (e.Item is GridEditFormItem)  
            {  
                RadTextBox EnteredBy = (RadTextBox)e.Item.FindControl("EnteredBy");  
                if (EnteredBy != null)  
                {  
                    if (Session["EnteredBy"] != null)  
                    {  
                        EnteredBy.Text = Session["EnteredBy"].ToString();  
                    }  
                }  
                RadComboBox Location = (RadComboBox)e.Item.FindControl("Location");  
                if (Location != null)  
                {  
                    if (Session["Location"] != null)  
                    {  
                        Location.SelectedValue = Session["Location"].ToString();  
                    }  
                    else  
                    {  
                        Location.SelectedValue = "Fuquay";  
                    }  
                }  
            } 

This does work and sets intended value, however, after that the bind ( ie Bind( "EnteredBy" ) ) on the control fires and the intended values are discarded.

The bind has to be there for edits, and the initial insert fails if they are not there complaining of null values, so how can I only bind on initial insert and edit?


1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Jun 2010, 07:07 AM
Hello Jeff,

Try the following code in ItemDataBound event instead of ItemCreated event and see whether it helps you.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
                RadTextBox EnteredBy = (RadTextBox)e.Item.FindControl("EnteredBy");   
                if (EnteredBy != null)   
                {   
                    if (Session["EnteredBy"] != null)   
                    {   
                        EnteredBy.Text = Session["EnteredBy"].ToString();   
                    }   
                }   
                RadComboBox Location = (RadComboBox)e.Item.FindControl("Location");   
                if (Location != null)   
                {   
                    if (Session["Location"] != null)   
                    {   
                        Location.SelectedValue = Session["Location"].ToString();   
                    }   
                    else   
                    {   
                        Location.SelectedValue = "Fuquay";   
                    }   
                }   
           }  
      } 
   

Regards,
Shinu.
Tags
Grid
Asked by
Jeff Reinhardt
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or