1

I have created UserControl that I wish to use on multiple pages. This control contains classic javascript but for some reason it will not load element to a variable. Client IDs look ok.

This is button that activates javascript:

<input type="submit" name="ctl00$ContentPlaceHolder1$ContactList$btn_NewContact" value="Potvrdit" onclick="javascript:return CheckContactName();" id="ContentPlaceHolder1_ContactList_btn_NewContact" class="MyButton" style="color:white;" />

This is the textbox:

<input name="ctl00$ContentPlaceHolder1$ContactList$con_fullname" type="text" id="ContentPlaceHolder1_ContactList_con_fullname" class="MyTextBox" />

This is Javascript function:

    function CheckContactName() {
    name = document.getElementById("ContentPlaceHolder1_ContactList_con_fullname");

    if (name.value == '' | name.value == null) {
        return false;
    }

    UploadContact();

    return true;
}

Now, when I debug this in a console the name.value is undefined. The name variable itself is just "[object HTMLInputElement]".

So no matter what is in that textbox, this function is always false. I also checked all IDs inside final client page and there are no duplicates. Can you tell why this is? Thanks.

5
  • try set your input type to button not submit. As submit in webforms application will do a postback, so this could be the issue of your problem. Commented Jul 26, 2018 at 8:16
  • Problem with that is that in my .aspx file I use button like this: <asp:Button ID="btn_NewContact" runat="server" Text="Potvrdit" CssClass="MyButton" style="color:white;" OnClientClick="javascript:return CheckContactName();" OnClick="btn_NewContact_Click"/> OnClientClick return false it should not do postback Commented Jul 26, 2018 at 8:21
  • ok... have you enabled viewstate on control. page or global on the project. Commented Jul 26, 2018 at 8:25
  • no, should I do it? Commented Jul 26, 2018 at 8:29
  • yes try it. on your control or directly on the textbox if it is an asp:textbox Commented Jul 26, 2018 at 8:30

2 Answers 2

1

I supose you set the input's value in code behind right?

Anyways, you might try using document.querySelector, and it seems that your logical operator is wrong, you are using | instead of ||.

function CheckContactName() {
    let name = document.querySelector(
        '#ContentPlaceHolder1_ContactList_con_fullname'
    );

    if ((name.value == '') || (name.value == null) || (name == undefined)) {
        return false;
    }

    UploadContact();

    return true;
}
Sign up to request clarification or add additional context in comments.

3 Comments

No I do not set value in code behind and "|" will just try first expression first if it is false it will not care for the other one. I will look into querySelector.
Does that work in Javascript tho? As far as I know in C# it does but never tested it on Javascript.
Still, value is undefined either way
1

Changed name to cname.

It seems that when you use control on a page that already contains some JavaScript function and that function declares variable with the same name as the one in usercontrol - this happends.

1 Comment

But how to avoid it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.