c# - How to enable javascript in asp.net webforms - Stack Overflow

admin2025-04-18  1

i am trying to use javascript events in asp webforms. but events for input controls like textfield, such as onClick, onFocus,onBlur, dont appear. do i need to change my directive:

<%@ Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" CodeFile="AskQuestion.aspx.cs" Inherits="AskQuestion" %>

i want to be able to do this:

//code page
    protected void Page_Load(object sender, EventArgs e)
{
    QuestionTextBox1.Attributes["onfocus"] = "ClearSearchText()";

//Markup page
     function ClearSearchText() {
        var searchUserName = document.getElementById('<%=QuestionTextBox1.ClientID%>');

        if (searchUserName.value = searchUserName.defaultValue) {
            searchUserName.value = "";
        }


        return false;
    }

<p dir="rtl" style="">
<asp:TextBox ID="QuestionTextBox1" runat="server" Width="702px" 

Text="פרטים עד 5000 תווים"></asp:TextBox>

i am trying to use javascript events in asp webforms. but events for input controls like textfield, such as onClick, onFocus,onBlur, dont appear. do i need to change my directive:

<%@ Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" CodeFile="AskQuestion.aspx.cs" Inherits="AskQuestion" %>

i want to be able to do this:

//code page
    protected void Page_Load(object sender, EventArgs e)
{
    QuestionTextBox1.Attributes["onfocus"] = "ClearSearchText()";

//Markup page
     function ClearSearchText() {
        var searchUserName = document.getElementById('<%=QuestionTextBox1.ClientID%>');

        if (searchUserName.value = searchUserName.defaultValue) {
            searchUserName.value = "";
        }


        return false;
    }

<p dir="rtl" style="">
<asp:TextBox ID="QuestionTextBox1" runat="server" Width="702px" 

Text="פרטים עד 5000 תווים"></asp:TextBox>

Share Improve this question edited May 7, 2011 at 14:14 Raynos 170k57 gold badges357 silver badges398 bronze badges asked May 7, 2011 at 11:41 Dmitry MakovetskiydDmitry Makovetskiyd 7,05533 gold badges102 silver badges160 bronze badges 4
  • So, what is the question? Yes, I think you really need AutoEventWireup. – Erick Petrucelli Commented May 7, 2011 at 11:44
  • Can you provide a sample of the actual HTML elements and associated events that aren't firing? – Sir Crispalot Commented May 7, 2011 at 11:58
  • You certainly don't need to change the directive. – jakubiszon Commented May 7, 2011 at 12:15
  • "AutoEventWireup" is for server side events, not JS. And not for onSomeEvent="method" code, but for Object_Event methods to work automatically, and they usually apply to things like page and user controls not text boxes and stuff, like Page_Load. – Meligy Commented May 7, 2011 at 13:00
Add a ment  | 

2 Answers 2

Reset to default 2

Add onfocus and onblur into the markup as follows:

<asp:TextBox ID="TextBox1" runat="server" onfocus="TextBox1_focus(this, event)" onblur="TextBox1_blur(this, event)" Text="Search..."></asp:TextBox>

<script type="text/javascript">
    var searchText = 'Search...';

    function TextBox1_focus(sender, e) {
        if (sender.value == searchText)
            sender.value = '';
    }

    function TextBox1_blur(sender, e) {
        if (sender.value == '')
            sender.value = searchText;
    }
</script>

Well, not sure which ASP.NET version you use. I think last versions allow this (rendering attributes that the server controls don't understand to the browser still). Try using "onfocus" instead (lower case).

However, if this is not working for you, then you have to do it from code behind...

protected void Page_Load(object sender, EventArgs e)
{
    QuestionTextBox1. Attributes["onfocus"]="someJavaScriptMethod";
}

Alternatively, if you have jQuery in the page you can go something like ...

<script type="text/javascript">
$(function() {
    $('#<%= QuestionTextBox1.ClientID %>').focus(someJavaScriptMethod);
});
</script>

If you do that, inside someJavaScriptMethod(), you can use the word this to point at the focused control, and you can create a jQuery object from it easily like $(this).

.

Please leave me a ment if none of the above solves your problem.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744971052a277490.html

最新回复(0)