Need to get value of a cell - but pass it to a C# function. Possible from js?

6 posts, 0 answers
  1. G S S
    G S S avatar
    85 posts
    Member since:
    Mar 2008

    Posted 06 Jun 2009 Link to this post

    Hi,

    I am developing an Mp3 Player for my site, using telerik rad ajax controls. A requirement I have is to bind the song titles and their paths (They are dictionary <TKey, TString> collection). However, I need to get the clicked row's song path title to pass to the mp3 player code to then play that song.

    I have used the API quite heavily and I know how to do this (get value of a cell on clicking a row), but with the client side API, I can't pass the value of a cell to a C# function - or can I?

    Thanks
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2009 Link to this post

    Hi,

    You may access the clicked row's cell value on the client and then pass it to the server side in the RaisePostBackEvent event as shown below.

    ASPX:
     
    <script type="text/javascript">  
     function  RowClick(sender, eventArgs) 
     { 
       var grid = sender
       var MasterTable = grid.get_masterTableView(); 
       var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()]; 
       //to access the cell value on the client 
       var cell = MasterTable.getCellByColumnUniqueName(row, "ProductName"); 
       
       //pass the cell value to the server side 
       __doPostBack("<%= RadGrid1.UniqueID %>", "CellValue:"  + cell.innerHTML); 
       
     } 
     
    </script> 

    CS:
     
     protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument) 
        { 
            base.RaisePostBackEvent(source, eventArgument); 
     
            if (source == RadGrid1 && eventArgument.IndexOf("CellValue") != -1) 
            { 
                string cellvalue = eventArgument.Split(':')[1].ToString(); 
                // you can pass the cell value to desired function 
            } 
        } 


    Thanks
    Shinu



  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. G S S
    G S S avatar
    85 posts
    Member since:
    Mar 2008

    Posted 08 Jun 2009 Link to this post

    Hi,

    Thanks for that. I'm still confused how to use that code though.

    I will call the javascript function in the onrowclick event (in clientSettings) and write that C# code in the codebehind (obviously). So when that javascript function's __doPostBack line is triggered, that will cause a postback (I've noticed that ASP.NET writes this code in my markup too), and will the C# event then automatically execute?

    Also, if there is a postback setting in the grid, will this have to be on? What if I want to do this partially with no postback (AJAX style)?


    Thanks
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Jun 2009 Link to this post

    Hello,

    You can try using the AjaxManager and pass the cell value in its AjaxRequest event which is automatically fired as shown below. When using this method you need not enable any postback setting in the grid.
    aspx:
     <telerik:RadAjaxManager  ID="RadAjaxManager1" EnableAJAX="true" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">      
            <AjaxSettings>        
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1"
            <UpdatedControls> 
            <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
            </UpdatedControls> 
            </telerik:AjaxSetting> 
            </AjaxSettings> 
     </telerik:RadAjaxManager> 

    js:
    function  RowClick(sender, eventArgs)  
     {   
       var grid = sender;  
       var MasterTable = grid.get_masterTableView();  
       var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()];    
       var cell = MasterTable.getCellByColumnUniqueName(row, "ColumnUniqueName"); 
        
       $find("<%=RadAjaxManager1.ClientID%>").ajaxRequest(cell.innerHTML);     
     }  

    c#:
    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e) 
        { 
            
        } 

    Thanks
    Princy.
  6. G S S
    G S S avatar
    85 posts
    Member since:
    Mar 2008

    Posted 10 Jun 2009 Link to this post

    Princy,

    Everytime the js function executes, does the c# function then execute? Would I cast "e" (of its ajaxrequesteventargs type) to a string to get the relevant cell name?

    Thanks
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Jun 2009 Link to this post

    Hello,

    On calling the ajaxRequest client method, the server side event is triggered immediately. Thereby, to access the cell value on the server side, you can try the following code:
    c#:
    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e) 
        { 
            string strtxt = e.Argument; 
        } 

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