Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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++)
    {
        temp=newarray[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

Try
    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
    Next
    retArr=CHR(34)
    ' 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
    Next
    RETARR= RETARR + CHR(34)

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.

License

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

About the Author

Prakash Kalakoti
Technical Lead
India India
Its me Smile | :)

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Feb-12 18:58 
Questionisn't more useful the showModalDialog function? Pinmemberjohndoe249011-Nov-04 5:45 
GeneralClient Side Strategy Pinmemberbobgramcko28-Jun-04 14:07 
General&quot;/&quot; Delimeter PinmemberSpiff Dog23-Jun-04 8:07 
GeneralRe: &quot;/&quot; Delimeter PinmemberPrakash Kalakoti23-Jun-04 18:35 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 17 Jun 2004
Article Copyright 2004 by Prakash Kalakoti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid