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

http upload with ajax

3 Answers 122 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Amanda
Top achievements
Rank 2
Amanda asked on 15 Sep 2008, 07:59 PM
Hello, I have the newest version of the Rad Controls and I have a web page with a fileupload on it along with some other form controls.  The user must submit the top part of the page and then the fileupload and a few other controls will then be enabled.  I'm using Ajax for this so the page doesn't look like it refreshes.  When I click the add file button i have to add the file from the FileUpload asp control, it doesn't see anything in the control.  I was wondering how to bypass this.  I've tried this javascript and I can't get it to work. I was wondering if htere's any other ways of making this work?
<script type="text/javascript">  
        function OnRequestStart(target, arguments) {  
            alert("here");  
            if (arguments.EventTarget == "<%# butFile.UniqueID %>") {  
                arguments.EnableAjax = false;  
                alert("Ajax disabled");  
            }  
        }  
</script> 

Here's some of my code: (upload file)
 Protected Sub butFile_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles butFile.Click  
              Dim postedFile As HttpPostedFile = Me.FileUpload1.PostedFile  
 
        ' Validate   
        If Not postedFile Is Nothing AndAlso postedFile.ContentLength > 0 Then 
 
            ' Retreive the byte data   
            Dim b(postedFile.ContentLength - 1) As Byte 
            postedFile.InputStream.Read(b, 0, b.Length)  
            ' Insert into the database   
            Dim sqlConn As SqlConnection = GetSqlConnection()  
            Dim sqlComm As New SqlCommand  
            ' Configure the command object   
            With sqlComm  
                .Connection = sqlConn  
                .CommandType = Data.CommandType.Text  
                Dim ecn As Integer = 201  
                ''''.CommandText = "INSERT INTO [tblFiles]( [fileName], [fileSize], [fileContentType], [fileImage]) VALUES(@fileName, @fileSize, @fileContentType, @fileImage)"  
                .CommandText = "INSERT INTO tblDGdrawings (dg_id, downloadName, length, type, attachment, description, date_added) VALUES ('" & lblid.Text & "', @fileName, @fileSize, @fileContentType, @fileImage, '" & tbDescription.Text & "', '" & Lbldate.Text & "')" 
 
                ' Configure the simple parameters   
                'sqlComm.Parameters.Add(New SqlParameter("@fileName", postedFile.FileName))  
                sqlComm.Parameters.Add(New SqlParameter("@fileName", Replace(FileUpload1.PostedFile.FileName, StrReverse(Mid(StrReverse(FileUpload1.PostedFile.FileName), InStr(1, StrReverse(FileUpload1.PostedFile.FileName), "\"))), "")))  
 
                sqlComm.Parameters.Add(New SqlParameter("@fileSize", postedFile.ContentLength))  
                sqlComm.Parameters.Add(New SqlParameter("@fileContentType", postedFile.ContentType))  
 
                ' Byte data   
                Dim paramBytes As New SqlParameter("@fileImage", Data.SqlDbType.Image, postedFile.ContentLength, "attachment")  
                ' Assign byte value   
                paramBytes.Value = b  
                sqlComm.Parameters.Add(paramBytes)  
 
                ' Open the connection and execute   
                sqlConn.Open()  
                sqlComm.ExecuteNonQuery()  
 
                ' Cleanup   
                sqlConn.Close()  
                sqlComm.Dispose()  
                sqlComm = Nothing 
                sqlConn.Dispose()  
                sqlConn = Nothing 
            End With 
 
        End If 
 
        tbDescription.Text = "" 
        grdFiles.Rebind()  
    End Sub 

asp code:
 <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server">  
    </telerik:RadStyleSheetManager> 
    <p> 
        <br /> 
        </p> 
     
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server"  ClientEvents-OnResponseStart="OnRequestStart">  
        <table style="width:50%;">  
            <tr> 
                <td colspan="4">  
                    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">  
                    </telerik:RadScriptManager> 
                    </td> 
            </tr> 
            <tr> 
                <td> 
                    <asp:Label ID="Label1" runat="server" Text="School/Company Name:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"   
                        ControlToValidate="tbName">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbName" runat="server"></asp:TextBox> 
                    </td> 
                <td> 
                    <asp:Label ID="Label6" runat="server" Text="Width:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"   
                        ControlToValidate="tbWidth" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbWidth" runat="server"></asp:TextBox> 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <asp:Label ID="Label2" runat="server" Text="Dealer:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"   
                        ControlToValidate="tbDealer">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbDealer" runat="server"></asp:TextBox> 
                    </td> 
                <td> 
                    <asp:Label ID="Label7" runat="server" Text="Length:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"   
                        ControlToValidate="tbLength" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbLength" runat="server"></asp:TextBox> 
                </td> 
            </tr> 
             <tr> 
                <td> 
                    <asp:Label ID="Label3" runat="server" Text="Lab Type:"></asp:Label> 
                    </td> 
                <td> 
                    <asp:DropDownList ID="ddlLabType" runat="server">  
                        <asp:ListItem Value="General">General Lab</asp:ListItem> 
                        <asp:ListItem Value="Chem">Chemistry</asp:ListItem> 
                        <asp:ListItem>Biology</asp:ListItem> 
                        <asp:ListItem>Physics</asp:ListItem> 
                        <asp:ListItem Value="Prep">Prep Lab</asp:ListItem> 
                        <asp:ListItem>Storage</asp:ListItem> 
                        <asp:ListItem Value="Middle">Middle School</asp:ListItem> 
                    </asp:DropDownList> 
                    </td> 
                <td> 
                    <asp:Label ID="Label8" runat="server" Text="Square Feet:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"   
                        ControlToValidate="tbSqFeet" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbSqFeet" runat="server"></asp:TextBox> 
                 </td> 
            </tr> 
             <tr> 
                <td> 
                    <asp:Label ID="Label4" runat="server" Text="Sudent Capacity:"></asp:Label> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"   
                        ControlToValidate="tbStudCap" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbStudCap" runat="server"></asp:TextBox> 
                    </td> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
             <tr> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    </td> 
                <td> 
                    </td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
             <tr> 
                <td colspan="4">  
                    <asp:CheckBoxList ID="cblOptions" runat="server" CellPadding="2"   
                        CellSpacing="5" RepeatColumns="4" Width="614px">  
                        <asp:ListItem>Perimeter</asp:ListItem> 
                        <asp:ListItem>Peninsula</asp:ListItem> 
                        <asp:ListItem Value="Octagon Island">Octagon Island</asp:ListItem> 
                        <asp:ListItem>Forward Facing Island</asp:ListItem> 
                        <asp:ListItem>Rectangular Island</asp:ListItem> 
                        <asp:ListItem>Tables</asp:ListItem> 
                        <asp:ListItem>B, Open</asp:ListItem> 
                        <asp:ListItem>B, Closed</asp:ListItem> 
                        <asp:ListItem>B, Drawer</asp:ListItem> 
                        <asp:ListItem>B, Combo</asp:ListItem> 
                        <asp:ListItem>W, Open</asp:ListItem> 
                        <asp:ListItem>W, Closed</asp:ListItem> 
                        <asp:ListItem>W, Glass</asp:ListItem> 
                        <asp:ListItem>T, Open</asp:ListItem> 
                        <asp:ListItem>T, Closed</asp:ListItem> 
                        <asp:ListItem>T, Glass</asp:ListItem> 
                    </asp:CheckBoxList> 
                    </td> 
            </tr> 
             <tr> 
                <td colspan="4">  
                    <asp:Button ID="butSubmit" runat="server" Text="Submit" /> 
                    </td> 
            </tr> 
             </table> 
                    
            <table> 
             <tr> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    <asp:Button ID="butStartNew" runat="server" Text="Start New Entry"   
                        Enabled="False" /> 
                 </td> 
                <td> 
                    </td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
             
             <tr> 
                 <td> 
                     <asp:Label ID="Label12" runat="server" Font-Bold="True" Text="Add Files"   
                         Enabled="False"></asp:Label> 
                 </td> 
                 <td> 
                     &nbsp;</td> 
                 <td colspan="2" rowspan="4">  
                     <telerik:RadGrid ID="grdFiles" runat="server" AutoGenerateColumns="False"   
                         DataSourceID="SqlDataSourceFiles" GridLines="None" Skin="WebBlue">  
<MasterTableView DataSourceID="SqlDataSourceFiles">  
<RowIndicatorColumn Visible="False">  
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn Visible="False" Resizable="False">  
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
 
    <Columns> 
        <telerik:GridBoundColumn DataField="dg_id" SortExpression="dg_id"   
            UniqueName="column" Visible="False">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="id" SortExpression="id"   
            UniqueName="column1" Visible="False">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="downloadName" HeaderText="File"   
            SortExpression="downloadName" UniqueName="column2">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="description" HeaderText="Description"   
            SortExpression="description" UniqueName="column3">  
        </telerik:GridBoundColumn> 
    </Columns> 
 
<EditFormSettings> 
<PopUpSettings ScrollBars="None"></PopUpSettings> 
</EditFormSettings> 
</MasterTableView> 
                     </telerik:RadGrid> 
                 </td> 
            </tr> 
             <tr> 
                <td> 
                    <asp:Label ID="Label10" runat="server" Text="File:" Enabled="False"></asp:Label> 
                    </td> 
                <td> 
                    <asp:FileUpload ID="FileUpload1" runat="server" /> 
                    </td> 
            </tr> 
             <tr> 
                <td> 
                    <asp:Label ID="Label11" runat="server" Text="File Description:" Enabled="False"></asp:Label> 
                    </td> 
                <td> 
                    <asp:TextBox ID="tbDescription" runat="server" Width="325px" Enabled="False"></asp:TextBox> 
                    </td> 
            </tr> 
             <tr> 
                <td> 
                    <asp:Button ID="butFile" runat="server" Text="Add File" Enabled="False" OnClick="butFile_Click" /> 
                    </td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
             <tr> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    <telerik:RadUpload ID="RadUpload1" Runat="server" Skin="Web20"   
                        ControlObjectsVisibility="RemoveButtons, AddButton" /> 
                 </td> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
             <tr> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    &nbsp;</td> 
                <td> 
                    </td> 
                <td> 
                    &nbsp;</td> 
            </tr> 
        </table> 
        </telerik:RadAjaxPanel> 
        <asp:SqlDataSource ID="SqlDataSourceFiles" runat="server"   
            ConnectionString="<%$ ConnectionStrings:DWJobInfoConnectionString %>"   
            SelectCommand="SELECT [dg_id], [id], [downloadName], [description] FROM [tblDGdrawings] WHERE ([dg_id] = @dg_id)">  
            <SelectParameters> 
                <asp:ControlParameter ControlID="lblid" Name="dg_id" PropertyName="Text"   
                    Type="Int32" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
    
 
    <rad:AjaxLoadingPanel ID="AjaxLoadingPanel1" Runat="server" height="16px"   
        width="59px">  
        <asp:Image runat="server" AlternateText="Loading..."   
            ImageUrl="~/RadControls/Ajax/Skins/Default/loading4.gif" /> 
    </rad:AjaxLoadingPanel> 
        <asp:Label ID="lblid" runat="server" Visible="False"></asp:Label> 
        <asp:Label ID="lbldate" runat="server" Visible="False"></asp:Label> 
        <br /> 
Let me know if you need anything else.  Thanks!

3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 17 Sep 2008, 01:47 PM
Hi Amanda,

I followed your scenario in order to replicate the issue but no avail. Please find the attached sample and let me know how it works on your and what differs in your case.

All the best,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amanda
Top achievements
Rank 2
answered on 22 Sep 2008, 05:13 PM
Okay, so I think I figured it out.  When this is called:
args.EventTarget == "Button1"
It is button1 but the EventTarget is actually the clientID, so it's actually like this:
ctl00$ContentPlaceHolder1$Button1
So, now is turns off the Ajax when that button is clicked.  My problem is that it's running my vb code twice.  So the file is actually being added twice and I only want it once.  How can I change this, do you know?
0
Amanda
Top achievements
Rank 2
answered on 22 Sep 2008, 05:55 PM
okay, scratch that!  I was calling the click event twice, so I took it out of the asp on_click event and now it's only running once.  Thanks for your help!
Tags
Ajax
Asked by
Amanda
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Amanda
Top achievements
Rank 2
Share this question
or