QuestionableSounder
QuestionableSounder

Reputation: 91

Unable to get value of the property 'click': object is null or undefined

So I want a Button1 to be clicked when the submit button has been clicked.

Here is the relevant code:

<input id="Submit1" type="submit" value="Search" onclick="javascript:check_user()" /> 
<script type="text/javascript">
            function check_user() {
                document.getElementById('<%=Button1.ClientID%>').click();
            }  
</script>
<asp:Button id="Button1" runat="server" Visible="false" OnClick="Button1_Click"/>

And here is the error I get:

Microsoft JScript runtime error: Unable to get value of the property 'click': object is null or undefined

Does anyone have any ideas as to what the problem might be? I've been tearing my hair out trying to work it out.

Upvotes: 0

Views: 12326

Answers (3)

Chen G
Chen G

Reputation: 41

As you made the button invisible, asp.net is not rendering the button control in html. (You can check the HTML source). You can create a style and apply it to the asp.net button once it is rendered at client side to make it invisible.

.MyHiddenButtonStyle{display:none;}// add this in the header in styles

<input id="Submit1" type="button" name="search" value="Search" onclick="javascript:check_user()" />
<asp:Button id="Button1" runat="server" CssClass="MyHiddenButtonStyle" OnClick="Button1_Click"/>

function check_user() {
        document.getElementById('<%=Button1.ClientID%>').click();
    }

One more thing I have made the submit button as normal button. anyways you are raising a postback event using javascript.

Upvotes: 0

mellamokb
mellamokb

Reputation: 56779

Can't you just do it with a single button?

<asp:Button id="Button1" runat="server" Text="Search" OnClick="Button1_Click"/>

Server-side code:

protected void Button1_Click(object sender, EventArgs e) {
    // processing code here
}

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13599

if you are not limited to java-script you can try using the trigger from jquery like this

 <script type="text/javascript">
            function check_user() {
           $("#<%=Button1.ClientID%>").trigger('click');
         //or

 $("#<%=Button1.ClientID%>").click();

            }  
</script>

Upvotes: 1

Related Questions