C# ASP .Net, triggering button control inside Gridview - c#

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.

Related

Must click a button twice to fire an event

I have a data grid that is created dynamically to display information retrieved from a database. The data grid consists of multiple bound columns and two button columns for each row, a view and a delete button.
Clicking either button calls the Page_Load() method with Page.IsPostBack as false.
if (!Page.IsPostBack)
{
this.bindForm(); // Populates drop down lists that the user can select
setAccess(false); // Sets access for the page
// Stuff commented out to avoid confusion
}
else
{
bindDynamicGrids(); // Create the data grid(s) and populate them
}
So when I first click the button the above code gets called with IsPostBack false, and the dynamic grids get bound. But, the button click event does not fire. When I click the button a second time, Page_Load is called with IsPostBack false, and after executing bindDynamicGrids() the button click event is fired. I cannot understand the difference between the first and second click.
I have read a few threads;
ASP.NET C#, need to press a button twice to make something happen
http://forums.asp.net/t/1783694.aspx?ASP+NET+Button+needs+to+be+clicked+twice
To attempt to understand the issue, but I must be missing something. From what I am gathering from the second link, a session variable may be getting set in the click event, which is also being set in Page_Load, and this is all an issue with ordering. If that is the case I am not seeing where it is happening.
When it is not a post back the bindForm() method is called, which populates all drop down lists. The edit click event populates those drop downs with the values from the row, but the click event is always a post back and the form has already been bound.
I have also considered having a script automatically double click one of the buttons anytime the user single clicks, but I have not been able to find an "OnClick" property for the column button. Any help that could be provided would be phenomenal.
The thing is, that the button click event happens after the Page_Load event meaning that the filtering does not get applied on the first postback. It has been updated on the second postback and you see the filtering.
You can try to move the code of your page_load event to OnPreRender so the reload happens after the button click event.
for more information look here: Button needs to be clicked twice
and here

SelectedIndexChanges fires before TextChanged event

I have a grid which contains multiple columns/rows with ASP TextBox controls. In addition to this, above the grid I have a radio button list which contains years.
The user can enter numbers into the TextBoxes and then click an item on the RadioButton list to save the information then switch years or click an update button to just save the information and remain viewing the data.
If I click the 'save' button the textchanged event handler fires and I know what rows on my grid had something changed, then I update my DB and then get the data again to display to the user.
If however I click the radiobuttonlist to switch years the SelectedIndexChanged event fires but the TextChanged event handler does not run because the save and get data runs first, rebinding the grid and eventhandler.
This appears to me to be something to do with the way events run in .net, does anyone know how can I get the textchanged event handler to run first when clicking a radio button list?
I'm using VS2005, .Net 2.5, ASP.Net, C#
Thanks in advance
Your question is not clear. Anyway i can understand that you have problem with the event handlers and how they are called.
I want to know what is the need of calling the textchanged event and SelectedIndexChanged. Even they are not called your data would be get saved perfectly.

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.

ASP.Net C# gridview

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.)

ASP.NET GridView binding sequence

I'm trying to bind my GridView at runtime, but I'm also trying to avoid running all the binding events twice.
I have a GridView that gets populated from a function that returns a DataTable. I'm not using ViewState in the grid for a couple of reasons. I seem to have a Catch-22 situation here:
If I don't bind the grid by Page_Load at the latest, the RowCommand and other grid events won't fire.
If I DO bind the grid in Page_Load, but I'm on a PostBack from a pager link, sort link, or search button, those event handlers will change the data and need to rebind it, running all the binding code again.
The grid triggers DataBound, RowDataBound, and RowCreated events, which could be performing expensive operations. I really hate to call them all in Page_Load, and then wipe out the data and call them all again if the data changes. But I can't seem to avoid this double duty, because in Page_Load I don't know if it was a grid event that will change the data, or a grid event that doesn't.
Any ideas?
Try the command arguments. If a button in the gridview was clicked, that event will be fired and you can handle it appropriately. Your question is not clear enough i'm afraid. Could you be more specific?
Check if request is a postback. Bind the datatable to the grid like so:
If(!ispostback)...
That way you wont be binding the table to the grid on each request.

Categories

Resources