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

CommandArgument

7 Answers 1706 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mww
Top achievements
Rank 1
mww asked on 28 Oct 2008, 11:15 AM
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 ?

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 28 Oct 2008, 12:04 PM
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.
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2008, 12:19 PM
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.
0
Suraj Shrestha
Top achievements
Rank 1
answered on 07 Sep 2012, 04:30 PM
hello princy, your idea does not work.
have you tried that before posting.
0
Casey
Top achievements
Rank 1
answered on 07 Sep 2012, 04:51 PM
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
0
Suraj Shrestha
Top achievements
Rank 1
answered on 07 Sep 2012, 07:03 PM
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");
            
0
Casey
Top achievements
Rank 1
answered on 07 Sep 2012, 07:20 PM
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;
     }
}
}

0
Suraj Shrestha
Top achievements
Rank 1
answered on 07 Sep 2012, 07:30 PM
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) {
 
}
Casey
Top achievements
Rank 1
commented on 10 Sep 2012, 01:59 PM

Suraj,

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

Thanks!
Casey
Tags
Grid
Asked by
mww
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Princy
Top achievements
Rank 2
Suraj Shrestha
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Share this question
or