asp.net GridView Pager disappears! - asp.net

I have a datagridview that is using paging, it works perfectly fine and I have a drop down that allows the user to change the 'PageSize' property - 10, 15, 25, 50, 100, 1000 etc.
When I select a value for the PageSize that is greater than the row count for the grid the Pager is disappearing from both the top & bottom of the grid.
Anyone got any ideas why?
I'm using a custom PageTemplate element in the aspx page.
Cheers
Ollie

Behaviour is by design. You can force it to remain visible by setting the Visible property of the pager row (accessed using either TopPagerRow or BottomPagerRow property) in the grid's OnDataBound event. For example:
protected void grid_DataBound(object sender, EventArgs e)
{
grid.TopPagerRow.Visible = true;
}

I found that this happens if you are trying to force a column to be invisible.
for example if you use:
e.Row.Cells[0].Visible = false;
You can cause the pager to render invisible.
You should use this code instead:
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}

When the number of pages is one, there is no need to show Next/Previous or other pages. Sounds like normal behavior to me.

the Issue is Related to Design so Please Go to The Properties of Rad Grid View and Just Change The Property : Style-->PagerStyle-->AlwaysVisible To (True)

Verify the GridView.VirtualItemCount
Sometimes after update DataSource this value is not the same.
For Exemple: First time => 100 and in the next time you set a different value to the same database query.
http://www.nullskull.com/articles/20060109.asp

Related

GridView column width

I'm using VS 2010. I have a GridView with few template columns. I want the 2nd column to not be visible at all, but still to be existed so javascript will be able to see it's value.
Does someone knows how to set this width value?
Thanks
Place a HiddenField in the first column and put the value that you need to put it in the second column in it instead of creating the second column.
The Problem:
Your problem originates from the fact that when you hide a data-bound GridView's column, its bounded value is no longer available and if you tried to access it you will get an empty string.
The solution:
Enable 2 events in in your gridview:
RowDataBound: In this event you can access the hidden cell value (before hiding it yet)
protected void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs)
{
// Here you store the value
this.sID = e.Row.Cells[1].Text;
}
RowCreated: In this event you hide the cell, write this in the event handler:
protected void MyGridView_RowCreated(Object sender, GridViewRowEventArgs)
{
// then you hide the cell (Only the cell not the column)
e.Row.Cells[1].Visible = false;
}
In these codes, after we save the value we need in another variable/array whatever, we can easily hide the cell. You can put that value in a hidden input to enable accessing the value from javascript.

asp:GridView doesn't contain control added in OnRowDataBound

I have problem, I can't get control which I added in DataGrid. I am adding it in OnRowDataBound event like:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
//int cindex = 0;
//for (cindex = 0; cindex < e.Row.Controls.Count; cindex++)
foreach (Control ctl in e.Row.Controls)
{
DataControlFieldCell dcctl = (DataControlFieldCell)ctl;
TableCell tcell = (TableCell)dcctl;
Label lblComment = new Label();
TextBox txtComment = new TextBox();
lblComment.Text = "<br>Comment: ";
dcctl.Controls.Add(lblComment);
dcctl.Controls.Add(txtComment);
//tcell.Controls.Add(lblComment);
//tcell.Controls.Add(txtComment);
//e.Row.Cells[cindex].Controls.Add(lblComment);
//e.Row.Cells[cindex].Controls.Add(txtComment);
What is happening here: there is already exist one TextBox in TableCell by default and I want to add another one TextBox and Label. After the bounding I can see 2 textboxes, I can input data into the both, but when I click Update button, then raises OnRowUpdating event where I can't get my TextBox!
protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
grdView.EditIndex = -1;
int counter = 0;
for (counter = 0; counter < grdView.Rows[e.RowIndex].Cells.Count; counter++)
{
foreach (Control ctl in grdView.Rows[e.RowIndex].Cells[counter].Controls)
{
And here I will be getting only default one TextBox (with its value). But my TextBox is disappeared! :(
What could you suggest me here to do?
P.S. I can't use predifined columns, like asp:TemplateField in aspx file, because my table has different amount of rows every time. It is dynamic
The issue is that after you dynamically add a control to a page (or any of the page's child controls such as your datagrid) then you must recreate the controls on the server side on postback. If you don't recreate the controls on the server side, then when the runtime processes the postback it will have no idea where to put the contents of the form post.
So essentially when the page is processing the postback, it sees an HTML field called "gridView1_txtComment" (the actual HTML id is probably something else, I know). But the server side code model only has an instance of gridView1, there isn't an instance of a TextBox named txtComment unless you run the RowDataBound method again to create that control.
I think it has to do with ViewState. Make a templated column out of it, then add the second textbox to the template.
I did it!
Refused of dynamically adding controls in OnRowDataBound, and created dynamical TempalteField columns, which were containing needed to me 2 TextBoxes and Label. (With help of http://www.codeproject.com/KB/aspnet/create_template_columns.aspx)
But after my problem returned back.. On OnRowUpdating event still was not having my added TextBoxes. Finally I've found here notice http://forums.asp.net/p/1537632/3738331.aspx, that it is needed to implement TempalteField-s adding on Page_Load, that helped me to solve the problem!

dynamic data - all option in paging

I'm developing a dynamic data web app. On list.aspx page it has GridViewPager control for paging, it option in drop down list as 10,20,.. rows in a page like that, but it does not an option show all rows in a page.
How I can add "All" option in it?
I assume you are referring to a GridView and the automatic paging functionality included. If not please clarify. However, if this is the case then the default paging options do not include a show all. You can roll your own, I would start here: http://msdn.microsoft.com/en-us/library/5aw1xfh3.asp
In content folder Dynamic Data site lies the code for GridViewPager control.
What I have done is I added "All" option in dropdown list with values 0, and in the code behind file in function DropDownListPageSize_SelectedIndexChanged I check if selected value is 0 then set AllowPaging = false else true.
protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
if (_gridView == null)
{
return;
}
DropDownList dropdownlistpagersize = (DropDownList)sender;
int sz=Convert.ToInt32(dropdownlistpagersize.SelectedValue);
//_gridView.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue);
if (sz<=0)
{
_gridView.AllowPaging = false;
//_gridView.DataBind();
//return;
}
else
{
_gridView.AllowPaging = true;
_gridView.PageSize = sz;
_gridView.AllowPaging = true;
}
int pageindex = _gridView.PageIndex;
_gridView.DataBind();
if (_gridView.PageIndex != pageindex)
{
//if page index changed it means the previous page was not valid and was adjusted. Rebind to fill control with adjusted page
_gridView.DataBind();
}
}
You will have to implement your own pager and attached it to the Gridview. Default pager will not give you this option. May be this link could help you. http://www.codeproject.com/KB/grid/GridView_pager.aspx

Forcing ASP.NET GridView's pager to show

is there some way to force the gridview's pager to show up, even when there is only one page of data on screen?
I'm building a gridview-based control with a custom pager (w/dropdown for pagesize) and everything is working fine, except when user selects pagesize that is larger than the current row count of the grid. At that point the pager disappears. I've been googling this and i think that i should be doing something in override OnRowCreated...
Custom pager is added by overriding InitializePager. I'll be glad to provide more information if required!
greets,
J.Arola
Ok, that wasn't too hard :-)
Based on my initial testing the following did the trick:
GridViewRow pagerRow = (GridViewRow) this.BottomPagerRow;
if(pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
I just added that to overridden OnPreRender, and lo, pager is visible, even when there is just one page page of data shown. Got to do some additional testing before I can be sure, though. Seems to simple to me.
The above will work
But this might be helpful also
GridView.BottomPagerRow.Visible=true
protected void GridView_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow pagerRow = (GridViewRow)gv.BottomPagerRow;
if (pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
}
GridView.BottomPagerRow.Visible=true works like a charm

gridview asp.net mouse over TR find which row was clicked on server side code

I am pretty new to the whole JavaScript thing. I have a gridview that I want the user to be able to hover over the whole row (believe its the whole TR) and be able to click anywhere and that would be able to select that row. I need the server-side code to be able to know which row was clicked.
I don't really know where to start with this and would love some guidance on:
Best way to add the ability to show that the user is hovering over a row (changing the background colour or something)
How to hook up the ability to click anywhere on that row and fire server-side code to know which row was clicked.
Was reading this article http://www.dotnetcurry.com/ShowArticle.aspx?ID=109 on firing server-side code from client-side but don't know how to figure out what row it would have come from.
This should do the trick, just make the gridview selectable
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.background = '#CCCCCC';";
e.Row.Attributes["onmouseout"] = "this.style.background = '#FFFFFF';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.Gridview1, "Select$" + e.Row.RowIndex);
}
}
The click will be detected server-side by the GridView click event.
I always work with repeater control, for hover effect on repeater i use jQuery
This goes between the <head> tags
$(document).ready(function() {
$(".tr-base").mouseover(function() {
$(this).toggleClass("trHover");
}).mouseout(function() {
$(this).removeClass("trHover");
});
});
and this is the CSS class.
.trHover{background-color: #D5E5ED;}
If you won't do anything in server-side when the row is clicked you can use jQuery again for the selected effect.

Resources