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

How to set the default value of a dropdownlist for new records

3 Answers 976 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark Breen
Top achievements
Rank 1
Mark Breen asked on 10 Mar 2010, 01:12 PM
Hello All,

re: Telerik.Web.UI 2009.3.1103.35 and the radGrid

I am using the radgrid and would like to set the default value of a few text controls and dropdownlists for the user when they click Add New Record.

I see lots of complex examples when I search, but all I want to do is just set an initial value to zero, and the user can change it if they wish.  How can I do this?

As far as I can see, the insert event happens when the user clicks update, so I guess I need to set the value prior to the form loading?  Can anyone assist in a) the event I need and b) a few lines of the code required

Thanks in advance for your assistance,

PS As usual I have a demo tomorrow and am trying to get this going today!

Mark


3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Mar 2010, 01:55 PM
Hello Mark,

The following code snippet shows how to set default value in textbox when grid in insert mode.

C#:
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)   
    {   
        if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridEditFormInsertItem)   
        {   
            GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;   
            TextBox textBox = (TextBox)item["ColumnUniqueName"].Controls[0];   
            textBox.Text = "My Text";             
        }    
    }   

Access the dropdown controls as DropDownList or RadComboBox (according to your PickerType setting) and set the SelectedValue.

Thanks,
Princy.
0
Mark Breen
Top achievements
Rank 1
answered on 10 Mar 2010, 02:11 PM
Hello Princy,

thank you for your prompt and accurate response.  I am pleased to say that I did manage to get the setting of the text box default values working in the last fifteen minutes.  However, if you take a look the code I have posted below,  you can see that I have to rem out the line that creates the RadComboBox variable.  If I rem this out, the form opens.

You can see the text box is pre-populated with "Hello MaryLou", but i cannot do the same with the RadComboBox.  What is wrong with my syntax for the RadCombo?  I checked and it is a RadCombo that I am using.  I believe the problem is with the following line of code

RadComboBox BoatCraftTypId = (RadComboBox)item["BoatCraftTypId"].Controls[0]; 


Here is the entire snippit, thanks again Princy.
Mark

 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            // This code turns off the Column for the foreign key 
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode)) 
            { 
                GridEditFormItem editform = (GridEditFormItem)e.Item; 
                editform["OwnersName"].Parent.Visible = false
 
                GridEditableItem item = (GridEditableItem)e.Item; 
 
                TextBox VesselName = (TextBox)item["VesselName"].Controls[0]; 
                VesselName.Text = "Hello MaryLou"
 
                //RadComboBox BoatCraftTypId = (RadComboBox)item["BoatCraftTypId"].Controls[0]; 
                //BoatCraftTypId.SelectedValue = "0"; 
 
                 
            } 
 

0
Mark Breen
Top achievements
Rank 1
answered on 10 Mar 2010, 03:06 PM
Hello Again Princy,

Just as I was about to give up and move to another piece of my system, I thought about one last thing to consider.  The unique name I had given the RadCombo.  It is my habit to set the header text to be equal to the looked up value, (Craft Type  is more meaningful as a header that BoatCraftTypeId).

So, usually when I name the header, I copy and paste and set that as the unique name also - to keep them matching.  Of course the previous code could not work because there was no column named BoatCraftTypeId in the Grid.  When I changed the code to CraftType, it works a treat.

Thanks Princy for the super prompt support that seems to be the norm for Telerik.  It is a joy to use the Telerik products and deal with Telerik support teams.

Mark Breen
Ireland


Tags
Grid
Asked by
Mark Breen
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mark Breen
Top achievements
Rank 1
Share this question
or