简体   繁体   中英

determining the index of item in the list view or grid view when using paging

i have the following problem concerning the index::

my source code::

<table style="width: 80%">
        <tr>
            <td>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <img src ="images/addNews_photo.jpg" width ="70px" 
    height ="70px" /><asp:Label ID="lbl_title" runat="server" CssClass="subtitle"></asp:Label><br />
                    <asp:HiddenField ID="hf_unitId" runat="server" />
                    <br />
                    <asp:Label ID="lbl_newsDate" runat="server"></asp:Label>
                    <br />
                    <asp:Image ID="img_news" runat="server" BorderWidth="4px" Height="400px" 
                        Width="669px" />
                    <br />
                    <br />
                    <asp:Label ID="txt_desc" runat="server" BorderStyle="None" BorderWidth="0px" 
                        Width="796px"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="lv_news" EventName="ItemCommand" />
                </Triggers>
                </asp:UpdatePanel>
               &nbsp;&nbsp;<br />
                <asp:Label ID="Label1" runat="server" CssClass="subtitle" Text="More News:"></asp:Label>
                <br />`
                            <hr class="Alternating" 

                                style="width: 560px; text-align: left; background-color: #008000; height: 1px;" 
                                __designer:mapid="3c5" />
                                <br />
                            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                <ContentTemplate>
                                    <asp:ListView ID="lv_news" runat="server" ItemPlaceholderID="a" 
                    DataSourceID="ObjectDataSource1" onitemcommand="lv_news_ItemCommand" 
                    DataKeyNames="newsId" onitemdatabound="lv_news_ItemDataBound">
                                        <LayoutTemplate>
                                            <table ID="Table1" runat="server" cellpadding="2" cellspacing="2" 
                                                 class="Alternating" >
                                                <tr ID="a" runat="server">
                                                </tr>
                                            </table>
                                        </LayoutTemplate>
                                        <ItemTemplate>
                                            <tr ID="Tr1" runat="server">
                                                <td>
                                                    <asp:HiddenField id = "hf_newsId" runat ="server" 
                                                        Value = '<%#Eval("newsId")%>' />
                                                    <asp:Label ID="Label2" runat="server" Text="*" Font-Bold="True"></asp:Label>
                                                    &nbsp;
                                                    <asp:LinkButton ID="lbtn_link" runat="server" 
                                                             Text ='<%#Eval("englishNewsTitle")%>' 
                                                             CommandArgument ='<%# Container.DataItemIndex%>' 
                                                        CommandName ="others"> </asp:LinkButton>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                    </asp:ListView>
                                    <asp:DataPager ID="DataPager1" runat="server" 
                                        onprerender="DataPager1_PreRender" PagedControlID="lv_news" PageSize="1">
                                        <Fields>
                                            <asp:NextPreviousPagerField />
                                        </Fields>
                                    </asp:DataPager>
                                    <br />
                                </ContentTemplate>
                </asp:UpdatePanel>

                <br />
                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                    SelectMethod="GetAllNewsEnabled" TypeName="Managers.News">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="hf_unitId" Name="FK_UnitId" 
                            PropertyName="Value" Type="Int32" />
                    </SelectParameters>
                </asp:ObjectDataSource>

                <br />

            </td>
        </tr>
    </table>

the code behind ::

public partial class showNews : System.Web.UI.Page
{
    static int index;
    protected void Page_Load(object sender, EventArgs e)
    {
        Managers.News nn = new Managers.News();
        Managers.Photo p = new Managers.Photo();
        hf_unitId.Value = "1";

        if (!IsPostBack)
        {

                lbl_title.Text = nn.GetTheLatestNews(1).Rows[0][1].ToString();
                string newsId = nn.GetTheLatestNews(1).Rows[0][0].ToString();
                string photoName = p.GetAllPhotos(int.Parse(newsId)).Rows[0][2].ToString();
                string dir = "NewsImages" + "/" + "UnitNum" + "1" + "_" + "NewsNum" + newsId + "/";
                img_news.ImageUrl = dir + photoName;
                lbl_newsDate.Text = nn.GetTheLatestNews(1).Rows[0][9].ToString();
                txt_desc.Text = nn.GetTheLatestNews(1).Rows[0][5].ToString();

        }
    }
    /* =========================================================================== */
    protected void lv_news_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        Managers.News nn = new Managers.News();
        Managers.Photo p = new Managers.Photo();
        index = Convert.ToInt32(e.CommandArgument);


        if (e.CommandName == "others")
        {
            int newsId = int.Parse(((HiddenField)lv_news.Items[index].FindControl("hf_newsId")).Value);
            lbl_title.Text = nn.GetNewsInList(newsId)[0].EnglishNewsTitle;
            string photoName = p.GetAllPhotos(newsId).Rows[0][2].ToString();
            string dir = "NewsImages" + "/" + "UnitNum" + "1" + "_" + "NewsNum" + newsId + "/";
            img_news.ImageUrl = dir + photoName;
            lbl_newsDate.Text = nn.GetNewsInList(newsId)[0].ShowingDate;
            txt_desc.Text = nn.GetNewsInList(newsId)[0].EnglishText;

        }
    }
    protected void DataPager1_PreRender(object sender, EventArgs e)
    {

        lv_news.DataBind();
    }

the problem is appeared when i have added the pager to my list view ,,i have out of range for index exception ... how to determine the page iam in and specify the right index either i use list view or grid view or other such controls..

Finally i fix my problem.

The problem is in the following line::

CommandArgument ='<%# Container.DataItemIndex%>'


the command argument not reset itself every time when move from page to another page..

  • The answer is ::(in the case of ListView ):

CommandArgument ='<%# ((ListViewDataItem)Container).DisplayIndex%>'


  • The answer is :: (in the case of GridView ):

CommandArgument='<%#((GridViewRow)Container).RowIndex%>'


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