使用Repeater控件使按钮动态化

时间:2017-05-01 13:08:29

标签: c# asp.net

我有这个Repeater控件,所以我想添加一个按钮,将根据我点击的项目到目前为止下载我有这个 如何使下载按钮动态

foreach (Details i in details) {
                command = @"UPDATE DETAILS SET Place = '" + i.place + @"', LessonType = '" + i.lessonType + @"', Teacher = '" + i.teacher + @"', Day = " + i.day + @", Week = " + i.week + @", Lesson = " + i.lesson +
                          @", LessonID = " + lessonID + @" WHERE LessonID = " + lessonID;
                using (var statement = database.Prepare(command)) {
                    while (statement.Step() == SQLiteResult.ROW) {
                    }
                }
            }

这是我的C#代码  所以当我点击按钮时,这段代码就会运行

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <h4>
            <asp:Label ID="title" runat="server" Text='<%# Eval("title")%>'>
                <asp:Image ID="musicimage" ImageUrl='<%# Eval("image_src")%>' runat="server" Height="150" Width="150" />
                <asp:Button ID="btnDownload" runat="server" Text="Download" />
        </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

1 个答案:

答案 0 :(得分:1)

您可以尝试下面的代码来获取音乐下载 aspx代码:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>
        <h4>
            <asp:Label ID="title" runat="server" Text='<%# Eval("title")%>'></asp:Label>
            <asp:Image ID="musicimage" ImageUrl='<%# Eval("image_src")%>' runat="server" Height="150" Width="150" />
            <asp:Button ID="btnDownload" runat="server" Text="Download" CommandName="DownloadMusic" />
        </div>
      </div>
    </ItemTemplate>
</asp:Repeater>

并且代码隐藏在.cs页面中

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "DownloadMusic")
    {
        //To get title of music
        string Title = string.Empty;
        Label lbltitle = new Label();
        lbltitle = (Label)e.Item.FindControl("title");
        Title = lbltitle.Text;

        //to get path fo music
        string FilePath = string.Empty;
        Image imgfile = new Image();
        imgfile = (Image)e.Item.FindControl("musicimage");
        FilePath = imgfile.ImageUrl;


        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.ContentType = "audio/mpeg";
        response.AddHeader("Content-Disposition", "attachment; filename=" + Title + ";");
        response.TransmitFile(FilePath);
        response.Flush();
        response.End();
    }
}

试试这个。