This is a migrated thread and some comments may be shown as answers.

Custom CommandItemTemplate LinkButtons not triggering the OnCommand client event handler

4 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob T
Top achievements
Rank 1
Rob T asked on 28 Oct 2009, 11:05 PM
I have a grid that I've added a custom CommandItemTemplate to.  The template contains (among other things) a link button with the CommandName of 'InitInsert'.  This button is working great for the purposes of adding new records to the grid.

My problem is that when my Add LinkButton is clicked the client side OnCommand event is not being fired.

Ideas on howto fix this?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2009, 06:09 AM
Hi Rob,

Generally to fire client OnCommand, you must attach client script to your Button and explicitly fire the OnCommand event using firecommand() method.

aspx:
 
<CommandItemTemplate> 
    <asp:LinkButton ID="LinkButton1" OnClientClick="ButtonClicked();" 
        runat="server">LinkButton</asp:LinkButton> 
</CommandItemTemplate> 

javascript:
 
<script type="text/javascript"
    function ButtonClicked() { 
        var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView(); 
        masterTable.fireCommand("InitInsert"""); 
    } 
function RadGridCommand(sender, args) { 
        if (args.get_commandName() == "InitInsert") { 
            alert("Insert"); 
        }        
    } 

-Shinu.
0
Rob T
Top achievements
Rank 1
answered on 29 Oct 2009, 04:59 PM
As I am adding this to a number of grids I do not want to do it on a per instance basis, so your precise solution will not work.

When I try to add that behavior to the LinkButton inside the InstantiateIn method I run into a problem.

public void InstantiateIn(System.Web.UI.Control container) 
            _addButton = new LinkButton(); 
            _addButton.ID = "AddButton"
            _addButton.Text = "Add"
            _addButton.CommandName = "InitInsert"
            _addButton.OnClientClick = String.Format("if(!$find('{0}').fireCommand('{1}','{2}')) return false;", _addButton.ClientID, "InitInsert"string.Empty); 
 
            container.Controls.Add(_addButton); 

At this point in the code execution ClientID == "AddButton", not the eventual ClientID that the will actually be written to the page (something like ctl00_SomeForm_SomeGrid_ctl00_ctl03_ctl01_AddButton). So when the firecommand js gets called it looks for just ID AddButton and blows up on the null value.

It might be helpful to know that I add my custom template in the Grid's Init.  I thought custom templates needed to be added at Init.  Is there a better 'time' to add it?  Should I be adding the OnClientClick at a different time?

Thanks!
0
Rob T
Top achievements
Rank 1
answered on 29 Oct 2009, 09:04 PM
I am able to assign the proper ClientID by delaying the ClientClick assignment until the ItemCreated event.

        void MyGrid_ItemCreated(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridCommandItem) 
            { 
                LinkButton addButton = e.Item.FindControl("AddButton"as LinkButton; 
 
                if (addButton != null
                    addButton.OnClientClick = String.Format("if(!$find('{0}').fireCommand('{1}','{2}')) return false;", e.Item.OwnerID, "InitInsert"string.Empty); 
 
            } 
        } 


Thanks for your time.
0
Tracy Dryden
Top achievements
Rank 1
answered on 25 Sep 2012, 03:50 PM
If you add the item to the controls collection FIRST, then you will be able to get the correct ClientID. Since your _addButton variable still refers to the control, you are still able to set properties on it even after it has been added to the controls collection, but since it has been added, it's ClientID will be correct. I've used this successfully in other cases.

However, as you apparently discovered (since your eventual code uses the button's OwnerID), using the button's ClientID is incorrect in any case, it needs to be the id of the MasterTableView. But you could also have accessed that correctly after adding the button to the controls collection.
Tags
Grid
Asked by
Rob T
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Rob T
Top achievements
Rank 1
Tracy Dryden
Top achievements
Rank 1
Share this question
or