Asp.net concerned with gridview paging - asp.net

I am trying to paging in asp.net.
so how to code for the paging.I want group of 5 items in bunch and next page carry same group of information.

In your grid view you can add these line which will set your page with a group of 5 items
<PagerSettings PageButtonCount="5"></PagerSettings>

Try this for pagination in Grid View.
protected void TeacherGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
myGrid.PageIndex = e.NewPageIndex;
bindDataToGrid();
}
// add columns per your requirement and bind the grid with data source.
<asp:gridview id="myGrid" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="5" OnPageIndexChanging="myGrid_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

In your GridView you have to give the size that you want to show the number of results in the GridView also in code behind you have to set your page index so whenever you go to the next 5 records, it will show the next 5 records instead of same.
Fix the PageSize also use a OnPageIndexChanged to show the unique records every time.
<asp:GridView ID="myGridView" PageSize="5" OnPageIndexChanging="myGridView_OnPageIndexChanging" >
in codebehind you write this code in you OnPageIndexChanging...
protected void myGridView_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.myGridView.PageIndex = e.NewPageIndex;
this.myGridView.DataSource = this.Session["myData"];
this.myGridView.DataBind();
}

Related

How to use checkbox check change without refreshing page in ASP.NET

I have two checkboxes in a gridview. The scenario is this: only one of these checkboxes can be chosen or none of them.
My problem is that when my gridview got too big, every time I check or uncheck each row the page refresh that is because of I set auto post back = true! What should I do to avoid this refreshing and the scenario works except UpdatePanel!
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White">
<Columns>
<asp:TemplateField HeaderText="morakhasi" ItemStyle-Width="80">
<ItemTemplate><asp:CheckBox ID="chkboxMorakhasi" OnCheckedChanged="chkboxMorakhasi_CheckedChanged" runat="server" AutoPostBack="true"/>
</ItemTemplate>
<HeaderStyle Font-Size="16px" Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="taiid" ItemStyle-Width="80">
<ItemTemplate><asp:CheckBox ID="chkboxTaiid" OnCheckedChanged="chkboxTaiid_CheckedChanged" runat="server" Checked="True" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</gridview>
Code behind:
protected void chkboxTaiid_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView2.Rows)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
int index = row.RowIndex;
CheckBox cb1 = (CheckBox)GridView2.Rows[index].FindControl("chkboxTaiid");
CheckBox cb2 = (CheckBox)GridView2.Rows[index].FindControl("chkboxMorakhasi");
cb2.Checked = Convert.ToBoolean(0);
}
}
protected void chkboxMorakhasi_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView2.Rows)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
int index = row.RowIndex;
CheckBox cb1 = (CheckBox)GridView2.Rows[index].FindControl("chkboxTaiid");
CheckBox cb2 = (CheckBox)GridView2.Rows[index].FindControl("chkboxMorakhasi");
cb1.Checked = Convert.ToBoolean(0);
}
}
You should use UpdatePanel control to do this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:CheckBox
OnCheckedChanged="chkCompany_OnCheckedChanged"
AutoPostBack="True" CssClass="chkCompany"
ClientIDMode="Static" ID="chkCompany" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Well, first up, when you say the grid is large? Well, how large? 20 rows or 2000 rows?
When Google, or even Microsoft asks interview questions? A good number of these questions are of a how high, how far, how big type of questions. And the reason for that is simple:
They want to determine if you have a mind can is able to evaluate scope and scale of problems that you have to solve. (so, they might ask how would you move Mount Fuji?)
anyway:
I can't image the grid has what, 30 rows on a page? (why so many and why so large??).
I mean, if you have more, then we need to know how many more?
However, lets get the first problem solved. (the check box issue), and then we can try and deal with the re-plot/refresh issue.
Ok, so say we have this grid markup - grid + 2 check boxes
<asp:GridView ID="GVHotels" runat="server" class="table" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" HeaderStyle-Width="200" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Choice 1" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:CheckBox ID="chk1" runat="server"
OnCheckedChanged="chk1_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Choice 2" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:CheckBox ID="chk2" runat="server"
OnCheckedChanged="chk2_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code to load the grid is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.TEST3))
{
using (SqlCommand cmdSQL =
new SqlCommand("SELECT * from tblHotels ORDER BY HotelName ", con))
{
con.Open();
GVHotels.DataSource = cmdSQL.ExecuteReader();
GVHotels.DataBind();
}
}
}
And now we have this:
And our two check box changed events are this:
protected void chk1_CheckedChanged(object sender, EventArgs e)
{
CheckBox ck = (CheckBox)sender;
GridViewRow gRow = (GridViewRow)ck.Parent.Parent;
if (ck.Checked)
// uncheck the other box
((CheckBox)gRow.FindControl("chk2")).Checked = false;
}
protected void chk2_CheckedChanged(object sender, EventArgs e)
{
CheckBox ck = (CheckBox)sender;
GridViewRow gRow = (GridViewRow)ck.Parent.Parent;
if (ck.Checked)
// uncheck the other box
((CheckBox)gRow.FindControl("chk1")).Checked = false;
}
Ok, so we are done.
the next issue is performance? A grid of say 20-40 rows on the form should work ok.
I think just drop a update panel around the grid, and you done.
So, we drop in script manager, and then this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
-- above grid goes here!!!
</ContentTemplate>
</asp:UpdatePanel>
Now, I just tested the above with 50 rows on the screen. I can't see or feel, or notice ANY replot delay, and in fact the check box reacts as fast as my mouse click. In fact the check box status changes quite much as fast as the mouse click, and in fact quite much at the same time I hear the click.
So, above is the layout for the code you require here. If there is some "slow" ness, or delay, or your data set is larger, then perhaps some kind of data pager should be introduced. On the other hand, you not shared, or noted how many rows this grid is displaying on the screen. And without that information, we shooting in the dark here as to what possible solutions(s) exist (or that what you have is a very bad idea).

itemTemplate item id not existing in code behind

I am trying to create textboxes that are equal to the number of rows in grid view (databound from db). here is my markup
<asp:GridView ID="quizGrid" runat="server" CssClass="Grid" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="admissionNO" HeaderText="Admission NO"/>
<asp:BoundField DataField="studentName" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Textbox runat="server" ID="marks" > </asp:Textbox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
but when i use the marks in code behind it says
quizGrid_marks_0 does not exists in the current context
what im doing wrong here?
You can't access your textbox like that in code behind file, rather you need to find them in RowDataBound event like this:-
protected void quizGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox marks = (TextBox)e.Row.FindControl("marks");
txtMarks.Text = "Test";
}
}
Edit:
Okay, suppose you have a button btnGetData with button click event as btnGetData_Click, then you can find the textbox text by looping through the gridview rows like this:-
protected void btnGetData_Click(object sender, EventArgs e)
{
GridView quizGrid = (GridView)Page.FindControl("quizGrid");
foreach (GridViewRow row in quizGrid.Rows)
{
TextBox marks = (TextBox)row.FindControl("marks");
}
}

Dropdownlist disappear when put it in EditItemTemplate

If I put my dropdownlist column in ItemTemplate, it appear but I can't change the value. When I put it in EditItemTemplate like this:
<EditItemTemplate>
<asp:DropDownList DataValueField="COLUMN_NAME" DataTextField="COLUMN_NAME" DataSource='<%#GetDataSourceDesCol()%>' Width="90%" Visible=true ID="ddlDesCol" runat="server">
</asp:DropDownList>
</EditItemTemplate>
Then my ddl is not showing any more. How to fix it?
p/s: Even that I try with a new project and simple code like:
<asp:GridView ID="GridView1" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The ddl is not showing too!
All the controls inside the edit item template will be visible only when the grid view is in the edit mode.
So, you need to set the grid to edit mode. Inorder to make your code to work.
Hope this helps..
if you want to put entire grid view in edit mode:
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
if you want a particular row to be in edit mode
Just implement the Row_Editing event and do something like this:
protected void Row_Editing(object sender, GridViewEditArgs e)
{
myGridView.EditItemIndex = e.EditItemIndex;
BindData();
}
Bind data will populate the GridView with the data.

Why does GridView inside a Repeater (or other similar controls) has the event PageIndexChanging as NOT IMPLEMENTED

Simply I want to know WHY!
Is it the DataSource Type of the GridView? or the Repeater inner implementation?
The error text:
The GridView 'grdArticles' fired event PageIndexChanging which wasn't handled.
Here's the Markup code, I think it's familiar enough for everyone.
<asp:Repeater ID="rptCategories" DataSourceID="ldsCategories" runat="server">
<ItemTemplate>
<asp:GridView runat="server" ID="grdArticles" AllowPaging="true" GridLines="None" DataKeyNames="id" AutoGenerateColumns="false" DataSource='<%# Eval("Articles") %>'>
<Columns>
<asp:TemplateField ShowHeader="false" ItemStyle-Width="100%" FooterStyle-Width="100%">
<ItemTemplate>
<div class="article-menu-item">
<h1>
<asp:HyperLink ID="lnkTitle" CssClass="article-menu-title" runat="server" Text ='<%# Eval("title") %>'
NavigateUrl='<%# Vars.ArticleUrl + "?action=view&id=" + Eval("id") %>' ></asp:HyperLink>
</h1>
<!-- Date -->
<div class="article-menu-date">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("date") %>'></asp:Label>
</div>
<!-- Meta Content -->
<div class="article-menu-meta">
<asp:Label ID="lblContent" runat="server" Text='<%# Bind("meta") %>'></asp:Label>
</div>
<div class="article-menu-delete">
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" OnClick="btnDelete_Click" TargetID='<%# Eval("id") %>' />
</div>
<!-- Line -->
<div style="border-bottom: 1px solid #ccc"></div>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<p>No items to view!</p>
</EmptyDataTemplate>
</asp:GridView>
<br />
</ItemTemplate>
</asp:Repeater>
EDIT:
If I implement the OnPageIndexChanging event using the code (And modified the aspx gridview markup to handle the event):
ASPX:
<asp:GridView runat="server" ID="grdArticles" AllowPaging="true" OnPageIndexChanging="grdArticles_PageIndexChanging" GridLines="None" DataKeyNames="id" AutoGenerateColumns="false" DataSource='<%# Eval("Articles") %>'>
cs:
protected void grdArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView grd = sender as GridView;
grd.PageIndex = e.NewPageIndex;
grd.DataBind();
}
Another exception thrown:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Edit #2:
After following Mr #Garrison solution, and handling the Repeater itemDataBound event using the code:
protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
GridView grd = e.Item.FindControl("grdArticles") as GridView;
if (grd != null)
{
DatabaseDataClassesDataContext dc = new DatabaseDataClassesDataContext();
grd.DataSource = dc.Articles.Where(a => a.category_id == (e.Item.DataItem as Category).id);
grd.DataBind();
}
}
Got another problem: No exceptions thrown, but when navigating to another page, the GridView shows NO ROWS!!
I really think that there is an issue in the repeater core implementation!
You have two options:
Remove Paging: Set AllowPaging="false" in your GridView.
Implement Paging: Set OnPageIndexChanging="grdArticles_PageIndexChanging"
Create a method in your code behind that looks like this:
protected void grdArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdArticles.PageIndex = e.NewPageIndex;
grdArticles.DataBind();
}
Now that we're past the first issue, let's tackle the second one. You need to handle your Repeater's ItemDataBound event. Inside there, first find your GridView with the following code:
var grdArticles = (GridView)e.Item.FindControl("grdArticles");
Now you have access to the GridView, but you've got to find the set of Articles you want to data bind to the GridView. I don't know how you're retrieving your data, so I leave that portion up to you, but once you find your list of articles to bind, use the following code:
grdArticles.DataSource = relevantArticles; // relevantArticles is a stand-in variable name, because I don't know how you're going to do it
grdArticles.DataBind();
I've figured out -with the help of others answers- that's the problem is with the DataSource type, which is -in my example- the Eval("Items"), I don't know really what's the type of it, but it's not supporting auto paging.
So, one way to step out of it -without rewriting paging logic and writing lots of code and get some mess with Sessions and querying- is to handle the ItemDataBound of the container of the GridView, I mean the Repeater or DataList, or you can handle the OnPreRender of the GridView and create the DataSource that supports the auto paging like LinqDataSource.
You may also need to handle the PageIndexChanging of the GridView like what #Garrison told us:
protected void grdArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdArticles.PageIndex = e.NewPageIndex;
grdArticles.DataBind();
}
After that I got everything work :)

Lost Focus Events for a Control Within GridView

I have multiple textboxes and dropdown lists within my GridView. For one particular textbox I need trigger a server event which gets data from the database and fills it in other columns of the Grid. Is there a simple way to do it or a slightly complicated way as detailed here
I have no problems implementing the above method or thinking of a work around but then thought that there is Cell Lost Focus in a grid control surprises me a little. Am I missing something ? Any help on this would appreciated.
You can set AutoPostBack to true and handle it's TextChanged event.
<asp:GridView ID="GridView1" runat="server" EmptyDataText="It's Empty.">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%#Eval("Name") %>'
AutoPostBack="true"
OnTextChanged="NameChanged" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
in codebehind:
protected void NameChanged(Object sender, EventArgs e)
{
var txtName = (TextBox) sender;
var row = (GridViewRow) txtName.NamingContainer;
// you could find other controls in this GridViewRow via
// row.FindControl("ControlID") in case of a TemplateField or
// row.Cells[0].Text (0 = index of column) in case of a BoundField
}

Resources