问题
I want to use On Blur event in my project. but it doesn't work. this is my code:
code c#:
protected void xx()
{
TextBox2.Text = "zzzzzzzzzzzzzzzzzzz";
}
code design:
<asp:TextBox ID="TextBox1" runat="server" onblur="xx"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
what is wrong?
by the way, I use visual studio 2010.
回答1:
**aspx code:**
<asp:TextBox ID="TextBox1" runat="server" onblur="validate();"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
**javascript-code:**
function validate()
{
var _textbox = $(this);
$.ajax({
url: "your page url here"(index.aspx) + "?FuncName=ValidateUserid&text="+_textbox.val(),
context: document.body
}).done(function() {
alert("you can do whatever you want here");
});
}
**aspx.cs code (server-side)**
protected void Page_Load()
{
if(Request.QueryString["FuncName"].Equal("ValidateUserid"))
{
string UserId = Request.QueryString["text"];
//Put your database logic here.
}
}
Dont forget to include your Jquery library in HEAD section of your ASPX page.
回答2:
try like this
the Code behind:
On page load event
txtAccountNumber.Attributes("onBlur") = "IsAccNumberValid(" & txtAccountNumber.ClientID & ")";
Where txtAccountNumber is the ID of the TextBox in the markup page and you pass the ClientID of the textbox because JavaScript is client side not server side. And now in the markup page(.aspx) have this javascript in the head section of the page:
<script type="text/javascript">
function IsAccNumberValid(txtAccountNumber) {
if (txtAccountNumber.value.length < 6) {
alert(txtAccountNumber.value);
}
}
</script>
来源:https://stackoverflow.com/questions/21365181/how-to-use-on-blur-event-using-c