ASP.Net C# gridview - c#

I have a gridview with a linkbutton inside a <HeaderTemplate>. There is an event handler for a click on this button. Now if I bind data to the gridview on every Page_Load event, then this event fires. But if I bind data to the gridview inside if (Page.IsPostBack == false), then this event doesn't fire.
Is it that after the pageload it realizes there is no data in the gridview hence ignore events generated from the grid?
How do I slove the problem ?

The LinkButton behaves like a Button
The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control.
That means it triggers a postback when clicked. So the only way to get the click event to fire, is to wire up the handler if Page.Postback=true. (Keep in mind that since HTTP is stateless, if you wire up an event handler on the initial page load, it won't "remember" upon postback.)

Related

restrict firing of data grid view control validation event

I have a datagridview control in winform. The datagrid view is extended to implement validating event of the grid. The problem is I have several buttons including button named ok in my form. But, I want to validating event to be fired in ok button click. How can I restrict validating event to be fired in ok button click.

gridview click causing postback in usercontrol

I have three user controls in one aspx page, where each user control is loading in each separate tabpanel.
Each usercontrol has gridivew and objectdatasource(which has selectmethods).
The gridview is binding data from the objectdatasource, on every postback.
I have some dropdowns, the selected values are passed to the objectdatasource on submit button click, which then populates the results in the gridview.
Now, if I click on any linkbutton on the gridview, the postback is happening and gridview is rebinding with null values, so no click event of linkbutton is happening.
Anybody can suggest me how to stop the postbacks for gridview. I tried placing the updatepanels but didnt help
You can control the flow of the program by checking IsPostback in Page_Load() and filling the grid only when it's supposed to be filled. You can also specify your UI elements to not postback as well, if certain ones are posting back when you don't want them to. Look into the AutoPostBack property.
If (IsPostBack == true) {
// do something, load that
}
Else {
// do something else, don't load that
}

asp.net, Gridview RowEditing event only works on first row

Im working on asp.net with c#.
I have a gridview with templatefield columns, data comes from an sql database. I have linkbutton on the item template, the linkbutton calls the Rowediting event to enable the editing. This works fine on the first row. But when I click on any of the other rows nothing happens, the event never gets fires.
How can I solve this?
Thanks..
Most likely you are data-binding the grid in the Page_Load event. If this is the case, the ASP.NET Page Lifecycle is getting in your way. (Be sure to read the article in the link provided. Every .NET developer needs to know about the Page_Lifecycle. It explains a lot of behavior thaqt would otherwise cause confusion, such as this behavior.)
The Page_Load event happens on every postback - every button click, or any event that triggers the postback.
If this is the case, there are two possible options:
Move your data binding code to Page_Init
Put your data-binding in Page_Load inside an if(!Page.IsPostback) block.
In essence, the problem is that your page is data-binding on the first load.
Then the editing event is triggered by some client action, which triggers a postback. In this postback, Page_Load fires first, which re-binds the GridView, erasing all of the data that was associated with it on the previous load. So when the RowEditing event fires (control events always happen AFTER Page_Load) there's nothing for it to do. All references to the data as it existed before postback are gone.
If you move your binding code too Page_Init, you can get around this because the page will be bound, and then all of the Viewstate will be re-applied to it, restoring the data that was lost in the postback in the scenario above.

C# ASP .Net, triggering button control inside Gridview

I have a question about button inside the gridview. As the title says, I designed a gridview with a button inside. Whenever I trigger the button, it always binding the gridview before it can reach the button event.
Say, onLoad page, I pull data and bind them to a gridview, then I would like to do a mass update which will be triggered by the button inside it. If i put !IsPostBack, it never triggers the button event, which means it always has to read the data bind them into gridview again then execute the button event.
Problem is, I have a huge data and it takes much time to update because of it. I am wondering if there are any ways to trigger the button event without rebinding the gridview again? Is it because of the button inside the gridview so it is necessary to rebind again before it could read the button event?
Any suggestion would be really appreciated.
Why don't you use the RowCommand instead of the onclick event?
you can add a command name property to the button to understand what operation you need to do
Have a look at the link below and just replace the ButtonField with a classic button. don't forget to assign a CommandName
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx
You should not use the button event, but the GridView.RowCommand event instead. See here. If you do this in combination with the if (!IsPostBack) check in Page_Load, it should work for your scenario.
your button must be triggered by OnRowCommand event and ! IsPostBack shouldn't prevent triggering your button
protected void GridView_RowCommand(object source, GridViewCommandEventArgs e)
{
if(e.CommandName=="Update")
{
// put your update code here
}
}
and you can use ajax to update your datasource without rebinding your GridView and update your UI using javascript.

What is ItemCommand and ItemDatabound of a Data Representation Control?

Please Explain with example ItemCommand and ItemDatabound of a Data Representation Control.
ItemDataBound is an event that fires once on your server for every record bound to the control. ItemCommand is the event that will fire if you click a command button that is associated with the record.
These are events associated with data driven server controls.
ItemDataBound is fired everytime a new item is binded to your data control.
ItemCommand is fired when the command event for a Button or LinkButton or ImageButton inside the data control is used.

Categories

Resources