使用DataPager绑定时,Repeater Onitemcommand不会触发。为什么?

时间:2013-10-27 15:46:44

标签: c# asp.net webforms

我正在使用datapager在转发器控件中显示视频缩略图。 我最近发现转发器控件的onitem命令不会触发一次 它正在与数据捕获器绑定,但是如果从绑定中删除数据捕获器 它工作的转发器。但我想使用数据传输器作为我想要的转发器 设置pagesize以在转发器上显示特定数量的缩略图。 如何使用datapager使用转发器的onitem命令?如何在asp.net中执行此操作?请帮助......谢谢。

指令:

<%@ Register Assembly ="DataPagerRepeater" Namespace ="DataPagerRepeater" TagPrefix ="vid"  %>

这是我使用DataPager的转发器控件的HTML源代码:

<vid:DataPagerRepeater ID="Repeater1" runat="server" 
                        DataSourceID="SqlDataSource1" onitemcommand="Repeater1_ItemCommand">

                   <HeaderTemplate >
                   <table border ="0" style ="width :350px;" >
                   </HeaderTemplate>

                   <ItemTemplate >
                   <tr>
                   <td style ="border :0px; height :100px; width :350px">
                   <asp:ImageButton ID ="thumb" runat ="server"  style=" margin-right :5px" Width ="120px" Height ="75px" ImageAlign = "Left"  ImageUrl='<%#Eval("Filename","~/Thumbs/{0}") %>' />
                   <asp:Label ID="title" runat ="server"  Text ='<%#Eval("Title") %>' />
                   <br />
                   <asp:Label ID ="artist" runat ="server"  Text ='<%#Eval("Artist") %>' />
                   <br />
                    <asp:Label ID ="view" runat ="server"   Text ='<%#Eval("Views") %> '  />
                  <asp:Label ID ="Label3" runat ="server" Text =" Views" /> 
                    <br />
                   <%--  <asp:Label ID ="fname" runat ="server"  Text ='<%#Eval("VidFname") %>' /> --%>
                   <asp:TextBox ID ="fname" runat ="server"  Text ='<%#Eval("VidFname") %>' />
                     </td>

                      </tr>

                   </ItemTemplate>

                   <%--  
                   <AlternatingItemTemplate >
                   <tr>
                   <td style ="border :0px; height :100px; width :140px">
                    <asp:ImageButton ID ="thumb2" runat ="server" style=" margin-right :5px" Width ="120px" Height ="75px" ImageAlign ="Left" ImageUrl ='<%#Eval("Filename","~/Thumbs/{0}") %>' />
                   <asp:Label ID="title2" runat ="server" Text ='<%#Eval("Title") %>' />
                   <asp:Label ID ="artist2" runat ="server"  style="margin-left :20px" Text ='<%#Eval("Artist") %>' />
                   <asp:Label ID ="view2" runat ="server"  style="margin-left :20px" Text ='<%#Eval("Views") %> ' />
                  <!-- <asp:Label ID ="Label4" runat ="server" Text =" Views" /> -->

                   </td>

                      </tr>

                   </AlternatingItemTemplate>
                  --%>

                   <SeparatorTemplate >
                   <br />
                   </SeparatorTemplate>
                   <FooterTemplate >
                   </table>
                   </FooterTemplate>

</vid:DataPagerRepeater>


<asp:DataPager ID="DataPager1" runat="server" PagedControlID="Repeater1" PageSize="10">
                   <Fields>

          <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />

          <asp:NumericPagerField />

          <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />

                 </Fields> 
        </asp:DataPager>

1 个答案:

答案 0 :(得分:0)

确实有效!添加一个Linkbutton来激活ItemCommand:

<ItemTemplate >
    <tr>
        <td style ="border :0px; height :100px; width :350px">
        <asp:ImageButton ID ="thumb" runat ="server"  style=" margin-right :5px" Width ="120px" Height ="75px" ImageAlign = "Left"  ImageUrl='<%#Eval("Filename","~/Thumbs/{0}") %>' />
        <asp:LinkButton ID="LinkButton1" runat="server"
                            CommandName="cmd"
                            CommandArgument='<%# Eval("VidFname") %>' Text="TEST" />

在代码中测试:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "cmd")
    {
        //set a break point in the next line
        string myString = e.CommandName;
    }
}

编辑:图像按钮就像命令按钮,它们会触发自己的OnCommand而不是转发器的OnItemCommand,因此它们不会触发转发器的OnItemCommand。而是使用带有图像的链接按钮:

    <asp:LinkButton ID="LinkButton1" runat="server"
                        CommandName="cmd"
                        CommandArgument='<%# Eval("VidFname") %>' >
            <img src="test.jpg" alt="my cmd" />TEST
    </asp:LinkButton>