How to use on blur event using c#?

大城市里の小女人 提交于 2019-12-13 02:19:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!