Chris B
Chris B

Reputation: 709

Creating controls with unique IDs dynamically inside an ASP.NET repeater

I have a list of files (stored in a database) that I would like the user to be able to upload in asynchronous callbacks.

I have got as far as displaying AJAX Control Toolkit's AsyncFileUpload controls next to the name of each file I'm expecting:

<asp:Repeater ID="SourceTables" runat="server">
    <ItemTemplate>
        <tr>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "LongName")%>
            </td>
            <td>
                <ajax:AsyncFileUpload runat="server" ClientIDMode="AutoID" />
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

All of these async upload controls will end up calling the same method in code behind, which is fine, but when I come to save the file I need some way to identify which control is causing the postback (i.e. which of the several files has just been uploaded). However, I have been unable to set the ID dynamically inside the repeater (I believe it is only possible from code behind).

Clearly this doesn't work:

<ajax:AsyncFileUpload ID=<%#DataBinder.Eval(Container.DataItem, "ShortName")%> runat="server" ClientIDMode="AutoID" />

Is there another way I can set the ID of each of the upload controls dynamically inside the repeater (or otherwise) or another approach I can take?

Upvotes: 0

Views: 5008

Answers (4)

Sreekanth Mohan
Sreekanth Mohan

Reputation: 348

You can add controls dynamically in "ItemDatabound" event of the repeater.

Upvotes: 1

Frazell Thomas
Frazell Thomas

Reputation: 6111

You can't bind the ID property of controls. You will need to create the control in code behind and add it to the repeater.

Example:

protected void GridDataBind(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        IEnumerable list = PremiumUnitFees.PremiumAmenities.ObtainFeesForProject(IntranetUser.Current.ProjectId);
        foreach (PremiumUnitFees.PremiumAmenities feature in list) {
            e.Row.Cells(3).Controls.Add(new CheckBox {
                ID = feature.Id.ToString(),
                Text = feature.NickName,
                Checked = PremiumUnitFees.PremiumUnitView.IsUnitPremium(feature.Id, Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "Id")))
            });
        }
    }
}

Upvotes: 0

Mikey Mouse
Mikey Mouse

Reputation: 3098

Something like this

  <ajax:AsyncFileUpload runat="server" id="afuMyUpload" ClientIDMode="AutoID" OnClientUploadComplete="MyFunction" />



   foreach (Control c in SourceTables.Items)
   {
        var myUpload = c.FindControl("afuMyUpload") as AsyncFileUpload;
        //Do stuff with your control
   }

Upvotes: 0

Thomas Krantz
Thomas Krantz

Reputation: 226

How are you handling the postback events?

If you are using the UploadedComplete event of the AsyncFileUpload control, you get the uploaded file through the AsyncFileUploadEventArgs object.

You could also cast sender to your AsyncFileUpload control and then get the file through the PostedFile property.

 protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
 {
     string savePath = MapPath("~/Uploads/" + Path.GetFileName(e.filename));
 }

Upvotes: 0

Related Questions