tags:

views:

659

answers:

3

I am using asp.net 3.5 with C#. I need to do a database lookup when a user enters ProductID in txtProductID. I guess doing javascript is out of the question since this will have to be server side call. I wrote this code in the page_load event of the webpage:

        protected void Page_Load(object sender, EventArgs e)
    {
        txtProductID.Attributes.Add("onblur", "LookupProduct()");
    }

        protected void LookupProduct()
    {
        //Lookup Product information on onBlur event;
    }

I get an error message: Microsoft JScript runtime error: Object expected How can I resolve this ?

+1  A: 

This should do the trick, as referenced here: http://www.codedigest.com/CodeDigest/80-Calling-a-Serverside-Method-from-JavaScript-in-ASP-Net-AJAX---PageMethods.aspx

These are the controls

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
<asp:TextBox ID="txtTest" onblur="LookupProduct()" runat="server" />

This is the Javascript

<script language="javascript">
function LookupProduct()
{  
    PageMethods.LookupProduct('',OnSuccess, OnFailure);
}

function OnSuccess(result) {
    if (result)
    {
    }
}

function OnFailure(error) {
}
</script>

This is the server sidewebmethod

[WebMethod]
public static bool LookupProduct()
{
    return true;
}
Dustin Laine
^^ while already using an UpdatePanel, which is a better way to achieve this behaviour, the onTextChanged event handler or your suggested PageMethods way?
Dienekes
It depends. Do you want a postback? If so then the `ontextchanged` is probably the way to go. I wrote my answer around his markup, while the @jrummel's answer has less code.
Dustin Laine
+1  A: 

Use the TextBox.TextChanged event.

ASPX markup:

<asp:TextBox ID="txtProductID" runat="server" AutoPostBack="true" OnTextChanged="txtProductID_TextChanged" />

Codebehind:

protected void txtProductID_TextChanged(object sender, EventArgs e)
{
   // do your database query here
}
jrummell
Note that this answer will cause PostBack while @durilai's will not.
jrummell
This code was easy and did the job. Thanks, @jrummell !
user279521
+2  A: 

onblur is a client-side event. LookupProduct is a server-side method. You can't reference one from the other - there's simply no association whatsoever between the two.

There's no quick fix for this - you have to either trigger a postback on the client event (using ClientScriptManager.GetPostBackEventReference) or implement an Ajax callback using a library like Microsoft ASP.NET Ajax.

Alternatively, if you don't really need to fire this event on every blur, and only when the text has changed, then you can simply use the server-side TextBox.OnChanged event and set the TextBox's AutoPostBack property to true. Make sure you remember to set AutoPostBack, otherwise this won't get you anywhere.

Aaronaught
The event needs to be fired on LostFocus / onBlur of the TextBox
user279521
@user279521: There is no server-side `LostFocus` event in ASP.NET, so if you must wire it up to the `onblur` event, then you need to construct the postback/callback as indicated in the second paragraph.
Aaronaught
@user279521. Look at my answer below which will allow you to call a server side method from any type of JavaScript event.
Dustin Laine
@durilai, why do you have a ScriptManager in your sample code below?
user279521