【问题标题】:Run script on a runat=server control在 runat=server 控件上运行脚本
【发布时间】:2015-12-07 15:24:33
【问题描述】:

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

删除 runat=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>

谢谢

【问题讨论】:

  • 使用 asp.net 控件的 ClientID 属性。如果您使用母版页,则服务器端控件 ID 将在运行时更改。参考这个msdn.microsoft.com/en-us/library/…
  • 将其更改为 $('').AnyTime_picker{...}
  • 控件在 FormView 里面所以我必须这样写
  • 你能把你完整的设计器代码吗?

标签: javascript asp.net runatserver


【解决方案1】:

你可以像这样从后面的代码调用函数:

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>

【讨论】:

  • 你,$("#txthSchedTime") 对我不起作用,所以我必须用 document.getEelement 替换它
  • 好的,但是如果您在 标记中包含 jquery.js 文件,它应该对您有用,祝您好运,如果现在可以使用,请选择最佳答案作为接受:) 谢谢跨度>
【解决方案2】:

试试这个例子:

 <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”。

【讨论】:

  • 遗憾的是,如果控件具有 runat=server 属性,类选择器仍然无法工作
【解决方案3】:

经过几次尝试和试验,我终于做到了。

<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();" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多