Gridview PageIndexChanging not Firing - c#

I am writing my own event handler for GridView in PageIndexChanging event, I didn't explicit set the DataSourceID for my GridView, here is my code:
Code for GridView data binding:
protected void DetailsView_DataBound (object sender, EventArgs e )
{
Customer cust = (Customer)DetailsView.DataItem;
this.GridView.DataSource = cust.Orders;
this.GridView.DataBind();
}
This part of the code allows me to show order details in GridView when data bound with DetailsView. Then I write my own GridView_PageIndexChanging event handler and DOES NOT work for me:
protected void GridView_PageIndexChanging(object sender, EventArgs e)
{
GridView.PageIndex = e.NewPageIndex();
GridView.DataBind();
}
If I click the next page number, the website shows nothing. But if I change GridView.DataBind() to DataBind() The paging works.
Anyone has any idea why the second Databind method works and what is the reason?

the second databind is DataBind(Page.DataBind) which refrence to current page binds all page controls and its child controls.

Have you set allowPaging="True" of gridview. if not then set it to true.

Related

why my radcombobox is empty after postback?

I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element

add image to datatable or gridview

I have a datatable that I bind to a gridview. The columns are variable so I'd like to take advantage of AutoGeneratedColumns. I'd like to bind an image in certain condtions. What do I need to do?
void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv != null)
drv[1] = new HtmlImage() { Src = "add.png" };
}
It sounds like the AutoGeneratedColumns property won't help you here because column types apply to the whole GridView; they are not calculated per-row.
You might be able to use a TemplateField with databinding to conditionally format the field for each row without writing any code.
If that doesn't get it done for you, I suppose you will have to write code. Bear in mind that the RowCreated event always fires (event on postback) when a row is created, but will only give you a non-null DataItem (e.Row.DataItem) when the GridView actually goes to its DataSource for databinding; if the GridView has cached its rendered state (in ViewState), the data item will be null. At that point, you would only be able to access the row's primary key fields by doing something like this: var keys = myGridView.DataKeys[rowIndex]; (The primary key fields are determined by the value you give the GridView's DataKeyNames property, and are stored in ViewState so that you can access them on postback.)
You would also be careful when modifying a column that is some type of DataBoundField (as most Fields are); since the RowDataBound event happens after the RowCreated event, any manual changes to the content of a row/cell you make in the RowCreated event handler are going to be clobbered by databinding when RowDataBound is fired.
That said, RowDataBound is probably the event you want.
The RowDataBound event will always give you a non-null DataItem, but only fires when real databinding happens (as opposed to "binding" from ViewState); so typically this event does not fire at all on postbacks. That's OK, though, since the GridView will remember its state for you.
If you must use code, it should probably look something like this:
//don't forget to attach this event handler, either in markup
//on the GridView control, in code (say, in the Page_Init event handler.)
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
//HtmlImage gives you a plain-vanilla <img> tag in the HTML.
//If you need to handle some server side events (such as Click)
//for the image, use a System.Web.UI.WebControls.Image control
//instead.
HtmlImage img = new HtmlImage() { Src = "path/to/image.jpg" };
e.Row.Cells[1].Controls.Add(img);
}
But, seriously, check out TemplateField first.
You can use RowDataBound event to process each row:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv != null)
{
// your code here...
}
}
}
For more information about this event see here
This should work, it uses the controls of the actual cell:
void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
HtmlImage img = new HtmlImage() { Src = "add.png" };
e.Row.Cells[1].Controls.Add(img);
}

Dropdownlist values comes null on button click.Dropdownlist is placed at first row of gridview

Here is code that i used to access dropdownlist inside gridview on button click.I know its due to postback of page.But how to fix it.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
On page load i have called the method to bind gridview.
if (page.ispostback==false)
{
grdbind();
}
try with 0th cell if it is first
(row.Cells[0].FindControl("ddlCol1"))

Changing the FormView record while in insert mode

I have 2 ObjectDataSource's - 1 for the GridView and 1 for the FormView.
I enabled selection on the GridView and all is well; the page loads, you select a record, and the FormView populates with the record.
However, once the FormView is in Insert mode, when I select a record on the GridView, it is not changing back to display the record I selected.
Where could I handle this in the code, as I don't see how to access the "Enable Selection" on the GridView in code behind?
What I have tried
protected void grdSearchGroup_SelectedIndexChanged(object sender, EventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
protected void grdSearchGroup_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
I think the simplest way to solve your problem is to handle the GridView's SelectedIndexChanging method, and set the FormView back to read-only mode (that way you can see the records details in the FormView like you want).
protected void yourGridView_SelectedIndexChanging(Object sender, EventArgs e)
{
yourFormView.ChangeMode(FormViewMode.ReadOnly);
}
Note that you must call the FormView's ChangeMode method in order to update the mode. Just setting the DefaultMode property is not enough.

Lost GridView SelectIndexChanged at UserControl

I have a UserControl which contain GridView.I set AutoGenerateSelectButton is true for this GridView.
But it didn't work when I press Select inside UserControl.
Do you have anyidea?
You should move your event handling to the page that owns the UserControl and not in the UserControl
Inside the Page_Load of your page, add this
myUserControl.FindControl("GridView1"));
dvGrid.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
Add the handler to the page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//access the GridView
GridView grid = (GridView) sender;
//access the selected row
GridViewRow selectedRow = grid.SelectedRow;
//access the selected Primary key - make sure you set the DataKeyNames property of the GridView to the Record Id - in your Markup
string currentRowPrimaryKey = grid.SelectedValue;
//OR
string currentRowPrimaryKey = grid.SelectedDataKey.Value;
}
Now you have several values to play with. You can put a break point and examine the properties of the sender to have more options. Good Luck

Categories