RadControls for ASP.NET AJAX
You can add your own custom information to the files RadUpload uploads to the server.
RadUpload supports two types of custom fields: text fields and boolean fields.
Adding custom fields
To add custom fields to the uploaded files, use the OnClientAdded
client-side event to add controls to the RadUpload control where the user can enter the values:
Use the get_row() method of the eventArgs to access each row that is added to the
RadUpload control.
Use the row to add input elements (to the row or next to the row):
For each input element you add, assign its id and name
attributes to a value that you obtain using the RadUpload.getID(name) method. The id and name must be
generated by the RadUpload control so that it can locate the values server-side.
Caution |
|---|
To ensure the proper operation of the event handler in all browsers you must declare the function before the RadUpload declaration.
|
Caution |
|---|
The following example adds <LI> elements to the UL list of the RadUpload. Note that
MaxFileInputsCount property counts the number of <LI> elements so you need to
use it with attention. Check the online example for another approach.
|
The following example shows an OnClientAdded event handler that adds two elements, a text field and a boolean field:
CopyJavaScript
var numberOfCustomFields = 0;
function OnClientAddedHandler(sender, eventArgs) {
var inputRow = eventArgs.get_row();
var uList = inputRow.parentNode;
var count = 0;
newRow = document.createElement("li");
count++;
uList.insertBefore(newRow, inputRow);
var label = document.createElement("span");
label.innerHTML = "Title ";
label.style.fontSize = 12;
input = document.createElement("input");
input.type = "text";
input.id = input.name = sender.getID("Title");
newRow.appendChild(label);
newRow.appendChild(input);
newRow = document.createElement("li");
count++;
uList.insertBefore(newRow, inputRow);
var label = document.createElement("span");
label.innerHTML = "Create Thumbnail";
label.style.fontSize = 12;
input = document.createElement("input");
input.type = "checkbox";
input.id = input.name = sender.getID("Thumbnail");
newRow.appendChild(input);
newRow.appendChild(label);
var fileInputSpan = inputRow.getElementsByTagName("span")[0];
label = document.createElement("span");
label.innerHTML = "File ";
label.style.fontSize = 12;
inputRow.insertBefore(label, fileInputSpan);
numberOfCustomFields = count;
}
function OnClientDeletingHandler(sender, eventArgs) {
var input = eventArgs.get_fileInputField();
deleteCustomFields(input);
}
function OnClientDeletingSelectedHandler(sender, eventArgs) {
var inputs = eventArgs.get_fileInputFields();
for (i = 0; i < inputs.length; i++) {
deleteCustomFields(inputs[i]);
} eventArgs.set_cancel(true);
}
function deleteCustomFields(input) {
var li = input.parentNode.parentNode;
var ul = input.parentNode.parentNode.parentNode;
for (var i = 0; i < numberOfCustomFields; i++) {
ul.removeChild(li.previousSibling);
}
ul.removeChild(li);
}
CopyASPX
<telerik:radupload id="RadUpload1" onclientadded="OnClientAddedHandler" onclientdeleting="OnClientDeletingHandler"
onclientdeletingselected="OnClientDeletingSelectedHandler" runat="server" />
Retrieving Field Values
Server-side, you can use the method UplodedFile.GetFieldValue(name) method.
Note |
|---|
Use the same value that was passed to the RadUpload.getID(name) method when retrieving a field value.
|
The following example shows how to retrieve the values the user entered for the custom fields added in the previous example:
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
foreach (UploadedFile f in RadUpload1.UploadedFiles)
{
string fileName = f.GetName();
string title = f.GetFieldValue("Title");
bool createThumbnail = f.GetIsFieldChecked("Thumbnail");
ProcessFile(fileName, title, createThumbnail);
}
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For Each f As UploadedFile In RadUpload1.UploadedFiles
Dim fileName As String = f.GetName()
Dim title As String = f.GetFieldValue("Title")
Dim createThumbnail As Boolean = f.GetIsFieldChecked("Thumbnail")
ProcessFile(fileName, title, createThumbnail)
Next
End Sub
See Also