Last post Apr 04, 2009 01:31 PM by Vautour
Feb 16, 2009 04:48 PM|PilSung|LINK
I am writing a custom web control that will show a list of users from the Membership API and allow the user to perform actions on those users in the list. One of the actions is to approve/disable users via a LinkButton in a TemplateField on the grid. I
wrote a template class with code in the InstantiateIn method as follows:
LinkButton approvedButton = new LinkButton();
approvedButton.CommandName = "ApprovedClick";
approvedButton.ID = "ApprovedLink";
approvedButton.DataBinding +=new EventHandler(item_DataBinding);
The event handler fires on data binding, calling code as follows...
LinkButton button = (LinkButton)sender;GridViewRow row = (GridViewRow)button.NamingContainer;object userName = DataBinder.Eval(row.DataItem, "UserName");object isApproved = DataBinder.Eval(row.DataItem, "IsApproved");button.CommandArgument = userName.ToString();button.Text = (bool)isApproved ? "Disable" : "Approve";
The grid then uses this column while being created via code in the OnInit of the control class. The specific code for this is:
//create the grid!
gridUsers.AutoGenerateColumns = false;
gridUsers.ID = "gridUsers";
gridUsers.RowCommand += new GridViewCommandEventHandler(gridUsers_RowCommand);
TemplateField approvedField = new TemplateField();
approvedField.ItemTemplate = new UserListTemplate("Approved");
And then there is an event for the RowCommand firing as follows...
protected void gridUsers_RowCommand(object sender, GridViewCommandEventArgs e)
MembershipUser user = Membership.GetUser((string)e.CommandArgument);
//approve/unapprove the selected user
user.IsApproved = !user.IsApproved;
BindUsers(); //this does the rebinding of the grid
I stepped through the code and the RowCommand event is never firing. I have been comparing this to an existing page where everything is in a standard aspx page where it works without a problem. Any reason why the event would not fire?
Thanks for any help.
Apr 04, 2009 01:31 PM|Vautour|LINK
I had a similar problem with a button inserted as a template field. By clicking on the button the user should get a detailed view of the data. For this the button's OnClick event should have fired. It didn't and the debugger never stopped at the event.
Now, since my GridView was filled dynamically I had to implement the GridView's Init-Event. So each time the init event was fired all dynamicly generated controls would be regenerated and the event was fired and catched thus executing the respective code.
Hope this helps. Gave me some headaches too (I didn't realize at first that the buttons where still created dynamically just because I didn't write the code myself instead letting the GridView do the work for me ;)).