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.
Related
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.
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.)
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.
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.
I have a user control that i've loaded in the itemdatabound event of a repeater and the control itself doesn't seem to want to load. The page life cylce seems to be the page that has the itemdatabound event then the user control so the control loads too late. I need to access properties from my control in the itemdatabound event.
Does anybody know how this can get done?
thanks!
Have you tried overriding OnInit on your page, and then databinding your repeater in there ?
Usually you want to add controls dynamically whilst in OnInit, since otherwise you're too late in the life cycle (as you point out yourself).
If you're already databinding in OnInit, it should work.
Preferably post your ItemDataBound event and where you databind the repeater (the code) :-)