admin管理员组文章数量:1326286
how can you run a script on a control that has a runat=server attribute?
Removing the runat=server makes the script run smoothly, but I wouldn't be able to access the control.
Here's the script. Any idea?
<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>
thanks
how can you run a script on a control that has a runat=server attribute?
Removing the runat=server makes the script run smoothly, but I wouldn't be able to access the control.
Here's the script. Any idea?
<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>
thanks
Share Improve this question asked Sep 11, 2015 at 6:43 cracker_chancracker_chan 931 gold badge3 silver badges13 bronze badges 5- Use ClientID property of the asp control. If you are using master page then the server side control id will be changed at run time. Refer this msdn.microsoft./en-us/library/… – Sain Pradeep Commented Sep 11, 2015 at 6:46
- <script>$('<%=((HtmlInputText)fvVIPDtls.FindControl("txthSchedTime")).ClientID %>').AnyTime_picker... I did this but it' doesn't work – cracker_chan Commented Sep 11, 2015 at 6:56
- change it to $('<%=txthSchedTime.ClientID %>').AnyTime_picker{...} – Sain Pradeep Commented Sep 11, 2015 at 7:01
- The control is inside a FormView so I have to write it that way – cracker_chan Commented Sep 11, 2015 at 7:20
- can you put your plete designer code? – Sain Pradeep Commented Sep 11, 2015 at 7:31
3 Answers
Reset to default 4You can call the function from code behind like this :
yourForm.aspx.cs:
protected void txthSchedTimeEvent(....)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "myFunction();", true);
}
yourForm.aspx
<html xmlns="http://www.w3/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>
Try using this for example :
<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>
Just tested it on my local machine. The input gets the value "test".
After several attempts and trials it leads me to this.
<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>
then i just call the function i made
<input runat="server" readonly="true" type="text" id="txthSchedTime" class="asclock" style="width:100px; background-color:lightyellow" onfocus="setTimePick();" />
本文标签: javascriptRun script on a runatserver controlStack Overflow
版权声明:本文标题:javascript - Run script on a runat=server control - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211073a2433723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论