c#asp.net textbox动态添加oninput属性

时间:2016-05-19 07:31:54

标签: javascript c# asp.net

我想这样做,所以每次文本框内的值发生变化时,我动态添加的文本框都会调用javascript函数。这通过使用onInput属性来工作。我可以动态地添加这个属性吗?

Textbox txt = new TextBox();
txt.ID = "test";
txt.Attrubute = onInput("jsfunction()");
form1.Controls.Add(txt);

txt.Attributes["onInput"] = "jsfunction()";

提前致谢:)

编辑:

这是我的整个代码段。

pholder.Controls.Add(new LiteralControl("<table>"));
for (int i = 0; i < dv_inputParameter.Count; i++)
{
    pholder.Controls.Add(new LiteralControl("<tr>"));
    pholder.Controls.Add(new LiteralControl("<td>"));
    Label lbl = new Label();
    lbl.Text = dv_inputParameter.Table.Rows[i][1].ToString() + " ";
    lbl.ID = "lbl_parameter" + i;
    pholder.Controls.Add(lbl);
    pholder.Controls.Add(new LiteralControl("</td>"));

    pholder.Controls.Add(new LiteralControl("<td>"));
    TextBox txt = new TextBox();
    String tb_id = "tb_parameter" + i;
    txt.ID = tb_id;
    txt.Attributes.Add("onkeyup", String.Format(@"var txt = document.getElementById({0}); var btn = document.getElementById('Btn_Start'); btn.disabled = (txt.value.trim().length == 0)", tb_id));
    pholder.Controls.Add(txt);
    pholder.Controls.Add(new LiteralControl("</td>"));
    pholder.Controls.Add(new LiteralControl("</tr>"));
}
pholder.Controls.Add(new LiteralControl("</table>"));

我将标签和文本框放在html表中。该函数应该使用ID&#34; Btn_Start&#34;只要填写完所有文本框,就可以点击。

3 个答案:

答案 0 :(得分:0)

您可以使用此

txt.Attributes.Add("onkeyup", "alert('Key UP');");

答案 1 :(得分:0)

试试这个

  txt.Attributes.Add("onKeyUp", "keyUP()")

为文本框添加此属性

private int distance2(int alpha, int beta) {
    int distance = beta - alpha;      // This is the distance mod 360
    if (phi <= -180) {
        distance += 360; // you were between -360 and -180 -> 0 <= distance <= 180
    else if (phi > 180) {
        distance -= 360; // you were between 180 and 360 -> 180 < distance <= 0
    }
    return distance;
}

每当您在文本框中输入任何字母时,都会提醒您。

答案 2 :(得分:0)

如果您想在文本框内的文字发生变化时调用某个功能,可以将onchange attribut添加到文本框中。

txt.Attributes["onchange"] = "jsfunction()";