CommandArgument

9 posts, 0 answers
  1. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 28 Oct 2008 Link to this post

    How can I set the commandArgument for rows in the codebehind ?
    Ive tried this, but I get an error

    <telerik:GridButtonColumn CommandName="Select"  Text="Select" CommandArgument'<%# Eval ( "UserID" ) %>' 
                                                UniqueName="SelectButton">  
                                            </telerik:GridButtonColumn> 

    'Error Creting Control'
     Databinding Expressions are only supported on objects that have a databinding event.

    I need to gain access to the underlying datakey so that when a user selects a row I have some code that select data from a database using the datakey as the primary key into the table.  In a standard .net grid Id do this and it works

    protected void GridViewArtistName_RowCommand(object sender, GridViewCommandEventArgs e)  
            {  
                if (e.CommandName == "Select")  
                {  
     
                    Guid guid = new Guid(e.CommandArgument.ToString());  
                    Session.Add("USERID", e.CommandArgument.ToString());  
     
                    if (UserIDChange != null)  
                    {  
                        // Call the Event  
                        UserIDEventArgs E = new UserIDEventArgs(guid);  
                        OnUserId(E);  
                    }  
                }  
            } 
     How can I achieve the same functionality with a telerik grid ?
  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 28 Oct 2008 Link to this post

    Hello Mark,

    Try this approach:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        GridDataItem item = e.Item as GridDataItem; 
        if (item != null
            (item["SelectButton"].Controls[0] as LinkButton).CommandArgument = "myArgument"//Change the button type if appropriate 

    Greetings,
    Daniel
    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. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2008 Link to this post

    Hello Mark,

    You can also set the PrimaryKeyValue field as the DataKeyName for the MasterTableView and then it in the ItemCommand as shown below:
    aspx:
     <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None" OnItemCommand="RadGrid1_ItemCommand" > 
       <MasterTableView DataKeyNames="UserID" DataSourceID="SqlDataSource1"  > 
           ...        

    cs:
     protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if(e.CommandName=="Select") 
            { 
                string strtxt = (e.Item as GridDataItem).GetDataKeyValue("UserID").ToString(); 
                
            } 
        } 

    Thanks
    Princy.
  5. Suraj Shrestha
    Suraj Shrestha avatar
    7 posts
    Member since:
    Sep 2012

    Posted 07 Sep 2012 Link to this post

    hello princy, your idea does not work.
    have you tried that before posting.
  6. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 07 Sep 2012 Link to this post

    Hi Suraj,

    Could you please post your radgrid definition, and the radgrids ItemCommand even from your code behind? It would help to determine why Princy's solution isn't working for you. 

    Thanks!
    Casey
  7. Suraj Shrestha
    Suraj Shrestha avatar
    7 posts
    Member since:
    Sep 2012

    Posted 07 Sep 2012 Link to this post

    It seems its my mistake.
    I had used ClientEvent OnCommand as well as Server event OnItemCommand.
    So, I was getting datakey of first row only.

    Is there a way to get datakey of clicked row while using both events mentioned above.

    <telerik:RadGrid ID="rgDefaultOfferPriority" runat="server" AllowPaging="true" PageSize="50"
                                    AllowFilteringByColumn="true" AllowSorting="true" Skin="Windows7" OnPageIndexChanged="RgDefaultOfferPriority_PageIndexChanged"
                                    EnableAjaxSkinRendering="true" OnNeedDataSource="RgDefaultOfferPriority_NeedDataSource"
                                    OnItemCommand="RgDefaultOfferPriority_ItemCommand">
                                    <GroupingSettings CaseSensitive="false" />
                                    <FilterMenu OnClientShown="MenuShowing" />
                                    <ClientSettings EnableRowHoverStyle="true">
                                        <Selecting AllowRowSelect="true" />
                                        <ClientEvents OnFilterMenuShowing="filterMenuShowing" OnCommand="RgDefaultOfferPriority_ClientCommand" />

    protected void RgDefaultOfferPriority_ItemCommand(object source, GridCommandEventArgs e)
    {
        try
        {
            if (e.Item is GridDataItem)
            {                   
                long defaultOfferID = (long)((GridDataItem)e.Item).GetDataKeyValue("ID");
                
  8. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 07 Sep 2012 Link to this post

    Suraj,

    You should check to verify that the CommandName is the proper command in the ItemCommand server side event. Could you please post your clientside ItemCommand event as well? 

    I hope this helps!
    Casey

    protected void RgDefaultOfferPriority_ItemCommand(object source, GridCommandEventArgs e)
    {
     
    if (e.CommandName == "YourCommandName")
    {
        try
        {
            if (e.Item is GridDataItem)
            {                  
                  GridDataItem dataItem = (GridDataItem)e.Item;
                  //This expects you to have a column with the UniqueName set to "ID" in the RadGrid definition
                  Int64 defaultOfferId = Convert.ToInt64(dataItem["ID"].Text);
            }
          }
         catch (Exception ex)
         {
              String s = ex.message;
         }
    }
    }

  9. Suraj Shrestha
    Suraj Shrestha avatar
    7 posts
    Member since:
    Sep 2012

    Posted 07 Sep 2012 Link to this post

    CommandName check is irrevelant of which command I use. 
    Anyway, I have done command name check later.

    My clientside Command is as below.
    It does not matter what you have inside JavaScript. 
    After this event completion, when control reaches to server, It is getting first row of the grid.

    function RgDefaultOfferPriority_ClientCommand(sender, eventArgs) {
     
    }
  10. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 10 Sep 2012 Link to this post

    Suraj,

    How are you firing the command from the RadGrid? Could you please post your full RadGrid definition? 

    Thanks!
    Casey
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017