LinkBut​​ton OnClick()没有点击脚本功能

时间:2013-11-10 06:39:39

标签: c# javascript asp.net

在我的aspx页面中,我在Repeater Control中有一个Linkbutton linkbtTitle

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
  <script runat="server">  
    protected void Buttonlink_Click(object sender, System.EventArgs e)
    {
        LinkButton linkbtTitle = (LinkButton)sender;
        linkbtTitle.ForeColor = System.Drawing.Color.HotPink;//change color when selected
        linkbtTitle.Font.Underline = true;
        linkbtTitle.Font.Italic = true;
     }  
  </script> 
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
  <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" /> 
   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

    <asp:Repeater ID="Repeater1" runat="server">
      <ItemTemplate>

          <asp:LinkButton ID="linkbtTitle" runat="server" Text='<%#Eval("TITLE")%>' CommandArgument='<%#Eval("ID")%>' OnCommand="linkbtTitle_Click" OnClick="Buttonlink_Click" ></asp:LinkButton>                               

    </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
    </asp:UpdatePanel> 

    </asp:Content>

当客户点击OnCommand="linkbtTitle_Click"Linkbutton更改所选链接按钮的颜色时,我会使用方法OnClick="Buttonlink_Click"自动下载附件。

当我构建此页面时,会自动下载附件,但Selected LinkBut​​ton不会更改其颜色。

尝试调试,函数Buttonlink_Click()未被调用,当鼠标悬停在语句上时,它有一条消息:Expression Statement is not assignment or call... OnClick="Buttonlink_Click" ...

我想知道linkbtTitle_Click中是否存在问题请查看我背后的代码:

public void linkbtTitle_Click(object sender, CommandEventArgs e)
    {
       //auto download the attachment when selected
        int mamd = Convert.ToInt32(e.CommandArgument.ToString());

        string sql = "select * from MAU_DON where MA_MAU_DON="+mamd;
        DataTable maudon = l.EXECUTEQUERYSQL(sql);

        string attachment = dirPath["MAU_DON"].DIRECTORY + @"/" + maudon.Rows[0]["PATH"].ToString();
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition",
          "attachment; filename=" + Path.GetFileName(attachment));
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.WriteFile(attachment);
        HttpContext.Current.Response.End();
    }

1 个答案:

答案 0 :(得分:0)

它不会命中服务器端功能。当在项目模板中使用类似linkbutton或甚至普通按钮本身的控件时,事件机制会稍微改变。而不是onclick事件触发,转发器上的事件将触发...其名为ItemCommand。这通过称为事件冒泡的机制发生。

要知道导致它的按钮,您需要使用CommandName和CommandArgument。

<asp:LinkButton ID="lnkbtn" runat="server" CommandName="foo" CommandArgument="<%# Eval ("Id") %>" Text="FooBtn" />

在后面的代码中,转发器的项命令事件,我们写了类似

的内容
if (e.CommandName == "foo") 
{
    // so we know what button triggered this
    // access the value of e.CommandArgument to get associated row data.
}