DataList 转发器的这个实现有什么问题?

What's wrong with this implementation of the DataList repeater?

我在 ascx 中有以下声明,它显示 4 列文件名列表。文件名是可以下载的xlxs个文件,所以点击文件名时会调用命令事件。

<asp:DataList runat="server" ID="dlHistoricalRates" RepeatColumns="4" >
    <HeaderStyle>
    </HeaderStyle>
    <HeaderTemplate>
        <span>Historial Rates</span>
    </HeaderTemplate>        
    <ItemTemplate>           
        <asp:LinkButton id="historicalRate" ClientIDMode="Static" 
          runat="server" CommandArgument='<%# Eval("filename") %>' 
          CommandName="Download" OnCommand="historicalRate_OnCommand" >
              <%# Eval("filename") %>
        </asp:LinkButton>
    </ItemTemplate>        
</asp:DataList>

后台代码命令代码:

protected void historicalRate_OnCommand(object sender, CommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        if (e.CommandArgument != null)
        {
            historicalRate_Download(e.CommandArgument.ToString());                    
        }                
    }
}

但是 CommandArgument 是一个空字符串,而它应该是文件名。我知道 express Eval() 正在工作,因为它在控件中显示文件名。

为什么,filename 没有作为 CommandArgument 传递?

您的 CommandArgument 看起来不错,但是另一种从 sender 对象获取 LinkBut​​ton arg 的方法:

protected void historicalRate_OnCommand(object sender, CommandEventArgs e)
{
    // get the reference of clicked LinkButton
    LinkButton lb = sender as LinkButton;
    string cmd = lb.CommandName;
    string arg = lb.CommandArgument;

    if (cmd == "Download")
    {
        if (arg != null)
        {
            historicalRate_Download(arg);                    
        }                
    }
}