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

Which cell being clicked

3 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mario Turcotte
Top achievements
Rank 1
Mario Turcotte asked on 12 Nov 2008, 09:00 PM
Hello all,

I have a simple grid with 4 columns, with EnablePostBackOnRowClick "on". I can retrieve which row has been selected but I want to be able to go deeper in the selection. The grid contains Project data and the columns are;

Column 1: Project Id + Project Description
Column 2: Director Id + Firstname Lastname
Column 3: Manager Id + Firstname Lastname
Column 4: Teamlead Id + Firstname Lastname

This is pretty simple and I think easy to visualize. What I need to achieve is to determine which cell has been clicked.

From the ItemCommand event of the grid, I can get all the values from the selected row with;

If e.CommandName = "RowClick" Then

However the purpose of that grid is to redirect the user to the appropriate page with the selected Id.  Right now I can concatenate all 4 values in the querystring but I need to know what cell has been clicked.

i.e. If the user click on a Director name, I have to load Director.aspx against the selected Id.
i.e. If the user click on a Team Lead name, I have to load Teamlead.aspx against the selected Id.
etc etc..

I need this done all on Server Side, is this possible?

I am using Q2 2008

Thanks in advance.

P.S. From the Search I found several thread about similar problem, none was providing what I need.
P.S.S. A solution has been suggested by a contributor to use the PreRender event and get the item.Selected; this always return the last column value.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Nov 2008, 11:04 AM
Hello Mario,

You can actually add an onClick attribute for the cell and then pass the cell value to a hidden field in the javascript function. Then redirect the page on row click command(with EnablePostBackOnRowClick="true") by passing the cell value stored in the hidden field to the url.
cs(the cell on click event):
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    {        
       foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
           foreach(GridColumn col in RadGrid1.Columns) 
            { 
                string cellText = dataItem[col.UniqueName].Text;               
                dataItem[col.UniqueName].Attributes.Add("OnClick", "return Click('" + cellText + "');"); 
             } 
         } 
    } 

aspx(hidden field):
<asp:HiddenField ID="HiddenField1" runat="server" /> 

js(pass the cell value as the hidden field value):
function Click(key) 
 {   
  var hiddenField=document.getElementById("HiddenField1"); 
  hiddenField.value=key;   
 } 

cs(Redirect on row click):
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick") 
        { 
            string strtxt=HiddenField1.Value; 
            Response.Redirect("Default.aspx?cell=" + strtxt); 
        } 
    } 

Thanks
Princy.
0
Daniel
Telerik team
answered on 13 Nov 2008, 03:56 PM
Hello Mario,

I suggest you try another approach:

<script type="text/javascript" language="javascript"
    function RowClick(sender, args) 
    { 
        var cellindex = args.get_domEvent().target.cellIndex 
        sender.get_masterTableView().fireCommand("CellClick", cellindex); 
    } 
</script> 

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == "CellClick"
        RadAjaxManager1.Alert("Cell " + e.CommandArgument); 

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mario Turcotte
Top achievements
Rank 1
answered on 14 Nov 2008, 03:46 PM
Thank you both for your reply.

I really like your suggestion including firecomand Daniel, and I gave it a try at first. Unfortunatly the Client side RowClick function is never triggered. Perhaps I need to add the attribute to the grid first.

I then look at Princy suggestion and it works perfectly.

Many many thanks for your help.

Cheer, Mario
Tags
Grid
Asked by
Mario Turcotte
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Daniel
Telerik team
Mario Turcotte
Top achievements
Rank 1
Share this question
or