这个asp.net代码有什么问题?文本框值未设置

时间:2014-01-19 16:51:22

标签: javascript asp.net

protected void Button1_Click(object sender, EventArgs e)
{
    if (count > 100) 
    {
        StringBuilder javascript = new StringBuilder();
        javascript.Append(" <script language=\"javascript\" type=\"text/javascript\">");
        javascript.Append(" var tmp = confirm(\"No:Of Records exceeds 1000.Please    confirm you want to continue\");");
        javascript.Append("if (tmp)");
        javascript.Append("{document.getElementById(\"    <%=TextBox1.ClientID%>\").value=\"1\"; alert(document.getElementById(\"<%=TextBox1.ClientID     %>\").value);}");


        javascript.Append(" </script>");
        ClientScript.RegisterStartupScript(GetType(), "recordscript", javascript.ToString(), false);

        return;
    }
}

这里我想通过单击按钮事件设置文本框的值,并且oly条件为true。所以我不能从source.actually调用该函数调用该函数但是文本框值没有设置..我真的不了解问题出在哪里..

1 个答案:

答案 0 :(得分:2)

protected void Button1_Click(object sender, EventArgs e)
    {

if(count>100) 
{
        StringBuilder javascript = new StringBuilder();
        javascript.Append(" <script language=\"javascript\" type=\"text/javascript\">");
        javascript.Append(" var tmp = confirm(\"No:Of Records exceeds 1000.Please    confirm you want to continue\");");
        javascript.Append("if (tmp)");
        javascript.Append("{document.getElementById('" + TextBox1.ClientID + "').value=\"1\"; alert(document.getElementById('" + TextBox1.ClientID+ "').value);}");


        javascript.Append(" </script>");
        ClientScript.RegisterStartupScript(GetType(), "recordscript", javascript.ToString(), false);

        return;
    }
}
}

您需要将TextBox1.ClientID与您的javascript字符串连接起来。您拥有的代码将按原样呈现到页面,查看带有视图源的呈现页面的输出,您将看到字符串'&lt;%= TextBox1.ClientID =%&gt;'不是预期的ID。请记住,内联显示表达式&lt;%= =%&gt;相当于服务器Response.Write()。