运行 runat=server 控件上的脚本

Run script on a runat=server control

如何在具有 运行at=server 属性的控件上 运行 脚本?

删除 运行at=server 使脚本 运行 顺利,但我将无法访问控件。

这是脚本。有什么想法吗?

<input runat="server" type="text" id="txthSchedTime" readonly="true" class="asclock" style="width:100px; background-color:lightyellow" onclick="setTimePicker();" />
<script>
$("#txthSchedTime").AnyTime_picker(
{
format: "%h:%i %p", labelTitle: "Schedule Time",
labelHour: "Hour", labelMinute: "Minutes"
});
</script>

谢谢

尝试使用此示例:

 <input runat="server" type="text" id="txthSchedTime" readonly="true" class="asclock" style="width:100px; background-color:lightyellow" onclick="setTimePicker();" />
<script>
    $("#<%=txthSchedTime.ClientID %>").val('test');

</script>

刚刚在我的本地机器上测试过。输入得到值"test".

您可以像这样从代码后面调用该函数:

yourForm.aspx.cs:

    protected void txthSchedTimeEvent(....)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "myFunction();", true);
    }

yourForm.aspx

        <html xmlns="http://www.w3.org/1999/xhtml">
            <head id="Head1" runat="server">
                <title>My Page</title>
                <script type="text/javascript">
                    function myFunction(){
                    $("#txthSchedTime").AnyTime_picker(
                    {
                    format: "%h:%i %p", labelTitle: "Schedule Time",
                    labelHour: "Hour", labelMinute: "Minutes"
                    });
                    }
                </script>
            </head>
            <body>
                <form id="form2" runat="server">
                <table>
                    <tr> <td> 
                     <asp:TextBox ID="txthSchedTime" runat="server" 
                        style="width:100px; background-color:lightyellow">
                     </asp:TextBox>
                    </td> </tr>
                </table> 
                </form>
            </body>
        </html>

经过多次尝试和试验,我找到了这个。

<script type="text/javascript">
        function setTimePick() {
            var tp = document.getElementById('<%=((HtmlInputText)fvVIPDtls.FindControl("txthSchedTime")).ClientID%>');
            $(tp).AnyTime_picker(
            {
                format: "%h:%i %p", labelTitle: "Schedule Time",
                labelHour: "Hour", labelMinute: "Minutes"
            });
        }
    </script>

然后我就调用我做的函数

<input runat="server" readonly="true" type="text" id="txthSchedTime" class="asclock" style="width:100px; background-color:lightyellow" onfocus="setTimePick();" />