i’m having a form with a textbox and a button
the textbox has an onblur event to it which is set using jquery after page load.
the button has a click event that sets some text in the textbox. The issue is, whenever the event occurs, the onblur events goes away. How to resolve this issue.
This is just an example. I have several controls on the page that is set some value using jquery. All the values are lost after his dropdown ajax event.
this is a sample code that shows the issue
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
var ctrl_name = "#<%=name.ClientID %>";
$(function () {
$(ctrl_name).blur(
function () {
alert("this texbox has lost its focus");
}
);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="name" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Codebehind Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
name.Text = "testing Ajax";
}
}
}
The reason you are losing your values on the page is that your button_click is a server side method, which will always cause the page to reload.
You need to make the method callable from the client side for it to be used in ajax. It neeeds to be a WebMethod or a ScriptMethod.
I haven’t used this stuff recently so my syntax may not be 100% correxct, but hopefully this points you int the right direction.
your button click method needs to be in the form:
and you call it like this: