Sun
Sun

Reputation: 4718

Pass ASP.Net control into Javascript function

I have a javascript function which I want to send a ASP button control. So my function is like this:

<script type="text/javascript">
    function PreventKeyPress(e, sender, button) {
        //do something with the button
        button.click();
    }
</script>

From the ASP markup I want to be able to pass the button in. I've tried a few ways but this is something like I'd want to do:

<asp:TextBox ID="textBoxDate" runat="server" onKeyDown="PreventKeyPress(event, this, <%=buttonDate.ClientId %>);" />
<asp:ImageButton ID="buttonDate" runat="server" />

Any ideas how I can get this to work?

Upvotes: 0

Views: 67

Answers (1)

Bhavik
Bhavik

Reputation: 4904

.aspx page

<form id="form1" runat="server">
<asp:TextBox ID="textBoxDate" runat="server" onKeyDown="PreventKeyPress(event, this, 'buttonDate');" />
<asp:ImageButton ID="buttonDate" runat="server" onclick="buttonDate_Click" />
</form>
<script type="text/javascript">
    function PreventKeyPress(e, sender, button) {
        console.log(e + '.......' + sender + '.......' + button);
        var buttonObj = document.getElementById(button);
        buttonObj.click();
    }
</script>

This worked on my system... Hope this helps...!!!

Upvotes: 1

Related Questions