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

Fileupload in nested formview in grid

9 Answers 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
BOS
Top achievements
Rank 1
BOS asked on 06 Mar 2012, 02:16 PM
I'm having problems addressing my fileupload.
My structure looks like this:

<telerik:RadGrid ID="RadGrid1" runat="server">
    <MasterTableView>
        <NestedViewTemplate>
            <asp:Panel ID="Panel1" runat="server">
                <telerik:RadTabStrip ID="RadTabStrip1" runat="server">
                    <Tabs>
                        <telerik:RadTab runat="server" Text="RadTab1" PageViewID="RadPageView1"></telerik:RadTab>
                        <telerik:RadTab runat="server" Text="RadTab2" PageViewID="RadPageView2"></telerik:RadTab>
                    </Tabs>
                </telerik:RadTabStrip>
                  
                <telerik:RadMultiPage ID="RadMultiPage1" runat="server">
                    <telerik:RadPageView ID="RadPageView1" runat="server">
                    </telerik:RadPageView>
                      
                    <telerik:RadPageView ID="RadPageView2" runat="server">
                        <asp:Label ID="Label13" runat="server" 
                                       Text='<%# Eval("LokationID") %>'
                                       Visible="false"  />
                              
                        <asp:FormView ID="FormView1" runat="server" 
                                      DataSourceID="sdsFile" DataKeyNames="FileID" 
                                      DefaultMode="Insert" 
                                      oniteminserting="FormView1_ItemInserting">
                            <InsertItemTemplate>
                                <table>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label1" runat="server"
                                                File: (max 1,5GB) 
                                            </asp:Label>
                                        </td>
                                        <td>
                                            <asp:FileUpload ID="FileUpload1" runat="server" 
                                                            Enabled='<%# Bind("FileDocument") %>' />
                                        </td>
                                    </tr>
                                      
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label2" runat="server"
                                                Remarks: 
                                            </asp:Label>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBox1" runat="server" 
                                                                Text='<%# Bind("FileRemarks") %>' 
                                                                TextMode="MultiLine">
                                            </telerik:RadTextBox>
                                        </td>
                                    </tr>
                                      
                                    <tr>
                                        <td colspan="2">
                                            <asp:ImageButton ID="ibtnInsert" runat="server" CommandName="Insert" />                          
                                            <asp:ImageButton ID="ibtnAbort" runat="server" onclick="ibtnAbort_Click" />
                                        </td>
                                    </tr>
                                </table>
                            </InsertItemTemplate>
                        </asp:FormView>
                          
                        <asp:SqlDataSource ID="sdsFile" runat="server" 
                                               ConnectionString="<%$ ConnectionStrings:AppendoConnStr03 %>" 
                                               SelectCommand="SELECT FileID, LokationID, FileDocument, FileName, FileExtension, FileMIMEType, FileRemarks FROM Files"
                                               InsertCommand="INSERT INTO Files(LokationID, FileDocument, FileName, FileExtension, FileMIMEType, FileRemarks) VALUES (@LokationID, @FileDocument, @FileName, @FileExtension, @FileMIMEType, @FileRemarks)">
                                <InsertParameters>
                                    <asp:ControlParameter ControlID="Label13" PropertyName="Text" Type="Int32" Name="LokationID" />
                                    <asp:Parameter Name="FileDocument" />
                                    <asp:Parameter Name="FileName" />
                                    <asp:Parameter Name="FileExtension" />
                                    <asp:Parameter Name="FileMIMEType" />
                                    <asp:Parameter Name="FileRemarks" />
                                </InsertParameters>
                            </asp:SqlDataSource>
                    </telerik:RadPageView>                       
                </telerik:RadMultiPage>
            </asp:Panel>
        </NestedViewTemplate>
    </MasterTableView>    
</telerik:RadGrid>

When I use FileUploads in Formviews outside grids and nestedviews, I use this code:
(which works fine btw :-)

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    FileUpload fileUpload = (FileUpload)FormView1.FindControl("FileUpload1");
  
    if (fileUpload.HasFile)
    {
        e.Values["FileName"] = fileUpload.FileName;
        e.Values["FileExtension"] = Path.GetExtension(fileUpload.PostedFile.FileName).ToLower();
        e.Values["FileMIMEType"] = fileUpload.PostedFile.ContentType;
        e.Values["FileDocument"] = fileUpload.FileBytes;
    }
    else
    {
        e.Values["FileName"] = null;
        e.Values["FileExtension"] = null;
        e.Values["FileMIMEType"] = null;
        e.Values["FileDocument"] = System.Data.SqlTypes.SqlBinary.Null;
    }
}

But inside the nestedview, it seems like I'm not able to address the correct formview - no matter what I try, I always ends up with no file found in the fileupload and null-values in the DB.

NB. The values from Label3 and RadTextBox1 are stored just fine in the DB.

Any helps would be appriciated :-)

Regards
/Berit

9 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 06 Mar 2012, 02:42 PM
Hello,

Are you using Ajax?

Thanks,
Casey

0
BOS
Top achievements
Rank 1
answered on 06 Mar 2012, 02:43 PM
Hi Casey

Yes, I have tried both with and without Ajax :-)
0
Casey
Top achievements
Rank 1
answered on 06 Mar 2012, 02:59 PM
Berit,

It should work with Ajax disabled. Have you tried using the RadGrid's OnItemCommand event instead of the FormView's OnItemInserting event? I've not used a FileUpload inside of a FormView, inside of a RadGrid, but I have used a FileUpload inside a RadGrid's edit template. 

Casey
0
BOS
Top achievements
Rank 1
answered on 06 Mar 2012, 04:51 PM
Hi Casey

Yes, I have tried that as well, but maybe I'm doing it wrong - could you please post an example? :-)
I couldn't even find an fileupload-element that way :-D
0
Casey
Top achievements
Rank 1
answered on 06 Mar 2012, 05:00 PM
Sure thing, here is an example. Please let me know if you have any questions!

I hope this helps!
Casey

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
   if (e.CommandName == "Insert")
   {
       GridEditFormInsertItem insItm = e.Item as GridEditFormInsertItem;
 
       FileUpload fileUpload = insItm .FindControl("FileUpload1") as FileUpload;
                
       if (fileUpload.HasFile)
       {
                     
       }
       else
       {
            e.Canceled = true;
       }
   }
}
0
BOS
Top achievements
Rank 1
answered on 06 Mar 2012, 07:57 PM
Hi Casey :-)

Sorry, I forgot to mention, that the source of the grid isn't the same as the source of the formview.

The grid contains locations and the files related to each location are placed in a separate table to make room for more than one file per location.
0
Casey
Top achievements
Rank 1
answered on 06 Mar 2012, 09:27 PM
Hmm, well I'm able to get a reference to the FormView, and then the FileUpload. I wasn't able to setup my page exactly like yours (I couldn't get it to display properly with the RadMultiPage), but here is how I was able to do so. I also used an ObjectDataSource.

I'm curious as to why you are using a FormView inside of the NestedViewTemplate, instead of specifying a FormTemplate in the EditFormSettings for the RadGrid. Instead of expanding the rows of the RadGrid, the users would have to click an edit link to put the row in EditMode, but I think it would eliminate a lot of the coding hassle you are experiencing.

I hope this helps!
Casey

   <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="PreRender">
    <MasterTableView>
        <NestedViewTemplate>
                              <asp:Panel ID="Panel1" runat="server">
                   
                        <asp:FormView ID="FormView1" runat="server"
                                      DataSourceID="sdsFile" DataKeyNames="ID"
                                      DefaultMode="Insert" >
                            <InsertItemTemplate>
                                <table>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label1" runat="server">
                                                File: (max 1,5GB)
                                            </asp:Label>
                                        </td>
                                        <td>
                                            <asp:FileUpload ID="FileUpload1" runat="server"
                                                             />
                                        </td>
                                    </tr>
                                       
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label2" runat="server">
                                                Remarks:
                                            </asp:Label>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBox1" runat="server"
                                                                Text='<%# Bind("FILE_NAME") %>'
                                                                TextMode="MultiLine">
                                            </telerik:RadTextBox>
                                        </td>
                                    </tr>
                                       
                                    <tr>
                                        <td colspan="2">
                                            <asp:ImageButton ID="ibtnInsert" runat="server" CommandName="AddFile" />                         
                                            <asp:ImageButton ID="ibtnAbort" runat="server" OnClick="ibtnAbort_Click" />  " />
                                        </td>
                                    </tr>
                                </table>
                            </InsertItemTemplate>
                        </asp:FormView>
                        <asp:ObjectDataSource ID="sdsFile" runat="server" TypeName="ClassLibrary.FileDB"
                                               SelectMethod="GetFiles">
                                               <SelectParameters>
                                                <asp:Parameter Name="ID" DefaultValue="1" />
                                               </SelectParameters>
                            </asp:ObjectDataSource>
                            </asp:Panel>
        </NestedViewTemplate>
    </MasterTableView>   
</telerik:RadGrid>


protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "AddFile")
    {
        GridNestedViewItem viewItm = e.Item as GridNestedViewItem;
        FormView formView = viewItm.FindControl("FormView1") as FormView;
        FileUpload fileUpload =  formView.FindControl("FileUpload1") as FileUpload;
          
 
        string s = "";
        if (fileUpload.HasFile)
        {
 
        }
    }
}
0
BOS
Top achievements
Rank 1
answered on 07 Mar 2012, 02:24 PM
Hi Casey :-)

I tried your suggestion, but now I'm having problems passing values to the SqlDataSource.
 
I've added this to your suggestion:

if(FileUpload.HasFile)
{
    sdsFile.InsertParameters["FileName"].DefaultValue = FileUpload.FileName;
    sdsFile.InsertParameters["FileExtension"].DefaultValue = Path.GetExtension(FileUpload.PostedFile.FileName).ToLower();
    sdsFile.InsertParameters["FileMIMEType"].DefaultValue = FileUpload.PostedFile.ContentType;
    sdsFile.InsertParameters["FileDocument"].DefaultValue = FileUpload.FileBytes;
}
else
{
    ...

But FileUpload.FileBytes is a byte array and you can only pass strings as defaultvalue.
The file is stored in the DB as Varbinary(MAX).

I've added two screenshots of my solution so far, to show what I'm trying to accomplish :-)
It's partial danish and partial swedish, but it's the structure, you should look at :-D

0
Casey
Top achievements
Rank 1
answered on 07 Mar 2012, 02:40 PM
Berit,

It looks like it might be possible to set the value of a SqlDataSource parameter to a Byte Array in the SqlDataSource's OnInserting event. The person in the following link had success doing so in the OnUpdating event.

SqlDataSource Byte Array Parameter - OnUpdating

I hope this helps!
Casey 
Tags
Grid
Asked by
BOS
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
BOS
Top achievements
Rank 1
Share this question
or