3

I'm trying to clear an ASP.NET Textbox, set to MultiLine (textarea in other words) when the user changes a project, here is the drop down list:

<label id="Label1" class="lbl">Project</label>

<asp:DropDownList ID="ddlProjectList" DataTextField="ClientProjectTitle" 
DataValueField="ProjectID" AppendDataBoundItems="true" CssClass="ddl"
AutoPostBack="true" ValidationGroup="Projects" runat="server">
    <asp:ListItem Text="-- select --" Value="0"></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="rfvProjectList" ControlToValidate="ddlProjectList" 
InitialValue="0" Text="(required)" ErrorMessage="Select a project" 
Display="Dynamic" ValidationGroup="Projects"
CssClass="error" runat="server"></asp:RequiredFieldValidator>

Here is the jQuery:

<script type="text/javascript">
    $(document).ready(function() {
        $('select[name*="ddlProjectList"]').change(function() {
            $('textarea[name*="txtDescription"]').attr('value', '');
        });
    });
</script>

Before people respond, I have tried it with just .val(''); - and here is the textbox I'm trying to clear:

<label class="lbl">Description</label>

<asp:TextBox ID="txtDescription" TextMode="MultiLine" Rows="5" 
CssClass="long-textbox-ml"  runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvDescription" ControlToValidate="txtDescription"
Text="(required)" ErrorMessage="Enter a description" Display="Dynamic"
ValidationGroup="Projects"
CssClass="error" runat="server"></asp:RequiredFieldValidator>

Any ideas why it is not working? I have stepped through it in Chrome and the event is firing.

Marcel
  • 15,039
  • 20
  • 92
  • 150
DarrylGodden
  • 1,526
  • 5
  • 25
  • 44

7 Answers7

3

You have 'AutoPostBack="true"' on the dropdown list. This will fire a form post back to the same page, causing the page to reload. Your JQuery event is trying to fire before the reload happens, but once that reload is triggered, you'll lose all the work that the JQuery function did.

Update: Either remove the 'AutoPostBack=true' or set the textfield's "Text" attribute to an empty string in the code behind when that event is fired (not sure from your question if there's a named function getting fired when that dropdown changes)

tbNumAcres.Text = "";
Graham
  • 3,217
  • 1
  • 27
  • 29
  • I think you're onto something here Graham, I've noticed that it is clearing on the first OnSelectedIndexChanged, but not subsequent ones, need to find a work around! – DarrylGodden Oct 25 '11 at 13:25
  • Yep, that's prob because after the first postback, the value stuck into the txtDescription gets recorded in the ASP.NET ViewState, so it sticks around after the page is 'reloaded' by the dropdown change. You need to clear the text field in CODE, not in jQuery. – Graham Oct 25 '11 at 17:52
  • Irrespective of what I've written below - you were first with the suggestion that the post back is causing this - so I've accepted yours, thank Graham. – DarrylGodden Oct 26 '11 at 07:27
  • Thanks! I didn't notice you posted your final solution before I updated my answer. Glad you got this figured out. ASP.NET WebForms auto-generated javascript can often collide with your own JS if you aren't careful. Its one of the reasons I'm looking into ASP MVC instead. – Graham Oct 26 '11 at 13:35
1

Try this:

$("#<%=ddlProjectList.ClientID%>").change(function(e){
    $("#<%=txtDescription.ClientID%>").text(""); //or val("")
});

Also, I don't know what you're doing with AutoPostBack="true", but if you're not using it for anything I would remove it.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
0

Assuming you have your selector correct and the event actual fires, try this instead...

$(document).ready(function() {
    $('select[name*="ddlProjectList"]').change(function() {
        $('#txtDescription').val('');
    });
});

I think your selector for txtDescription is failing.

EDIT: Based on Tariqulazam's comment...

$(document).ready(function() {
    $('select[name*="ddlProjectList"]').change(function() {
        $('#<%=txtDescription.ClientID%>').val('');
    });
});
musefan
  • 47,875
  • 21
  • 135
  • 185
  • This will definitely not work. Because the ASP.NET will emit the ID of txtDescription text box differently. I will suggest to use $('# <%=txtDescription.ClientID%>').val(''); – Tariqulazam Oct 25 '11 at 13:13
  • Hi all, here is the textarea in Chrome (inspect element): – DarrylGodden Oct 25 '11 at 13:15
  • @Tariqulazam I really hate that code block in jQuery, it causes security issues, which is why I tend to use the name selector. – DarrylGodden Oct 25 '11 at 13:15
  • @DarrylGodden - I replied based on your initial reply of using $('#txtDescription').val(''); – Tariqulazam Oct 25 '11 at 13:17
  • @musefan - I would suggest you to consider the security issue DarrylGodden mentioned. – Tariqulazam Oct 25 '11 at 13:20
  • 1
    @Tariqulazam: So what is the security issue? – musefan Oct 25 '11 at 13:23
  • 1
    @Musefan - Although in your scenario, I do not see any particular security issue, but there are issues in using inline code. I am not sure but you probably read this document from http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=19032. It is quiet long (I am yet to finish it) but worth a look. – Tariqulazam Oct 25 '11 at 23:13
  • @Tariqulazam: Thanks, will take a look when I get the chance – musefan Oct 26 '11 at 07:38
0

We would need to see more of the page, or maybe even its rendered version, but if I had to guess, your selector is not finding the element like you think it is.

Your best bet in my opinion is to use the console in Chrome, or the Firebug console and paste in your selector:

$('textarea[name*="txtDescription"]')

The console(s) should highlight or return a listing of the elements which match that selector. If none, that is your problem...

Here is a random SO post that has a screen grab of the console if it helps:

testing jQuery statements in Chrome JavaScript console

Community
  • 1
  • 1
fdfrye
  • 1,099
  • 7
  • 14
0

Is there a reason why you're using $('textarea[name*="txtDescription"]') instead of just using $('#txtDescription') ?

.val("") should work, as well as attr('value', '') or .empty(), so the problem here is probably that your selector doesn't work properly.

Leo
  • 5,069
  • 2
  • 20
  • 27
0

Try:

$(document).ready(function() {   
    $('[id$=ddlProjectList]').change(function() {  
        $('[id$=txtDescription]').val('');  
    });  
});  

EDIT
As Graham says, set the ddl AutoPostBack to false. Otherwise the jQuery handler will not work as you expected.

Also, you could use console.log to see if your handler is being fired (press F12 in Chrome to access the developer tools, and click the console button)

$(document).ready(function() {   
    $('[id$=ddlProjectList]').change(function() {  
        console.log("I'm in!");
        $('[id$=txtDescription]').val('');  
    });  
});
epzee
  • 568
  • 8
  • 22
0

Did it in C# instead:

if (!IsPostBack)
    {
        GetLiveProjects();
        GetClients();
        GetWeekRows();

        //if (Request.QueryString["SelectedDate"] != null)
        //{
        //    GetItineryForDay(Convert.ToDateTime(Request.QueryString["SelectedDate"]));
        //}
        //else
        //{
        //    DateTime dt = DateTime.Now;
        //    GetItineryForDay(dt);
        //}
    }
    else
    {
        txtDescription.Text = string.Empty;
    }

Utilising the postback of the drop down list - thanks for your help everyone!

DarrylGodden
  • 1,526
  • 5
  • 25
  • 44