开发者

Regarding 'this' keyword in JavaScript

开发者 https://www.devze.com 2023-01-27 14:53 出处:网络
In my web page I\'ve a Linkbutton with OnClientClick event as show below. <asp:LinkButton ID=\"lnkbtn\" Text=\"Click\" runat=\"server\" OnClientClick=\"dosomething(this.Text)\" />

In my web page I've a Linkbutton with OnClientClick event as show below.

<asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="dosomething(this.Text)" />

and I've defined the function as shown below in the head section of the web "page

<script type="text/javascript">
    function dosomething(ObjCntxt)
    {
        alert(ObjCntxt.toLocaleString());
    开发者_JAVA百科    var textval = ObjCntxt;
        alert(textval.value);            
    }
</script>

When i run the page and click on the LinkButton i'm getting the message undefined.

I request you all kindly solve my problem.

Thanks & Regards.


This works for me:

<script type="text/javascript" language="javascript">
    function doSomething(ObjCntxt) {
        alert(ObjCntxt); // Text
        alert(ObjCntxt.toLocaleString()); // Text
        alert(ObjCntxt.toString()); // Text

        alert(ObjCntxt.value); // undefiend
    }
</script>

<asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="doSomething(this.text);">Text</asp:LinkButton>

Remember, that the content of doSomething is JavaScript, not .NET, so you should use JavaScript members, such as this.text not this.Text

What do you expect from ObjCntxt.value?? Christmas gift?


Try this One

     <script type="text/javascript" language="javascript">
    function doSomething(ObjValue) {
        alert(ObjValue); // Text
       }
</script>   

    <asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="doSomething(this.value);">Text</asp:LinkButton>
0

精彩评论

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