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

Passing Extra Parameters to Web Method from ClientSettings DataBinding

6 Answers 745 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sypher
Top achievements
Rank 1
Sypher asked on 24 Mar 2009, 05:36 PM
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. :-)

6 Answers, 1 is accepted

Sort by
0
Sypher
Top achievements
Rank 1
answered on 24 Mar 2009, 05:42 PM
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.
0
Sypher
Top achievements
Rank 1
answered on 24 Mar 2009, 10:48 PM
No examples out there?
0
Sypher
Top achievements
Rank 1
answered on 26 Mar 2009, 04:55 AM
Okay, I figured it out.  I'll post an example after I get some sleep. :-)
0
Sypher
Top achievements
Rank 1
answered on 26 Mar 2009, 04:18 PM
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; 
 
        } 
 
    } 
 



0
Radovan Radic
Top achievements
Rank 1
answered on 02 Oct 2009, 02:12 PM
0
Nicholas
Top achievements
Rank 1
answered on 01 Apr 2011, 03:05 AM
Very helpful!! Thanks for posting!
Tags
Grid
Asked by
Sypher
Top achievements
Rank 1
Answers by
Sypher
Top achievements
Rank 1
Radovan Radic
Top achievements
Rank 1
Nicholas
Top achievements
Rank 1
Share this question
or