locked
Add [onkeydown] to Textbox Controls from codebehind. RRS feed

  • Question

  • User944339287 posted

    Hi guys.. i'm trying to add an attribute to Textbox Control while creating Dynamic TextBox Control.

    the [onkeydown] is supposed to like this this:
    <input type="text" readonly onkeydown="if(event.key==='.' || event.key==='e'){event.preventDefault();}" value="0">

    but the result returned as following:
    <input type="text" value="0" id="body_txt_product_option0" onkeydown="if(event.key===&#39;.&#39; || event.key===&#39;e&#39;){event.preventDefault();}" />

    Below is my sample code. Please advise. TQ

    Dim txt As New TextBox()
    txt.ID = "txt_product_option" & i
    txt.Text = "0"
    txt.Attributes.Add("readonly", "")
    txt.Attributes.Add("onkeydown", "if(event.key==='.' || event.key==='e'){event.preventDefault();}")
    Me.pnlTextBox.Controls.Add(txt)



    Monday, December 23, 2019 7:01 AM

All replies

  • Tuesday, December 24, 2019 2:17 AM
  • User288213138 posted

    Hi kengkit,

    the [onkeydown] is supposed to like this this:
    <input type="text" readonly onkeydown="if(event.key==='.' || event.key==='e'){event.preventDefault();}" value="0">

    but the result returned as following:
    <input type="text" value="0" id="body_txt_product_option0" onkeydown="if(event.key===&#39;.&#39; || event.key===&#39;e&#39;){event.preventDefault();}" />

    According to your description, I can't reproduce your question.

    Is pnlTextBox a TextBox control? What version of the browser are you using?

    Please show me your aspx code that can reproduce your question.

    Best regards,

    Sam

    Tuesday, December 24, 2019 2:57 AM