【问题标题】:how to code on on image button if image button are in listview in asp.net C#如果图像按钮在asp.net C#的列表视图中,如何在图像按钮上编码
【发布时间】:2013-06-08 14:31:36
【问题描述】:

这里是asp.net的code标签代码 并且有一个图像按钮,我想要下载项目的点击事件代码 我尝试在点击事件上编写代码,但它不起作用

    <asp:ListView runat="server" ID="ListView1" GroupItemCount="1">
                <LayoutTemplate> 
                <div>
                        <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
                    </div>              
                </LayoutTemplate>
                <GroupTemplate>   
                   <div style="clear: both;">
                        <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
                    </div>                       
                </GroupTemplate>
                <ItemTemplate>
                <div style="margin-left: 20px; border: 2px rgba(255, 102, 0, 0.36) 
dotted; width: 900px; padding: 10px;">
    <video src='<%# "Handler.ashx?id=" + Eval("id") %>' 
width="900" height="400" controls="" preload=""></video>
    </div>    
          <div style="width:950px; height:90px;" align="right">
              <asp:ImageButton ID="ImageButton1" 
ImageUrl="images/download-button.jpg" runat="server" width="230px"/> 
      </div>
                </ItemTemplate>            
                <GroupSeparatorTemplate>
                    <div style="clear:both"></div> 
                </GroupSeparatorTemplate>
                <EmptyDataTemplate>
                </EmptyDataTemplate>
            </asp:ListView>

这是同一页面上的一个 c# 私有代码类

Private void Download(string ID)
{
  some code here for download
}

请告诉我如何在图像按钮上编码以下载文件。

错误:

回发或回调参数无效。

Event validation is enabled using 
<pages enableEventValidation="true"/> in configuration or 
<%@ Page EnableEventValidation="true" %> in a page.  
For security purposes, this feature verifies that arguments to 
postback or callback events originate from the server 
control that originally rendered them.  If the data is valid and expected, use the    ClientScriptManager.RegisterForEventValidation method in order to 
register the postback or callback data for validation.

【问题讨论】:

  • 当您说点击事件不起作用时 - 您的意思是它没有触发?在这种情况下,您是否尝试过 Causesvalidation = "false"?

标签: c# asp.net sql-server listview imagebutton


【解决方案1】:

按钮控件在列表视图中,为什么不给它命令名称,然后运行列表视图的项目命令。 例如

分配Image按钮的CommandName属性,然后在ListView的ItemCommand中写入

<asp:ImageButton ID="ImageButton1" ImageUrl="images/download-button.jpg" 
runat="server" width="230px" CommandName ="Download"/> 

if (e.CommandName = "Download")
{

在此处编写您的代码..

 }

【讨论】:

  • 我收到此错误 Invalid postback or callback argument。使用配置中的 或页面中的 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。
【解决方案2】:

要在列表视图中访问该按钮,您应该使用这样的项目命令事件:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "EditRecord")
        {

        }
        else if (e.CommandName == "DeleteRecord")
        {

        }
    }

您也可以使用按钮的 onclick 事件,但不推荐这样做。您必须在按钮标记中手动添加OnClick="ImageButton1_OnClick()",然后手动将事件放入文件后面的代码中

protected void ImageButton1_OnClick(object sender, EventArgs e)
{

}

关于您的 Invalid postback 或 callback argument 错误,某些 html 是由某些控件生成的。您必须概述您的整个网络表单,并在 scriptmanager 下注册您正在使用的任何脚本。或者如果没有任何效果,您可以禁用此检查。 &lt;%@ Page EnableEventValidation="false" %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2011-02-12
    相关资源
    最近更新 更多