TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tuan Nguyen
NA
20
1.3k
Error while paging in Gridview: "Index was out of range."
Oct 30 2019 11:44 PM
Hi everyone, I have got an error "Index was out of range. Must be non-negative and less than the size of the collection." while paging in Gridview.
I tried to get value from a hidden Label while paging and this error happened. Please help me, thank you.
<asp:GridView ID=
"GridView2"
runat=
"server"
ForeColor=
"#333333"
AllowPaging=
"True"
AutoGenerateColumns=
"False"
DataKeyNames=
"id"
PageSize=
"1"
CssClass=
"table table-striped table-bordered table-hover"
OnPageIndexChanging=
"GridView2_PageIndexChanging"
OnRowCancelingEdit=
"GridView2_RowCancelingEdit"
OnRowDeleting=
"GridView2_RowDeleting"
OnRowEditing=
"GridView2_RowEditing"
OnRowUpdating=
"GridView2_RowUpdating"
OnRowDataBound=
"GridView2_RowDataBound"
>
<Columns>
<asp:BoundField DataField=
"id"
ItemStyle-CssClass=
""
HeaderStyle-CssClass=
""
FooterStyle-CssClass=
""
/>
<asp:TemplateField ItemStyle-CssClass=
""
HeaderStyle-CssClass=
""
FooterStyle-CssClass=
""
>
<ItemTemplate>
<asp:Label ID=
"lblFruitCategoryID"
runat=
"server"
Text=
'<%# Eval("fruitCategoryID") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass=
""
HeaderStyle-CssClass=
""
FooterStyle-CssClass=
""
>
<ItemTemplate>
<asp:Label ID=
"lblFruitSubCategoryID"
runat=
"server"
Text=
'<%# Eval("id") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign=
"Center"
>
<ItemTemplate>
<asp:Image ID=
"imgFruitImage"
runat=
"server"
Width=
"100px"
Height=
"100px"
/>
</ItemTemplate>
<EditItemTemplate>
<asp:Image ID=
"imgEditFruitImage"
runat=
"server"
Width=
"100px"
Height=
"100px"
/>
<%--<asp:Label ID=
"imgEditFruitImage"
runat=
"server"
Text=
""
></asp:Label>--%>
<asp:FileUpload ID=
"fileEdit"
runat=
"server"
CssClass=
"btn btn-blue-2"
/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Sub fruit category"
>
<ItemTemplate>
<asp:LinkButton ID=
"lbSubFruitCategoryName"
runat=
"server"
CommandName=
"view"
CommandArgument=
'<%# Bind("id") %>'
Text=
'<%# Bind("subFruitCategoryName") %>'
></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=
"txtEditSubFruitCategoryName"
CssClass=
"form-control"
runat=
"server"
Text=
'<%# Bind("subFruitCategoryName") %>'
></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Fruit category"
>
<ItemTemplate>
<asp:Label ID=
"Label1"
runat=
"server"
Text=
'<%# Bind("categoryName") %>'
></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID=
"ddlEditCategoryName"
CssClass=
"form-control"
runat=
"server"
></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width=
"60px"
>
<ItemTemplate>
<asp:Button ID=
"btn_Edit2"
runat=
"server"
Text=
"Edit"
CommandName=
"Edit"
CssClass=
"btn btn-blue-2"
/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID=
"btn_Update2"
runat=
"server"
Text=
"Update"
CommandName=
"Update"
CssClass=
"btn btn-blue-2"
/>
<asp:Button ID=
"btn_Cancel2"
runat=
"server"
Text=
"Cancel"
CommandName=
"Cancel"
CssClass=
"btn btn-default"
/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width=
"100px"
>
<ItemTemplate>
<asp:LinkButton ID=
"LkB11"
CssClass=
"btn btn-danger"
runat=
"server"
CommandName=
"Delete"
><i
class
=
"fa fa-trash-o"
></i> Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor=
"#009b97"
Font-Bold=
"True"
ForeColor=
"White"
Height=
"35"
/>
</asp:GridView>
And code behind for Paging is:
protected
void
GridView2_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
Admin_BO_Fruits obj_Admin_BO_Fruits =
new
Admin_BO_Fruits();
Label lbl = (Label)GridView2.Rows[e.NewPageIndex].FindControl(
"lblFruitCategoryID"
);
obj_Admin_BO_Fruits.Admin_BO_FruitCategoryID = Convert.ToInt32(lbl.Text);
GetAllSubFruitCategoryByFruitCategoryID(obj_Admin_BO_Fruits);
//GridView2.DataBind();
}
Reply
Answers (
1
)
webconfig issue
How to prevent loading content in mobile device