How can you invoke the paging of a gridview using button from the outside of the gridview?
I have searched it but unfortunately I can't find the right logic for it. I'm not talking about creating a button for preview and next function inside the pager template of the gridview, but outside, the gridview itself like a remote button. Whenever I click either the preview or next button, the gridview will do the same.
Suppose you have the Button outside the Grid View. You can do grid view paging as follows on the button click.
Basically we need to set the pageindex of the grid view and rebind the data. You need to have the data stored in session/cache or get it afresh for the gridview.
protected void ButtonNext_Click(object sender, EventArgs e)
{
++gridview.PageIndex;
// if we have reached the last page, either set to first page.
// you could also do nothing.
if (gridview.PageIndex >= gridview.PageCount)
{
gridview.PageIndex = 0;
}
gridview.DataSource = GetGridViewData(); // the data needs to be available.
gridview.DataBind();
}
Related
I am using Telerik gridview in a winform application. When I click on the row the control goes to another page with some data. I am using grid views click event to pass the control to another form. But even when you click on header column or pager row it is going to the next form with data selected from the first row. What is the way to figure out the type of row. Whether it is a datarow or a header row?
private void grdGuests_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(this.grdGuests.CurrentRow.Cells[1].Value.ToString());
GuestDetails gd = new GuestDetails(id);
gd.Show();
}
I even tried to use MouseClick but its the same thing if I click on next page button or header row it takes it as a grid click event and pass the control to next page.
You can use the CurrentRowChanged event and check if the row type is GridViewDataRow info, so you avoid non-data rows.
I have an aspx webform written in C#. This page has three dropdown boxes. These dropdown boxes load values for Department, Site and Team from the corresponding tables.
Based upon logged in user's permissions an item will be preselected from these three dropdown boxed during page load. On the same page I have a gridview whose data should be filtered based upon the selected values of these three dropdown boxes.
The sql for the gridview's data source should be something like this: select * from abc where Department = <selecteditem> and Site = <selecteditem> and Team = <selecteditem>.
I have a button on the page named as Apply Filter which takes the selected items and constructs the sql, and the gridview is populated fine. However I want this filtration to happen automatically during page load event without anyone manually clicking the button after the page load.
I've tried to execute the button click event during the page event, but the filtration doesn't happen. Please let me know the best way to achieve this.
Thanks
I think you have to do like this
`protected void DropDownBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
// Here gives the DataSource for the DataGrid View for the DEPARTMENTSelection
}
protected void DropDownBoxSite_SelectedIndexChanged(object sender, EventArgs e)
{
// Here gives the DataSource for the DataGrid View for the SITE Selection (or Department and Site Selection whatever you want)
}
protected void DropDownBoxTeam_SelectedIndexChanged(object sender, EventArgs e)
{
// Here gives the DataSource for the DataGrid View for the TEAM Selection (or Department, Site and Team Selection whatever you want)
}`
Hope this Code Helps You....If I understand your question well.
In the Page_load event do the below:
Get the user name of the user who has logged in currently by:
String name = System.Web.HttpContext.Current.User.Identity.Name;
I hope you could figure out the permissions levels of the user with the user name.
According the permission level set the desired selected index value of your three different drop down list like below (sample)
departmentDropDownList.SelectedIndex=1;
After setting the desired value for all the three drop down do the Grid DataBind().
NOTE: I'm pretty sure you would not require this automated GridView DataBind to happen for all the post back. If that is true then put the above Page_Load code within the below condition so that this automated filtering happens only for the initial page load.
if(!IsPostBack){
}
I am using a DataGrid in an ASP.NET page.
When someone clicks a row with their mouse, I want to fill in data on my form using info from that DataGrid's Selected Row.
I have wired up the control's SelectedIndexChanged event, but there does not seem to be any way to access the individual rows.
protected void DataGridRow_Selected(object sender, EventArgs e) {
var row = grid1. ???
}
Is there a way to do this?
I use Windows Forms mostly, so I could be missing something completely obvious to people who use WebForms on a more frequent basis.
You can use Ajax like JQuery to get the content of that grid cell and populate to your form field.
$("#inputfield").value($("#gridcell").html())
I have a linkbutton in a gridview that when clicked will update the DB and should remove it from being visible in the gridview. This is also in an updatepanel.
When clicking the linkbutton the DB is updated, however the gridview is never refreshed.
Both gridview and linkbuttons are dynamically generated.
Linkbuttons are created as follows:
'b' contains the unique id of the data in the row.
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbRemove = new LinkButton();
lbRemove.ID = "removeLink" + b;
lbRemove.Command += new CommandEventHandler(lbRemove_Click);
lbRemove.Attributes.Add("onclick","return confirm('Are you sure?');");
.......
e.Row.Cells[6].Controls.Add((Control)lbRemove);
lbRemove_Click contains the method to update DB and call the griview to bind amd update the panel:
protected void lbRemove_Click(object sender, CommandEventArgs e)
{
removeFromUser(Convert.ToInt32(e.CommandArgument.ToString()));
loadGridviews(Convert.ToInt32(ViewState["currUserID"]));
upnlUserDevices.Update();
i have tried creating a linkbutton outside of the gridview using the exact same properties as the one in the gridview. When clicked it calls the same method and it refreshes the gridviews, just not when being clicked from within the gridview itself.
Bit stuck on this one if you can help??
Thanks!
Looking at your code it seems to be alright. I can only think of two suggestions:
Make sure your are doing the DataBind to the GridView at the end of the method loadGridViews()
YourGridView.DataBind();
Make sure you are updating the right UpdatePanel after doing the binding:
upnlUserDevices.Update(); // is upnlUserDevices the UpdatePanel that wraps your GridView?
Hope this helps.
So i found out how to make this work.
I need to set the linkbutton .CausesValidation = false
No expert on quite what this does, but it does fix my issue!
put this gridview in update panel
I have the selected DataKey in session from the ListView.
I am able to set the selection back when I comeback to this aspx page containing listview.
But when the selected item in the listview belongs to some other page (not the first listview page) then I need to also set the selected listview page to the one, where my item belongs.
I use a listview and a datapager (with template paging)
How can I find, in which page my item to be selected exists?
Can I search for the datakey value's page and then activate it?
Well the simplest solution I could apply was to also save the pageindex on session.
protected void ListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{ CurrentPageSessionVariable = (e.StartRowIndex / e.maximumRows);
}
Now on pageload...
dataPager1.SetPageProperties(CurrentPageSessionVariable * dataPager1.PageSize, dataPager1.MaximumRows, true);
This will ensure that when we comeback to this page, the datapager is signaled to load the specified page and show the selected item(which is separate code).