+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / radgrid and radupload

radgrid and radupload

7 posts, 0 answered
  1. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    22 Apr 2010
    Link to this post
    I have a radupload in a radgrid. The radupload is to upload a file to a target folder. In the radgrid, I have columns in the database called "fileName" and "filelocation". Once the update or insert button is clicked, i would like the actually file name insert/update into the "fileName" column. And the "filelocation" column will also populate the file name with the path( ie: ~/myfolder/[filename]). The "fileName" and "filelocation" are not visible to a user. How do I accomplish this?

    Thanks,
    Pik
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Apr 2010
    Link to this post
    Hello Pik Lee,

    You could take a look at this sample which code and SQL queries are fully exposed for review - Controls / Binary Images and Upload in Grid

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    22 Apr 2010
    Link to this post
    Thanks for the info.  I have my code as follow. After the upload button is clicked, how do I change the textbox (msg_title) to the name of the upload file.

    <asp:TextBox id="msg_title" runat="server" Text='<%# Bind("msg_title") %>' />
    <telerik:RadUpload id="RadUpload1" TargetFolder="~/myfolder/" Runat="server"  InitialFileInputsCount="1" MaxFileInputsCount="1" OverwriteExistingFiles="false" ></telerik:RadUpload>

    Thanks,
    Pik
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    23 Apr 2010
    Link to this post
    Hi Pik Lee,

    If you keep the date abound the uploaded file in the datasource of the RadGrid you could use <%# Eval("msg_title") %>. Otherwise you need to find the TextBox control in ItemCreated event of the RadGridRadGrid1_ItemCreated and set the Text value of the TextBox.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    28 Apr 2010
    Link to this post
    I am trying to used OnInsertCommand in RadGrid to upload a single file into a database. When the Insert button clicked, 2 data get inserted into the db. Not sure what I did wrong. Here is my code.

     


    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)

     

    {

     

    GridEditableItem item = (GridEditableItem)e.Item;

     

     

    RadUpload upload = (RadUpload)e.Item.FindControl("RadUpload1");

     

     

    foreach (UploadedFile f in upload.UploadedFiles)

     

    {

    f.SaveAs(Server.MapPath(

    "~/MyFolder/" + f.GetName()));

     

     

    String x = f.GetName();

     

     

    TextBox xs = item.FindControl("msg_title") as TextBox;

     

     

    String msg_title = Convert.ToString(xs);

     

     

    String msg_file = "/MyFolder/" + x;

     

    SqlDataSource1.InsertParameters.Clear();

    SqlDataSource1.InsertParameters.Add(

    new Parameter("msg_title", TypeCode.String, msg_title));

     

    SqlDataSource1.InsertParameters.Add(

    new Parameter("msg_file", TypeCode.String, msg_file));

     

    SqlDataSource1.Insert();

    }

    }

    Thanks,
    Pik

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    29 Apr 2010
    Link to this post
    Hello Pik Lee,

    Try to insert the data in as shown below. You could attache the code to VS debugger to see whether InsertCommand is called twice.

    SqlDataSource SqlDataSource1 = new SqlDataSource();
    SqlDataSource1.InsertParameters["msg_title"].DefaultValue = msg_title.Text.ToString();
    SqlDataSource1.InsertParameters["msg_file"].DefaultValue = msg_file.Text.ToString();
    SqlDataSource1.Insert();


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    10 May 2010
    Link to this post
    Thanks for the info.

    I tried to implement validation a function in the RadGrid _UpdateCommand. I want to show a message if the file already exists in the target folder. For some reason it get uploaded twice when I tried to upload a brand new file. Not sure what I did wrong. Below is the code. Any feedback is greatly appreciated.  Thanks, Pik


     protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {

                GridEditableItem item = (GridEditableItem)e.Item;
                RadUpload upload = (RadUpload)e.Item.FindControl("RadUpload1");

     
                if (upload.UploadedFiles.Count > 0)
                {
                    foreach (UploadedFile f in upload.UploadedFiles)
                    {
                        int counter = 1;

                        string target = = Server.MapPath(upload.TargetFolder);

                        string targetFileName = Path.Combine(target, f.GetNameWithoutExtension() + f.GetExtension());

                        if (System.IO.File.Exists(targetFileName))
                        {
                            counter++;
                            targetFileName = Path.Combine(target, f.GetNameWithoutExtension() + counter.ToString() + f.GetExtension());
                            Label lbltest2 = item.FindControl("lblError") as Label;
                            lblError.Text = "File already exists. ";
                            lblError.Visible = true;
                            e.Canceled = true;

                        }
       else

                        {

                            RadComboBox catComboBox = (RadComboBox)e.Item.FindControl("RadComboBox1");
                            String msg_file = targetFileName;
                            String category = catComboBox.SelectedItem.Text;

                            SqlDataSource1.UpdateParameters.Clear();
                            SqlDataSource1.UpdateParameters.Add(new Parameter("msg_file",TypeCode.String, msg_file));
                            SqlDataSource1.UpdateParameters.Add(new Parameter("category", TypeCode.String, category));
                            f.SaveAs(Path.Combine(target, f.GetName()), true);
                        }
                    }
                }
                else
                {
                    Label lbltest = item.FindControl("lblError2") as Label;
                    lblError2.Text = "Please insert at least one file";
                    lblError2.Visible = true;
                    e.Canceled = true;
                }
        }

Register for webinar
7 posts, 0 answered