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
Add a ment  | 

3 Answers 3

Reset to default 4

You 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