Click here to Skip to main content
12,998,654 members (130,827 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 16 Jun 2004

Passing argument from the server side to a client-side JavaScript function

, 16 Jun 2004
Rate this:
Please Sign up or sign in to vote.
How to pass arguments from the server side to a client-side JavaScript function.


Let me explain this using a problem I have faced. I am using a database table from where I am getting the Item_id, Item_name, Item_price and Quantity values. I am populating Item_id and Item_name in a drop down list in a key value pair. Here, I am presenting an interface for adding an item to a DataGrid. Whenever the user presses Add button, I need to check whether that particular item is already present in the DataGrid or not. If yes, then I have to display a message: "item already present in the list .. do you want to add the quantity". On the basis of user response, I have to perform some action.

Now the main problem is when any body presses Add, I have to go back to the server side for checking the item status, and come back to the client side for displaying the JavaScript message, and user the presses Yes and again the control comes back to the server for adding the item to the DataGrid.

To solve this problem, I have used JavaScript. Every time when page loads, I am sending a string containing all the item_id present in the DataGrid, and when the user presses Add button, I am invoking the JavaScript function which check the selected item_id in the string. If the item_id is present, it displays a confirmation message: "item already present in the list .. do you want to add the quantity". If yes, then we need to go back to the server side for adding the quantity, otherwise we'll remain in the client side only. Using this technique, we can save the unnecessary expense of 1 trip to the server.

Screen 1

Screen 2

JavaScript function:

function getCofirmation(inArr)
    // Get the current selected value
    var val= document.form1.DropDownList1.value; 
    //split the string of item_id seperated by "/"
    var newarray= inArr.split("/"); var temp;

    for(var i= 0;  i <  newarray.length; i++)
        if (temp==val)
            return window.confirm("Record already Present" + 
                          " do you want to add the quantity ?");
    return false;

Server side code:

Dim val As String
Dim retArr As String

    Dim dtitems as datatable
    ' all selected item are presented in cache in the form of datatable
    dtitems = Cache.Item("dtItems")
    Dim ct As Integer = dtitems.Rows.Count
    Dim arr(ct) As String
    ' if currently no item is selected send the empty string
    If ct <= 0 Then
        'res is a hidden field where I am storing 
        'the user response(true/false) 
        'which I'll check in the server side 
        'for adding the record or not
        btnAddItem.Attributes.Add("OnClick", _
                 "form1.res.value = getCofirmation(new Array());")

        Exit Sub
    end if
    Dim counter As Int32 = 1

    Dim dbrow As DataRow
    For Each dbrow In dtitems.Rows
        arr(counter) = dbrow("item_id")
        counter = counter + 1
    ' create a string and seperate each item_if by "/" symbol
    For counter = 1 To ct
        retArr = retArr + arr(counter)
        If counter < ct Then
            retArr = retArr +  "/"
        end if

Catch e1 As Exception

End Try

Points of Interest

There may be some other better way of doing this thing .. but at that time, I found this best for saving the unnecessary post back time.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prakash Kalakoti
India India
Its me Smile | :)

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey16-Feb-12 18:58
membermanoj kumar choubey16-Feb-12 18:58 
Questionisn't more useful the showModalDialog function? Pin
johndoe249011-Nov-04 5:45
memberjohndoe249011-Nov-04 5:45 
GeneralClient Side Strategy Pin
bobgramcko28-Jun-04 14:07
memberbobgramcko28-Jun-04 14:07 
General&quot;/&quot; Delimeter Pin
Spiff Dog23-Jun-04 8:07
memberSpiff Dog23-Jun-04 8:07 
GeneralRe: &quot;/&quot; Delimeter Pin
Prakash Kalakoti23-Jun-04 18:35
memberPrakash Kalakoti23-Jun-04 18:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 17 Jun 2004
Article Copyright 2004 by Prakash Kalakoti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid