Hide DetailsView TemplateField from CodeBehind - asp.net

I am attempting to hide a DetailsView Row conditionally from code behind. I'd like to be able to call SomeMethod based on some condition and have it to hide Row 12. Does anyone know how I can go about accomplishing this?
I have used the debugger to confirm that Row[12] is the correct row and that the method is being executed.
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Rows[12].visible= false;
//Alternative methods I attempted that also did not work.
//DetailsViewRow row = DetailsView1.DataItem as DetailsViewRow;
//row[12].Visible = false;
//DetailsViewRow row = DetailsView1.Rows[12];
//row.visible = false;
}
This is my aspx.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="ObjectDataSource2">
...
<asp:TemplateField HeaderText="Item"
SortExpression="Item">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...

You have to use Fields collection of DetailsView. Your method should look like:
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Fields[12].Visible= false;
}
To test create add a DetailsView to page, add a button:
<asp:DetailsView ID="DetailsView1" AutoGenerateRows="false" runat="server" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<br />
<asp:Button ID="btnHide" runat="server" Text="Hide" OnClick="SomeMethod" />
And add the following code in code behind:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DetailsView1.DataSource = GetProduct();
DetailsView1.DataBind();
}
}
private List<Product> GetProduct()
{
return new List<Product>()
{
new Product{Id=1, Name="Paper", Category="Stationery"},
new Product{Id=2, Name="Pen", Category="Stationery"},
new Product{Id=3, Name="Pencil", Category="Stationery"}
};
}
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Fields[0].Visible = false;
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
}
And here's the result:

Related

GridView not showing up in ASP.NET?

For some reason my gridview is not showing up. On a debug I get rows and columns from the database when I check Results. I'm using a view to get the fields I need. I tried setting AutoGenerateColumns to true, but no luck. Am I missing something?
My aspx page:
<asp:Content ID="ContentMain" ContentPlaceHolderID="cphMain" runat="Server">
<asp:Panel ClientIDMode="Static" ID="meritData" runat="server" Visible="false" CssClass="dataTable">
<h5>Faculty Teaching Assignments</h5>
<p id="pMeritMsg" runat="server" class="userMsg"></p>
<asp:GridView ID="gridFTA" runat="server" AutoGenerateColumns="false"
ShowHeaderWhenEmpty="true"
ShowFooter="true"
DataKeyNames="FacultyId">
<Columns>
<asp:TemplateField HeaderText="Faculty Name">
<ItemTemplate>
<asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FacultyName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblFacultyName" runat="server" width="40px" Text='<%#Bind("FacultyName") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inFacultyName" width="140px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number Of Courses">
<ItemTemplate>
<asp:Label ID="lblNumberOfCourses" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNumberOfCourses" width="70px" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inNumberOfCourses" width="120px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Adjustment">
<ItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" Text='<%#Bind("Adjustment") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" visible = "false" Text='<%#Bind("Adjustment") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inAdjustment" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason">
<ItemTemplate>
<asp:Label ID="lblReason" runat="server" Text='<%#Bind("Reason") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblReason" runat="server" visible = "false" Text='<%#Bind("Reason") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inReason" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label ID="lblCourse" runat="server" Text='<%#Bind("CourseNumber") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCourse" runat="server" visible = "false" Text='<%#Bind("CourseNumber") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inCourse" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Faculty Teaching Assignment" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</asp:Content>
My code behind:
protected clsData moData = new clsData();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpContext.Current.Session["FTA"] = "";
BindData("");
}
}
protected void BindData(String FTA)
{
DataTable Results = moData.GetFTA(FTA);
gridFTA.DataSource = Results;
gridFTA.DataBind();
}
Code from the Database
public DataTable GetFTA(
string FTA)
{
// Prepare connection
SqlConnection conn = new SqlConnection(msConnString);
// Prepare command
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
if (FTA == "")
{ cmd.CommandText = "Select * from View_FTA order by FacultyName"; }
DataTable dt = new DataTable();
try
{
conn.Open();
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
logException(ex);
}
finally
{
conn.Close();
}
return dt;
}
Your asp:Panel has Visible set to false. This makes the ASP.NET engine to not to render the control (and its contents) at all.

my gridview footer is not showing the results of calculated columns

my aspx code is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="Item_id"
DataSourceID="SqlDataSource2" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Item_id" InsertVisible="False"
SortExpression="Item_id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" runat="server" Text="Insert"
Font-Bold="true" BackColor="#0099CC" ForeColor="white" Height="30px" Width="100px" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category_name" SortExpression="Category_name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Category_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Category_name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbC" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="item_name" SortExpression="item_name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("item_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("item_name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbN" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="unit_price" SortExpression="unit_price">
<ItemTemplate>
<asp:TextBox ID="TbPrice" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbP" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="QuantitySelected"
SortExpression="QuantitySelected">
<ItemTemplate>
<asp:Textbox ID="TbQt" runat="server" Text='<%# Bind("QuantitySelected") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("QuantitySelected") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbQ" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<ItemTemplate>
<asp:Textbox ID="TbTl" runat="server" Text='<%# Bind("Total") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Total") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbT" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount" SortExpression="Discount">
<ItemTemplate>
<asp:Textbox ID="TbD" runat="server" Text='<%# Bind("Discount") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Discount") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbD" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Grand_total" SortExpression="Grand_total">
<ItemTemplate>
<asp:Textbox ID="TbGtl" runat="server" Text='<%# Bind("Grand_total") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Grand_total") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbG" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
my code behind is:
public partial class ItemPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void btn_Click(object sender, EventArgs e)
{
SqlDataSource2.InsertParameters["Category_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbC")).Text;
SqlDataSource2.InsertParameters["item_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbN")).Text;
SqlDataSource2.InsertParameters["unit_price"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbP")).Text;
SqlDataSource2.InsertParameters["QuantitySelected"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbQ")).Text;
SqlDataSource2.InsertParameters["Total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbT")).Text;
SqlDataSource2.InsertParameters["Discount"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbD")).Text;
SqlDataSource2.InsertParameters["Grand_total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbG")).Text;
}
double price = 0.0f;
double quantity = 0.0f;
double total = 0.0f;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tb11 = (TextBox)e.Row.FindControl("Tb2");
TextBox tb12 = (TextBox)e.Row.FindControl("Tb3");
TextBox tb13 = (TextBox)e.Row.FindControl("TbPrice");
TextBox tb14 = (TextBox)e.Row.FindControl("TbQt");
TextBox tb15 = (TextBox)e.Row.FindControl("TbTl");
TextBox tb16 = (TextBox)e.Row.FindControl("TbDs");
TextBox tb17 = (TextBox)e.Row.FindControl("TbGtl");
price = Convert.ToDouble(((TextBox)e.Row.FindControl("TbPrice")).Text);
quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQt")).Text);
total = price * quantity;
//total += total;
tb15.Text = total.ToString();
}
else
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox tb1 = (TextBox)e.Row.FindControl("TbC");
TextBox tb2 = (TextBox)e.Row.FindControl("TbN");
TextBox tb3 = (TextBox)e.Row.FindControl("TbP");
TextBox tb4 = (TextBox)e.Row.FindControl("TbQ");
TextBox tb5 = (TextBox)e.Row.FindControl("TbT");
TextBox tb6 = (TextBox)e.Row.FindControl("TbD");
TextBox tb7 = (TextBox)e.Row.FindControl("TbG");
tb5.Text = total.ToString();
}
}
}
}
}
i hv posted my edit code again. the problem is when i run the page i am getting blank textboxes in my footer. only textbox in "total" column has vales and that too is some vague value.. i have to put values in "price" "quantity" and then click "insert" button to add values into grid along with calculated value in "total" column.. i am still stuck!! plz help!!
you also need this:
if (e.Row.RowType == DataControlRowType.DataRow)
where you do the summation.
Note: the variables need to be declared outside the event handler or they will be zeroed on each iteration.
EDIT- zero out the summation variable when e.Row.RowType == DataControlRowType.Header
EDIT Do something like this:
double grand_total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
grand_total = 0;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
double price = Convert.ToDouble(((TextBox)e.Row.FindControl("TbP")).Text);
double quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQ")).Text);
double total = price * quantity ;
((TextBox)e.Row.FindControl("TbT")).Text = total.ToString();
grand_total += total;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((TextBox)e.Row.FindControl("TbT")).Text = grand_total.ToString() ;
}
}

Gridview rowupdating doesn't work

i have a simple task of updating row, on a click of update linkbutton, as
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Eval("LName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Eval("LName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">LinkButton</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
at CS page
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1 = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
string s = t1.Text.ToString();
Label3.Text = s;
}
I tried working on this several times but i don't get any answer. what possible code i am missing or there is any error in a code.Thanks for any assistance.
Your LinkButton needs to have a CommandName applied to it in the EditTemplate:
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>

Grid View Styling Asp.Net

i want to style the grid view like i have to show 4 orders on the grid view in which 2 order has status true and 2 order has status false now i want to apply style like those order have false status should visible in yellow color and those orders are true should visible in normal color like black here is my grid view
<asp:GridView ID="CateringGridView" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
<RowStyle CssClass="GridRow" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People Attending">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("PeopleAttending") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Event Type">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("TypeOfEvent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Event Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("DateOfEvent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my code behind file is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.FillCateringGrid();
}
}
private void FillCateringGrid()
{
using (CateringOrderDataContext dataContext = new CateringOrderDataContext())
{
var query = dataContext.Caterings
.Where(e => e.Status == true)
.Select(e => e);
this.CateringGridView.DataSource = query;
this.CateringGridView.DataBind();
}
}
<RowStyle CssClass='<%=((Catering)Container.DataItem).Status ? "A" : "B"%>' />
Where A and B are class names based on whether the Status is true or false. then you can define the CSS format for td.A and td.B.

'GridView1' fired event PageIndexChanging which wasn't handled

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="5"
AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="5">
<Columns>
<asp:TemplateField HeaderText="contact_id">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
<asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
<asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="context">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>PENDING</asp:ListItem>
<asp:ListItem>OK</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle CssClass="button" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
You will have to handle the PageIndexChanging event for the grid
Something like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx
You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...
Add one more event in HTML mark up for pagging.
OnPageIndexChanging="GridView1_PageIndexChanging"
Now handle same event from code behind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}

Resources