[英]Do a database query on Textbox onblur event
I am using asp.net 3.5 with C#. 我正在使用带有C#的asp.net 3.5。 I need to do a database lookup when a user enters ProductID in txtProductID.
当用户在txtProductID中输入ProductID时,我需要进行数据库查找。 I guess doing javascript is out of the question since this will have to be server side call.
我想做javascript是不可能的,因为这将是服务器端调用。 I wrote this code in the page_load event of the webpage:
我在网页的page_load事件中写了这段代码:
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 ? 我收到一条错误消息:Microsoft JScript运行时错误:预期的对象如何解决此问题?
onblur
is a client-side event. onblur
是一个客户端事件。 LookupProduct
is a server-side method. LookupProduct
是一种服务器端方法。 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 . 对此没有快速解决方法 - 您必须在客户端事件上触发回发(使用
ClientScriptManager.GetPostBackEventReference
)或使用Microsoft ASP.NET Ajax等库实现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
. 或者,如果您不需要在每次模糊时触发此事件,并且仅在文本更改时 ,则可以使用服务器端
TextBox.OnChanged
事件并将TextBox的AutoPostBack
属性设置为true
。 Make sure you remember to set AutoPostBack
, otherwise this won't get you anywhere. 确保你记得设置
AutoPostBack
,否则这不会让你到任何地方。
Use the TextBox.TextChanged event. 使用TextBox.TextChanged事件。
ASPX markup: ASPX标记:
<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
}
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 这应该可行,如下所述: 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 这是Javascript
<script language="javascript">
function LookupProduct()
{
PageMethods.LookupProduct('',OnSuccess, OnFailure);
}
function OnSuccess(result) {
if (result)
{
}
}
function OnFailure(error) {
}
</script>
This is the server sidewebmethod 这是服务器sidewebmethod
[WebMethod]
public static bool LookupProduct()
{
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.