GridButtonColumn CommandArgument from data source

18 posts, 0 answers
  1. Kevin Warnke
    Kevin Warnke avatar
    98 posts
    Member since:
    Dec 2005

    Posted 09 Apr 2009 Link to this post

    Is it possible to use markup to set a value for the CommandArgument property of the GridButtonColumn from the DataSource?

    I'm sure I could set this in a DataBinding-related event but would like an easy way to do this in markup.  Something like a "DataCommandArgumentField", etc...

    Thanks,
    Kevin
  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 13 Apr 2009 Link to this post

    Hello Kevin Warnke,

    The GridButtonColumn is not a binding container and hence no binding expressions can be set for it.

    Best Regards,
    Tsvetoslav
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kevin Warnke
    Kevin Warnke avatar
    98 posts
    Member since:
    Dec 2005

    Posted 13 Apr 2009 Link to this post

    Why isn't it a binding container?  ;o)

    Given that buttons are usually meant to act on something and the fact that many times the DataItem hasn't been set when the click event fires what is the recommended way to get "what record you're on" to the button's event?

    thanks,
    Kevin
  5. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 14 Apr 2009 Link to this post

    Hi Kevin Warnke,

    Indeed, the data item is not set as early as the ItemCommand event of the grid, however, you can set a command name to the button column, intercept the item command event and check for the command name, get the index of the row that is raising the event and use it in the ItemDataBound event as per  your scenario.

    I hope this helps.

    Best Regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Kevin Warnke
    Kevin Warnke avatar
    98 posts
    Member since:
    Dec 2005

    Posted 14 Apr 2009 Link to this post

    This is my whole point :)

    It shouldn't be that complex to use the GridButtonColumn to fire an event and know what the data is behind the event.

    Thanks,
    Kevin
  7. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 16 Apr 2009 Link to this post

    Hi Kevin Warnke,

    Still this is the correct way to achieve this scenario and I believe it is not that difficult but simply reflecting the asp.net event cycle pecularities.

    Thank you.

    Best regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. sculver
    sculver avatar
    3 posts
    Member since:
    Aug 2006

    Posted 12 May 2009 Link to this post

    I too was looking for a solution to this issue, and came to this conclusion, but as an alternative for others out there.  If you wish to not worry with tying manually into events on the grid, you can create a Template column instead.  Then you simply create a standard Button (or Image Button or Hyperlink, as you see fit) and most of the properties are the same.
    This allows you to then bind or eval as needed to any property on the button you created, bypassing the issue described in this forum.
  9. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 13 May 2009 Link to this post

    Hi Stephen,

    Thank you for the suggestion - indeed this is an alternative and fully viable way to get the desired functionality discussed in this forum thread.

    Best Regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2008

    Posted 18 Sep 2009 Link to this post

    Is there/can anyone point me to an example of how either of the methods in this thread are done.  I want to have a Grid with a hyperlink column or button column that opens a modal window for editing the record - or should i be using a different approach altogether?

    Thanks!

    Scott
  11. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 18 Sep 2009 Link to this post

    Hi Scott,

    I have achieved this functionality in my RadGrid using the approach of adding a GridTemplateColumn and placing a LinkButton inside of the ItemTemplate. You could call the function to open your modal window with the OnClientClick event of the control you place in the ItemTemplate. If you want to use a method in your code behind you could assign a value to the CommandName property of the LinkButton and check the e.CommandName in the ItemCommand event of the grid (also below).

    Client Side:
    <telerik:GridTemplateColumn> 
             <ItemTemplate> 
                   <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="ClientClick()">Edit  
                   </asp:LinkButton> 
             </ItemTemplate> 
    </telerik:GridTemplateColumn> 

    Sever Side:
    <telerik:GridTemplateColumn>    
             <ItemTemplate>    
                   <asp:LinkButton ID="LinkButton1" runat="server" CommandName="OpenWin">Edit     
                   </asp:LinkButton>    
             </ItemTemplate>    
    </telerik:GridTemplateColumn>    
     
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
        {  
           if (e.CommandName == "OpenWin")  
              {  
                //Your code here.  
              }  
        } 

    I hope this helps!
    Casey
  12. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2008

    Posted 18 Sep 2009 Link to this post

    Thanks so much Casey!  I guess I am a beginner in some ways (plus I use VB) - I can't get this to work - is there any way you can send me a complete sample page(s)?

    If so thanks a million, if not, thanks so much again for the tip, I'm sure I'll get it sooner!

    Scott
  13. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 21 Sep 2009 Link to this post

    Hi Scott,

    I used Telerik's code converter to convert from C# to VB. Hopefully this will help you.

    Casey

    Protected Sub RadGrid1_ItemCommand(source As Object, e As GridCommandEventArgs)  
    If e.CommandName = "OpenWin" Then 
        End If 
    End Sub 

    Your grid will look something like this (with your settings also):

    <telerik:RadGrid id="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" >  
      <telerik:GridTemplateColumn>       
             <ItemTemplate>       
                   <asp:LinkButton ID="LinkButton1" runat="server"   
                   CommandName="OpenWin">Edit</asp:LinkButton>       
             </ItemTemplate>       
      </telerik:GridTemplateColumn>     
    </telerik:RadGrid> 
  14. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2008

    Posted 21 Sep 2009 Link to this post

    Thanks again Casey!  I mostly got it working (in both C# and VB - thanks for the headsup about the converter, and for doing the simple conversion for me).

    However, the behavior I get now is, I click the LinkButton column that I created using a TemplateColumn, and my function to open the Modal window gets called, but then the entire page refreshes, so the Modal window closes by itself.  At first I thought it wasn't working, but when I added some Alerts it slowed it stopped it so I could see that the Modal RadWindow was indeed opening.

    You can observe this at http:www.sundry.com/adb_web03/default6.aspx

    Here is my Default6.aspx page:
    <%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default6.aspx.vb" Inherits="Default6" %> 
     
    <asp:Content ID="Content1" ContentPlaceHolderID="cphHeader" Runat="Server">  
    </asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="cphMainContent" Runat="Server">  
     
     
    <script type="text/javascript">  
     
        function OpenNewWindow(theUrl)  
        {  
         var oManager = GetRadWindowManager();  
                alert(oManager.get_id());  
          var oWnd = oManager.getWindowByName('rw1');  
                alert(oWnd.get_id());  
          if (oWnd)  
          {  
              oWnd.setUrl(theUrl);  
              oWnd.show();  
          }  
      }  
     
    </script> 
     
        Page 6  
    <br /> 
    <br /> 
     
        <telerik:RadGrid ID="RadGrid1" runat="server"  Height="300px" Width="500px" Skin="Telerik"   
            AutoGenerateColumns="False" GridLines="None"   
        DataSourceID="ObjectDataSource1">  
            <MasterTableView DataSourceID="ObjectDataSource1">  
                <Columns> 
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn">   
                         <ItemTemplate>    
                               <asp:LinkButton ID="LinkButton1" runat="server"   OnClientClick="OpenNewWindow('SubWin1.aspx')">Edit     
                               </asp:LinkButton>    
                         </ItemTemplate>    
                    </telerik:GridTemplateColumn>   
                    <telerik:GridHyperLinkColumn UniqueName="column2" DataNavigateUrlFields="LogID, IPAddress" DataNavigateUrlFormatString="Default7?LogID={0}" DataTextField="IPAddress" NavigateUrl="Default7.aspx"   
                        Text="Details">  
                    </telerik:GridHyperLinkColumn>   
                    <telerik:GridBoundColumn DataField="mOS" HeaderText="OS" UniqueName="column2">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="IPUsers" HeaderText="IPUsers"   
                        UniqueName="column3">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Hits" HeaderText="Hits"   
                        UniqueName="column4">  
                    </telerik:GridBoundColumn>   
     
     
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid><br /> 
     
     
        <input id="Button1" type="button" value="Open Modal" onclick="OpenNewWindow('SubWin1.aspx');" /> 
     
    <br /> 
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"   
            SelectMethod="GetOS"   
            TypeName="MembershipManagement.MembershipManagagement.ABDMember"></asp:ObjectDataSource> 
     
    </asp:Content> 
    <asp:Content ID="Content3" ContentPlaceHolderID="cphFooter" Runat="Server">  
    </asp:Content> 
     
     

    FYI, as you can see, this is a Content Page.  On my Master Page I have a RadWindow Manager, with like 6 RadWindows to it (rw1 through rw6).  Also, the button (not in Grid) at the bottom calls the same script to open the RadWindow, and it works and the Modal window does not auto-close.

    So, I have 2 questions (for Casey or anyone):

    1.  Any idea why the whole page (default6.aspx) is refreshing and the just opened Modal RadWindow automatically closes when I click on the LinkButton?

    2.  How can I combine this with the functionality where I pass the record ID (of the row clicked on the grid) to the RadWindow I am opening.  For example, in other places I have a grid of data with a hyperlink column, and I use the following pair of properties to auto-specify a URL QueryString so I can drill down to a specific record once I get the the new (Modal) window? 
    These properties do not exist on the LinkButton like they do on a HyperlinkColumn.

    DataNavigateUrlFormatString

     

    ="IPDetails.aspx?LogID={0}&IPAddress={1}" DataTextField="IPAddress"

     



    Thanks!

    Scott
  15. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2008

    Posted 21 Sep 2009 Link to this post

    By the way, the GridHyperlinkColumn that is in my code block in my just posted Reply is not actually in my page (I had pasted it there momentarily), and is not in the published version that I gave the web address to either (so it's not the issue).


    Scott

  16. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 23 Sep 2009 Link to this post

    Hi Scott,

    You just need to prevent the post back by returning false from your javascript method.
    In addition you might be interested in reviewing the following online example:
    http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandwindow/defaultcs.aspx?product=window

    Best regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  17. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2008

    Posted 24 Sep 2009 Link to this post

    Tsvetoslav/Casey

    Adding the "return false;" did not do the trick, however, using the demo you sent a link to, as well as other forum threads/example,, I finally got Add/Edit on modal pop-ups from Grid, with close and AJAX rebind working.  Thank you both!!!



    Scott
  18. Nagendra
    Nagendra avatar
    5 posts
    Member since:
    Mar 2013

    Posted 12 Mar 2013 Link to this post

    Hi,
    I need help from u,
    I have a gridbuttoncolumn(linkbutton),i need to open a window on oncommand,and i am opening successfully but original page is refreshing when oncommand(client events) fires..please help me how to avoid page refresh on oncommand of gridbuttoncolumn(linkbutton).
  19. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 15 Mar 2013 Link to this post

    Hello Kevin,

    If you mean the OnCommand of the grid, just use:  
    function OnCommandHandler(sender, args)
    {
        args.set_cancel(true);
    }

    If you mean a custom handler of yours, just pass to it the event object, e.g. OnClientClick = "onCommand(event)", and use the following code:

    function onCommand(e)
    {
         var evt = e? e: window.event;
     
         evt.returnValue = false;
         evt.cancelBubble = false;
     
         if (evt.preventDefault)
        {
               evt.preventDefault();
               evt.stopPropagation();
         }
     
         return false;
    }

    Hope it helps.

    Greetings, Tsvetoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017