Passing Extra Parameters to Web Method from ClientSettings DataBinding

7 posts, 0 answers
  1. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 24 Mar 2009 Link to this post

    Is it possible to pass parameters other than row index, max rows, filter, and sort to the Web Method?  I can't seem to find any examples.  I have a RadGrid successfully using a Web Service to do client-side data binding, but I need it to pass in some extra parameters.

    I'm thinking that I *might* be able to do this by passing the other parameters as part of the filter parameters and then handling that in the Web Service, but that just seems like a hack. :-)
  2. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 24 Mar 2009 Link to this post

    Oh, I think I've seen someone using Session variables, but that won't work for me.  I need to find a way to have multiple copies of the same grid (on a user control) on the same page with different parameters.  If I had the parameters in Session variables, I would still need to pass in which grid I was binding somehow.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 24 Mar 2009 Link to this post

    No examples out there?
  5. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 25 Mar 2009 Link to this post

    Okay, I figured it out.  I'll post an example after I get some sleep. :-)
  6. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 26 Mar 2009 Link to this post

    So, I found the exact same question I had in another post over here, and a link to the code example that was supposed to help with the problem over here, but the code example just seemed to confuse me more.  So here's what I had to do:

    Problem
    Use client-side databinding on a RadGrid to get data from a Web method that requires extra parameters.  The same RadGrid can appear on a page multiple times with different parameters set, so the javascript associated with it cannot use in-line server code tags <% %>.  Also, since we are still using Visual Studio 2005 without LINQ, set the Web Method up so that it uses a SqlConnection and a non-shorthand version of a data class.

    Solution
    I added the extra parameters as hidden inputs in the grid's command item template.  Then, on the client-side databinding event, I got the value of that input and added it to the method arguments for the Web method.  In the Web method, I added the extra parameter and was then able to pass it as a parameter to a SQL Server stored procedure.

    The example also shows how to use Session variables in the Web method.  It works for any parameters that are the same across all instances of the grid in the same session.

    RadGrid Code
    <Telerik:RadGrid runat="server" ID="g" EnableLinqExpressions="false" OnItemCreated="g_ItemCreated"
        <ClientSettings> 
            <ClientEvents OnDataBinding="MetricDataBinding" /> 
            <DataBinding Location="~/ws/UI.asmx" 
                SelectMethod="GetMetrics" 
                SortParameterType="List" 
                FilterParameterType="List" 
                EnableCaching="false" /> 
        </ClientSettings> 
        <MasterTableView  
            ClientDataKeyNames="ID" 
            AutoGenerateColumns="false" 
            ShowHeadersWhenNoRecords="true">         
            <CommandItemTemplate> 
                <asp:HiddenField ID="a" runat="server" /> 
                [ ... The rest of the command template ... ] 
            </CommandItemTemplate> 
            <Columns> 
                [ ... Nothing special in here, just normal columns ... ] 
            </Columns> 
        </MasterTableView> 
    </Telerik:RadGrid> 
     

    The value gets set in the grid when the command item template is created...

            protected void g_ItemCreated(object sender, GridItemEventArgs e) { 
     
                if (e.Item.ItemType == GridItemType.CommandItem) { 
     
                    HiddenField a = (HiddenField)e.Item.FindControl("a"); 
                    a.Value = PageTypeID.ToString(); 
                 
                } 
     
            } 
     


    Client-side script (from a separate .js file)
    function MetricDataBinding(sender, args) 
        try { 
     
            // Add a method argument 
            // Passed in as the value of a hidden input on the grid 
            var t = sender.get_element(); 
            var v = t.getElementsByTagName('input')[0].value; 
            var methodArguments = args.get_methodArguments(); 
            methodArguments.AncestorID = v; 
            args.set_methodArguments(methodArguments); 
         
        } 
        catch (e) { alert(e.message); } 
     
     

    Web Method in a Web Service file
        public class MetricData { 
     
            private Guid _ID; 
            private int _Rank; 
            /* etc... */ 
     
            public Guid ID { get { return _ID; } set { _ID = value; } } 
            public int Rank { get { return _Rank; } set { _Rank = value; } } 
            /* etc... */ 
        } 
     
        [WebService(Namespace = "http://ourdomain.local/ws/ui", Name = "UI Web Services")] 
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
        [System.Web.Script.Services.ScriptService] 
        [ToolboxItem(false)] 
        public class UI : System.Web.Services.WebService { 
     
            [WebMethod(EnableSession = true)] 
            public List<MetricData> GetMetrics(int startRowIndex, int maximumRows, 
                List<GridFilterExpression> filterExpression, List<GridSortExpression> sortExpression, string AncestorID) { 
     
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[sConnectionName].ConnectionString); 
                SqlCommand cmd = new SqlCommand(); 
                DataSet ds = new DataSet(); 
                string token = Session.SessionID; 
                 
                cmd.CommandText = "GetMetrics"
                cmd.CommandType = CommandType.StoredProcedure; 
                cmd.Parameters.AddWithValue("@AncestorID", AncestorID); 
                cmd.Parameters.AddWithValue("@Session", token); 
     
                cmd.Connection = conn; 
     
                conn.Open(); 
                SqlDataAdapter adapter = new SqlDataAdapter(); 
     
                adapter.SelectCommand = cmd; 
                adapter.Fill(ds); 
                conn.Close(); 
     
                List<MetricData> l = new List<MetricData>();             
     
                int i = ds.Tables[0].Rows.Count; 
                foreach (DataRow r in ds.Tables[0].Rows) { 
     
                    MetricData d = new MetricData(); 
     
                    d.ID = new Guid(Convert.ToString(r["ID"])); 
                    d.Rank = Convert.ToInt32(r["Rank"]); 
                    /* etc... */ 
     
                    l.Add(d); 
     
                } 
     
                return l; 
     
            } 
     
        } 
     



  7. Radovan Radic
    Radovan Radic avatar
    7 posts
    Member since:
    Sep 2009

    Posted 02 Oct 2009 Link to this post

  8. Nicholas
    Nicholas avatar
    11 posts
    Member since:
    Jan 2011

    Posted 31 Mar 2011 Link to this post

    Very helpful!! Thanks for posting!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017