简体   繁体   English

在ASP.NET中继器内部动态创建具有唯一ID的控件

[英]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: 我可以在我期望的每个文件名旁边显示AJAX控件工具包的AsyncFileUpload控件:

<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 (ie 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). 但是,我一直无法在转发器内部动态设置ID(我相信这只能从后面的代码中进行)。

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? 有没有其他方法可以在中继器(或其他方法)内部动态设置每个上传控件的ID,还是可以采取其他方法?

您可以在转发器的“ ItemDatabound”事件中动态添加控件。

You can't bind the ID property of controls. 您不能绑定控件的ID属性。 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")))
            });
        }
    }
}

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. 如果使用的是AsyncFileUpload控件的UploadedComplete事件,则可以通过AsyncFileUploadEventArgs对象获取上载的文件。

You could also cast sender to your AsyncFileUpload control and then get the file through the PostedFile property. 您也可以将发件人转换AsyncFileUpload控件,然后通过PostedFile属性获取文件。

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

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
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM