【问题标题】:LinkButton Command event handler args always stringsLinkBut​​ton 命令事件处理程序参数始终为字符串
【发布时间】:2010-10-18 15:31:11
【问题描述】:

我在这样的用户控件中有一个 LinkBut​​ton 控件(Accordion 来自 AJAX 控件工具包):

<cc1:Accordion runat="server">
    <Panes></Panes>
    <HeaderTemplate></HeaderTemplate>
    <ContentTemplate>
        <asp:TextBox Text='<%# Bind("Title") %>' runat="server"></asp:TextBox>
        <asp:LinkButton Text="Update" CommandArgument='<%# Container.DataItem %>' CommandName="ItemUpdate" OnCommand="LinkButton_Command" runat="server"></asp:LinkButton>
    </ContentTemplate>
</cc1:Accordion>

手风琴在Page_Load中绑定:

protected void Page_Load(object sender, EventArgs e)
{
    Accordion1.DataSource = GetACollection();
    Accordion1.DataBind();
}

但在命令事件处理程序中,无论我做什么,参数始终是字符串:

protected void LinkButton_Command(object sender, CommandEventArgs e)
{
      // sender is alway a string (the Text of the clicked button)
      // e is always a string property of the Container.DataItem object
}

字符串来自正确的对象(分别是按钮和绑定的 DataItem),但我需要对象本身(特别是 DataItem)。

发生了什么事?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    这不是预期的行为吗..

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx

    属性值 类型:System..::.String
    传递给
    的可选参数 命令事件处理程序以及
    关联的 CommandName 属性。
    默认值为 String..::.Empty.

    【讨论】:

    • 哇!你说得对。调试器让我觉得我可以传递对象,但你是对的。该属性只接受字符串。
    猜你喜欢
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多