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

[Solved] FindControl in FormTemplate external of grid events

2 Answers 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Reuven
Top achievements
Rank 1
Reuven asked on 06 Jul 2009, 07:42 AM
I am trying to access controls within the formtemple on an "external event" (happens to be a datasource_inserting, but can be anything).

All I have read so far have been on itembound/itemcreated or when a control within the grid raises the event.

Any suggestions?

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Jul 2009, 08:15 AM
Hi Reuven,

Try the following code snippet in order to access the controls placed in FormTemplate.

ASPX:
 
<EditFormSettings EditFormType="Template">  
    <FormTemplate> 
        <formtemplate>   
                . . .  
              <asp:DropDownList ID="Ship" DataSourceID="SqlDataSource2" runat="server" DataValueField ="ShipName" DataTextField ="ShipName"/> <br/><br />   
              <asp:Button ID="btnSubmit" runat ="server" Text ="Submit" />   
              <asp:Button ID="btnCancel" runat ="server" Text ="Cancel" />   
        </formtemplate> 
    </FormTemplate> 
</EditFormSettings> 

C#:
 
protected void Button2_Click(object sender, EventArgs e)  
{  
    foreach(GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))  
    {  
        if (item.IsInEditMode)  
        {  
            DropDownList dropDown = (DropDownList)item.FindControl("Ship"); // Access the dropdownliast placed in form template  
            dropDown.SelectedIndex = 3;  
        }  
    }  

-Princy.
0
Reuven
Top achievements
Rank 1
answered on 06 Jul 2009, 08:42 AM
Perfect! Thank you so much.

I like keeping the code clean, using automatic updates, and just injecting the datasource parameters into the inserting/updating events. Your code was what I was missing.
Tags
Grid
Asked by
Reuven
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Reuven
Top achievements
Rank 1
Share this question
or