开发者

How to show javascript alertbox on button click event in asp.net webpage if textbox1.text=""?

开发者 https://www.devze.com 2023-01-31 23:27 出处:网络
How to show javascript alertbox on button click event in asp.net webpage if textbox1.text=\"开发者_如何学JAVA\" ?What you must do is attach a script to the button\'s client click event. In that script

How to show javascript alertbox on button click event in asp.net webpage if textbox1.text="开发者_如何学JAVA" ?


What you must do is attach a script to the button's client click event. In that script, check the value of textbox1. If it's empty, you show the alert and return false to prevent the form from submitting (prevent postback).

To do so, in Page_Load, insert the following:

if (!Page.IsPostBack)
{
    string script = string.Format("if (document.getElementById('{0}').value == '') {{ alert('Textbox is empty'); return false; }}", textbox1.ClientID);
    btnMyButton.Attributes.Add("onclick", script);
}

Of course, you could encapsulate the script in a reusable method, use jquery to check the value, but this is the big idea.


Are you talking about a server-side button click event? If so, then you can register javascript to run after the page is loaded, but you cannot execute javascript on the server in the context of the browser.


If you meant button click on client side you can use OnClientClick event of the asp.net Button Control. Check the link.

And if you simply want it for Validation purpose only then you can use RequiredFieldValidator and ValidationSummary Controls.

0

精彩评论

暂无评论...
验证码 换一张
取 消