Finding a control in the RadGridView.EditFormSettings.FormTemplate

8 posts, 1 answers
  1. Michael
    Michael avatar
    7 posts
    Member since:
    Oct 2010

    Posted 09 Aug 2011 Link to this post

    I have a page with a RadGrid on it. In that RadGrid I'm using  <EditFormSettings EditFormType="Template"> to handle the editing of the data. In that FormTemplate I have a FileUpload control where I'm allowing users to upload a file. I planned on handling the file upload in a seperate function. Problem is trying to get to the FileUpload control. Can someone help me? Code sample below:
    <telerik:RadGrid 
        ID="rgvwUserList" 
        runat="server" 
        AllowAutomaticDeletes="True" 
        AllowAutomaticInserts="True" 
        AllowAutomaticUpdates="True" 
        AllowFilteringByColumn="True" 
        AllowPaging="True" 
        AllowSorting="True" 
        AutoGenerateColumns="False" 
        CellSpacing="0" 
        DataSourceID="sdsUserList" 
        EnableLinqExpressions="False" 
        GridLines="None" 
        PageSize="20" 
        ShowGroupPanel="True" 
        Width="900px">
        <ClientSettings AllowDragToGroup="True"/>
        <MasterTableView 
            AutoGenerateColumns="False" 
            CommandItemDisplay="None" 
            DataKeyNames="StaffID" 
            DataSourceID="sdsUserList"
            EditMode="EditForms" 
            HorizontalAlign="NotSet" 
            Width="100%">
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderText="Edit" ItemStyle-HorizontalAlign="Center" UniqueName="EditCommandColumn">
                    <ItemStyle CssClass="MyImageButton" Wrap="False" />
                </telerik:GridEditCommandColumn>
                <telerik:GridTemplateColumn AllowFiltering="False">
                    <HeaderStyle HorizontalAlign="Center"/>
                    <HeaderTemplate>Photo</HeaderTemplate>
                    <ItemStyle HorizontalAlign="Center"/>
                    <ItemTemplate>
                        <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%#EVAL("PhotoPath") %>' Width="50"/>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="DisplayName" FilterControlAltText="Filter DisplayName column" HeaderText="DisplayName" ItemStyle-Width="20%" ReadOnly="True" SortExpression="LastName" UniqueName="DisplayName"/>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <div align="center">
                        <asp:FileUpload ID="fuPhotoPath" runat="server" SkinID="FileUpload" Width="290"/>
                        <asp:Button ID="btnAddPhoto" runat="server" CommandArgument='<%#Bind("StaffID")%>' OnCommand="UploadPhoto" SkinID="ButtonLittle" Text="Add"/>
                    </div>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>
  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 10 Aug 2011 Link to this post

    Hello,

    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
               AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" GridLines="None"
               ShowFooter="True" AllowMultiRowSelection="True" PageSize="7" AllowMultiRowEdit="True"
               OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand">
               <MasterTableView EditMode="EditForms" DataKeyNames="ID">
                   <Columns>
                       <telerik:GridBoundColumn DataField="ID" HeaderText="ID">
                       </telerik:GridBoundColumn>
                       <telerik:GridEditCommandColumn UniqueName="EditColumn">
                       </telerik:GridEditCommandColumn>
                   </Columns>
                   <EditFormSettings EditFormType="Template">
                       <FormTemplate>
                           <div align="center">
                               <asp:FileUpload ID="fuPhotoPath" runat="server" Width="290" />
                               <asp:Button ID="btnAddPhoto" runat="server" Text="Add" OnClick="btnAddPhoto_Click" />
                           </div>
                       </FormTemplate>
                   </EditFormSettings>
               </MasterTableView>
           </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                    new { ID = 1, Name ="Name1",TempDate = DateTime.Now},
                    new { ID = 2, Name = "Name2",TempDate = DateTime.Now},
                    new { ID = 3, Name = "Name3",TempDate = DateTime.Now},
                    new { ID = 4, Name = "Name4",TempDate = DateTime.Now},
                    new { ID = 5, Name = "Name5",TempDate = DateTime.Now},
                    new { ID = 6, Name ="Name6",TempDate = DateTime.Now},
                    new { ID = 7, Name = "Name7",TempDate = DateTime.Now},
                    new { ID = 8, Name = "Name8",TempDate = DateTime.Now},
                    new { ID = 9, Name = "Name9",TempDate = DateTime.Now},
                    new { ID = 10, Name = "Name10",TempDate = DateTime.Now},
                    new { ID = 11, Name ="Name11",TempDate = DateTime.Now},
                    new { ID = 12, Name = "Name12",TempDate = DateTime.Now},
                    new { ID = 13, Name = "Name13",TempDate = DateTime.Now},
                    new { ID = 14, Name = "Name14",TempDate = DateTime.Now},
                    new { ID = 15, Name = "Name15",TempDate = DateTime.Now}
                };
     
            RadGrid1.DataSource = data;
        }
        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
     
        }
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
             
        }
        protected void btnAddPhoto_Click(object sender, EventArgs e)
        {
            Button btnAddPhoto = sender as Button;
            GridEditFormItem item = (GridEditFormItem)btnAddPhoto.NamingContainer;
            FileUpload fuPhotoPath = item.FindControl("fuPhotoPath") as FileUpload;
            int ID =Convert.ToInt32(item.GetDataKeyValue("ID"));
        ///.......do your logic here
        }

    let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Aug 2011 Link to this post

    Hello Michael,

    Try the following code snippet.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
         {
            GridEditableItem item = (GridEditableItem)e.Item;
            FileUpload upload = (FileUpload)item.FindControl("fuPhotoPath");
         }
    }

    Thanks,
    Shinu.
  5. Michael
    Michael avatar
    7 posts
    Member since:
    Oct 2010

    Posted 10 Aug 2011 Link to this post

    Jayesh Goyani, you rock. That worked perfectly!
  6. Rohit
    Rohit avatar
    3 posts
    Member since:
    May 2015

    Posted 26 May 2015 Link to this post

    Jayesh Goyani i have same work to do by on page_load() event of the aspx page.

    can u help me with it.

  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 May 2015 in reply to Rohit Link to this post

    Hello Rohit,

    It would be nice if you will do some work in page_prerender event. Because all the control and its value assigned into grid created and itemdatabound event. This event is executed after page-load event.

     Let me know if any concern.

    http://www.telerik.com/help/aspnet-ajax/grid-event-sequence.html

    Thanks,

    Jayesh Goyani

  8. Rohit
    Rohit avatar
    3 posts
    Member since:
    May 2015

    Posted 26 May 2015 in reply to Jayesh Goyani Link to this post

    <telerik:RadGrid ID="radgrdFamilyHealth" runat="server" Width="98%" AutoGenerateColumns="false"
                                                OnNeedDataSource="radgrdFamilyHealth_NeedDataSource" OnUpdateCommand="radgrdFamilyHealth_UpdateCommand"
                                                OnInsertCommand="radgrdFamilyHealth_InsertCommand" OnItemCommand="radgrdFamilyHealth_ItemCommand"
                                                OnDeleteCommand="radgrdFamilyHealth_DeleteCommand"  OnItemDataBound="radgrdFamilyHealth_ItemDataBound"
                                                 >
                                                <MasterTableView CommandItemDisplay="Top" DataKeyNames="FamilyHealthid">
                                                    <CommandItemSettings ShowAddNewRecordButton="true" />
                                                    <ColumnGroups>
                                                        <telerik:GridColumnGroup HeaderText="If Alive" Name="IfAlive" HeaderStyle-HorizontalAlign="Center">
                                                        </telerik:GridColumnGroup>
                                                        <telerik:GridColumnGroup HeaderText="If Deceased (Y/N)" Name="IfDeceased" HeaderStyle-HorizontalAlign="Center"></telerik:GridColumnGroup>
                                                    </ColumnGroups>
                                                    <Columns>
                                                        <telerik:GridEditCommandColumn Visible="false" ButtonType="ImageButton">
                                                        </telerik:GridEditCommandColumn>
                                                        <telerik:GridBoundColumn DataField="FamilyHealthid" HeaderText="Sr.No." SortExpression="FamilyHealthid" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="FamilyHealthid">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="Member" HeaderText="Member" HeaderStyle-HorizontalAlign="Center"
                                                            SortExpression="Member" UniqueName="Member">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="PresentAge" HeaderText="Present Age" SortExpression="PresentAge" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="PresentAge" ColumnGroupName="IfAlive">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="Illness" HeaderText="Illness" SortExpression="Illness" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="Illness" ColumnGroupName="IfAlive">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="YearofDeath" HeaderText="Year of Death" SortExpression="YearofDeath" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="YearofDeath" ColumnGroupName="IfDeceased">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="AgeatDeath" HeaderText="Age at Death" SortExpression="AgeatDeath" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="AgeatDeath" ColumnGroupName="IfDeceased">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="CauseofDeath" HeaderText="Cause of Death" SortExpression="CauseofDeath" HeaderStyle-HorizontalAlign="Center"
                                                            UniqueName="CauseofDeath">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridButtonColumn Text="Delete" CommandName="Delete" HeaderText="Delete"
                                                            ButtonType="ImageButton" />
                                                                                                        </Columns>
                                                    <EditFormSettings EditFormType="Template">
                                                        <FormTemplate>
                                                            <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                                                                style="border-collapse: collapse;">
                                                                <tr>
                                                                    <td>
                                                                        <table id="Table3" width="100%" border="0" class="module" cellpadding="0px" cellspacing="0px">
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                    Member:
                                                                                </td>
                                                                                <td colspan="3" class="paddingtabletdleft">
                                                                                <telerik:RadDropDownList ID="raddrpMember" runat="server" SelectedValue='<%# Bind("Member") %>' 
                                                                                        AppendDataBoundItems="true" >
                                                                                        <Items>
                                                                                            <telerik:DropDownListItem Text="Select" Selected="true" Value="0" />
                                                                                            <telerik:DropDownListItem Text="Mr" Value="Mr" />
                                                                                            <telerik:DropDownListItem Text="Mrs" Value="Mrs" />
                                                                                            <telerik:DropDownListItem Text="Ms" Value="Ms" />
                                                                                        </Items>
                                                                                    </telerik:RadDropDownList>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="4">
                                                                                       
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                   Present Age
                                                                                </td>
                                                                                <td class="paddingtabletdleft" colspan="3">
                                                                                   <telerik:RadDropDownList ID="raddrpPresentAge" runat="server" SelectedValue='<%# Bind("PresentAge") %>' 
                                                                                        AppendDataBoundItems="true" >
                                                                                        <Items>
                                                                                        </Items>
                                                                                    </telerik:RadDropDownList>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="4">
                                                                                       
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                   Illness
                                                                                </td>
                                                                                <td colspan="3" class="paddingtabletdleft">
                                                                                     <telerik:RadDropDownList ID="raddrpIllness" runat="server" SelectedValue='<%# Bind("Illness") %>' 
                                                                                        AppendDataBoundItems="true" >
                                                                                        <Items>
                                                                                            <telerik:DropDownListItem Text="Select" Selected="true" Value="0" />
                                                                                            <telerik:DropDownListItem Text="Yes" Value="Yes" />
                                                                                            <telerik:DropDownListItem Text="No" Value="No" />
                                                                                        </Items>
                                                                                    </telerik:RadDropDownList>
                                                                                </td>
                                                                            </tr>
                                                                              
                                                                            <tr>
                                                                                <td colspan="4">
                                                                                       
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                    Year of Death
                                                                                </td>
                                                                                <td colspan="3" class="paddingtabletdleft">
                                                                                    <telerik:RadTextBox ID="RadTextBox7" runat="server" Text='<%# Bind("YearofDeath") %>'>
                                                                                    </telerik:RadTextBox>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="4">
                                                                                       
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                   Cause of Death
                                                                                </td>
                                                                                <td colspan="3" class="paddingtabletdleft">
                                                                                    <telerik:RadTextBox ID="RadTextBox4" runat="server" Text='<%# Bind("CauseofDeath") %>'>
                                                                                    </telerik:RadTextBox>
                                                                                </td>
                                                                            </tr> <tr>
                                                                                <td colspan="4">
                                                                                       
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td class="tdtext paddingtabletdleft">
                                                                                    Age at Death
                                                                                </td>
                                                                                <td colspan="3" class="paddingtabletdleft">
                                                                                    <telerik:RadTextBox ID="RadTextBox3" runat="server" Text='<%# Bind("AgeatDeath") %>'>
                                                                                    </telerik:RadTextBox>
                                                                                </td>
                                                                            </tr>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td>
                                                                           
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right">
                                                                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                                            runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                                                        </asp:Button
                                                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                                            CommandName="Cancel"></asp:Button>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </FormTemplate>
                                                    </EditFormSettings>
                                                </MasterTableView>
                                            </telerik:RadGrid>

    raddrpPresentAge and raddrpMember this two raddropdownlist need to be populated at page load but with the "itemdatabound" it cause issue while editing.

  9. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 17 Jun 2015 in reply to Rohit Link to this post

    Hello Rohit,

    Could you please provide issue detail?

     Thanks,

    Jayesh Goyani

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017