最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How to pass a value into a javascript function within an aspx page onClientClick - Stack Overflow

programmeradmin0浏览0评论

I want to check the textbox value before postback occurs. I set the onClientClick value to my function but I don't know how to pass the data to check, in this case I want to check the txt1 entered text.

<asp:textbox id="txt1" runat="server />

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                onclick="Save"
                OnClientClick="javascript:check(WANT TO PUT TEXTBOX VALUE HERE);"
                />

My javascript:

function check(txt) {
 var pattern = /^[0-9]{1,11}(,[0-9]{0,2})?$/;
 if (!pattern.test(txt)) {
    return false;
 }
 else {
    return true;
 }
}

The issue is that this check function is used along the keypress event of the txt1 so I cant use:

function check(){
    var txt=$('#txt1').val();
}

The whole JS code:

$('#txt1').keypress(function (e) {
    var key = String.fromCharCode(e.which);
    var txt = $(this).val() + key;
    if (check(txt)) {
        // do sth
    } else {
        e.preventDefault();
        // do sth
    }
});

How can I tell the OnclientCLick function to get the txt1 value?

Thank you.

I want to check the textbox value before postback occurs. I set the onClientClick value to my function but I don't know how to pass the data to check, in this case I want to check the txt1 entered text.

<asp:textbox id="txt1" runat="server />

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                onclick="Save"
                OnClientClick="javascript:check(WANT TO PUT TEXTBOX VALUE HERE);"
                />

My javascript:

function check(txt) {
 var pattern = /^[0-9]{1,11}(,[0-9]{0,2})?$/;
 if (!pattern.test(txt)) {
    return false;
 }
 else {
    return true;
 }
}

The issue is that this check function is used along the keypress event of the txt1 so I cant use:

function check(){
    var txt=$('#txt1').val();
}

The whole JS code:

$('#txt1').keypress(function (e) {
    var key = String.fromCharCode(e.which);
    var txt = $(this).val() + key;
    if (check(txt)) {
        // do sth
    } else {
        e.preventDefault();
        // do sth
    }
});

How can I tell the OnclientCLick function to get the txt1 value?

Thank you.

Share Improve this question edited Nov 21, 2012 at 15:27 anmarti asked Nov 21, 2012 at 15:10 anmartianmarti 5,14310 gold badges60 silver badges96 bronze badges 2
  • 1 OnClientClick="javascript:... <-- no need to specify the language. It can only be JS. – Diodeus - James MacFarlane Commented Nov 21, 2012 at 15:15
  • It does not meddles with my problem. No matters if javascript: is present or not – anmarti Commented Nov 21, 2012 at 15:22
Add a comment  | 

3 Answers 3

Reset to default 9

Something like that :

<asp:textbox id="txt1" runat="server />

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                onclick="Save"
                OnClientClick="check(document.getElementById('<%=txt1.ClientID%>').value;)"
                />

Edit:
Other methods :

  1. You can add it on server side : LinkButton1.OnClientClick="check(document.getElementById('" + txt1.ClientID + "').value;)";

  2. If you want to remain in the aspx page, you have to put a name of a javascript function in the OnClientClick and implement the javascript function in a script tag :

    <asp:LinkButton ID="LinkButton1"
                    runat="server"
                    Text="Save" 
                    OnClientClick="validate();"
                    />
    <script type="text/javascript">
        function validate() {
            alert(document.getElementById('<%=txt1.ClientID%>').value);
            return false;
        }
    </script>
    

You have several approaches.

First Approach:

function sayHello(obj){
   alert(obj.id + " says 'Hello!'");
};

<asp:Button runat="server" ID="btnApproach1" OnClientClick="sayHello(this)"
 Text="Say Hello" />

This will pass the Button control (actually, just an <input element) to the function and you can then work on it however you please.

Second Approach:

function checkValue(e) {

    //If it has a target, use it. Otherwise, use srcElement (for IE)
    var ctrl = e.target || e.srcElement;

    //Do whatever you need to with the text, no button necessary.

};

function bindEvent(ctrl, event, handler, propagates) {
    if (ctrl.addEventListener) {
        ctrl.addEventListener(event, handler, propagates);
    }
    else {
        ctrl.attachEvent("on" + event, handler);
    }
};

window.onload = function () {

var myCtrl = document.getElementById('<%=txtToValidate.ClientID%>');

bindEvent(myCtrl, "keydown", checkValue, false);

};

<asp:Button runat="server" ID="btnApproach2" Text="Say Hello" />

Here, you are binding the events to the controls behind the scenes (preferable) and removing that logic from your presentation layer (your .aspx).

Both approaches will work, but I'd suggest going with option two for separation of concerns.

You simply have to add single quote to both end of your string passes to check function as shown below.

javascript:check('WANT TO PUT TEXTBOX VALUE HERE');
发布评论

评论列表(0)

  1. 暂无评论