使用分页时确定列表视图或网格视图中的项目索引

时间:2010-10-20 07:25:21

标签: c# .net asp.net ajax

我对index ::

有以下问题

我的源代码::

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

背后的代码::

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();
    }

当我将寻呼机添加到我的列表视图时出现问题,我的索引异常超出范围... 如何确定页面iam并指定正确的索引我使用列表视图或网格视图或其他此类控件..

1 个答案:

答案 0 :(得分:3)

最后我解决了我的问题。

问题出在以下行::

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


每次从页面移动到另一页面时,命令参数都不会自动重置。

  • 答案是:(在的情况下) 的 的ListView ):

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


  • 答案是::(在的情况下) 的 GridView的 ):

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