简体   繁体   中英

Repeater Onitemcommand won't fire when binded with DataPager. Why?

I'm working with displaying video thumbnails in the repeater control with datapager. I recently discovered that the onitemcommand of the repeater control won't fire once it is being bind with the datapager but if you remove the datapager from binding the repeater it works.But I want to use the datapager for the repeater cause I want to set the pagesize in order to display specific number of thumbnails on the repeater. How do I make the onitemcommand of the repeater to fire using with datapager?How do I do this in asp.net? Pls help...Thankz.

Directive:

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

Here's my HTML source tag of the repeater control with DataPager:

<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>

It is working indeed! Add a Linkbutton to fire the 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" />

And test in the code:

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

Edit: Image buttons are like command button and they do fire their own OnCommand instead of repeater's OnItemCommand, so they will not fire repeater's OnItemCommand. Instead use link button with Image on it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM