简体   繁体   中英

ASP.NET TextChanged won't fire

I'm doing form were users send certain info as you can see. But I want to make event that when user has written text example to description and clicks away some event comes, example pop message saying "Hello". I have added TextChanged function to the text box and even AutoPostBack is set to true. Im new to ASP.net but in every tutorial I have checked says that I have to add only these to things to be able to do what I want to do. I have also noticed it is somewhat registering the text change because when I debug the site on Internet Explorer and write something to the description and then click away. Then on the low left corner of IE it says "Error on page" when I have written something in description and then clicked away otherwise there reads "Done". SO it IS firing but somehow it doesn't matter what code I put inside of the TextChanged it wont run it.

Here are my files:

aspx.cs:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Text;


public partial class _Default : System.Web.UI.Page
{
    static readonly String attUrl = @"c:\ASDBugReport/WSASDBugReport/Attachments/";
    int attnumber = 0;
    string userEmail = "";
    string mysiteAddress = "http://company.com:xyz/mysearch=";
    string project;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            userEmail = User.Identity.Name.ToString();
            if (cb9.Visible == true)
            {
                attnumber++;
            }
            if (cb8.Visible == true)
            {
                attnumber++;
            }
            if (cb7.Visible == true)
            {
                attnumber++;
            }
            if (cb6.Visible == true)
            {
                attnumber++;
            }
            if (cb5.Visible == true)
            {
                attnumber++;
            }
            if (cb4.Visible == true)
            {
                attnumber++;
            }
            if (cb3.Visible == true)
            {
                attnumber++;
            }
            if (cb2.Visible == true)
            {
                attnumber++;
            }
            if (cb1.Visible == true)
            {
                attnumber++;
            }
            if (cb0.Visible == true)
            {
                attnumber++;
            }
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(Page.GetType(),"Script","src='Popup.js';", true);
            ddlComponent.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlPriority.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlIssueType.Items.Insert(0, new ListItem(String.Empty, String.Empty));
        }
    }
    protected void btnSaveAttachment_Click(object sender, EventArgs e)
    {
        if (attnumber == 10)
        {
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "Script", "alert('Max 10 attachments');", true);
        }
        else
        {
            if (fuIssueAttachmentUppload.FileName.Length > 0)
            {
                addAttachmentToCheckBox();
                saveAttachment();
            }
            else { }
        }
    }
    protected void saveAttachment()
    {
        if(fuIssueAttachmentUppload.HasFile)
        {
            attnumber++;
            fuIssueAttachmentUppload.SaveAs(attUrl.ToString() + fuIssueAttachmentUppload.FileName.ToString());
        }
        else{}

    }
    protected void addAttachmentToCheckBox()
    {
        if (attnumber == 0)
        {
            cb0.Visible = true;
            cb0.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb0.Checked = true;
        }
        else if (attnumber == 1)
        {
            cb1.Visible = true;
            cb1.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb1.Checked = true;
        }
        else if (attnumber == 2)
        {
            cb2.Visible = true;
            cb2.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb2.Checked = true;
        }
        else if (attnumber == 3)
        {
            cb3.Visible = true;
            cb3.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb3.Checked = true;
        }
        else if (attnumber == 4)
        {
            cb4.Visible = true;
            cb4.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb4.Checked = true;
        }
        else if (attnumber == 5)
        {
            cb5.Visible = true;
            cb5.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb5.Checked = true;
        }
        else if (attnumber == 6)
        {
            cb6.Visible = true;
            cb6.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb6.Checked = true;
        }
        else if (attnumber == 7)
        {
            cb7.Visible = true;
            cb7.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb7.Checked = true;
        }
        else if (attnumber == 8)
        {
            cb8.Visible = true;
            cb8.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb8.Checked = true;
        }
        else if (attnumber == 9)
        {
            cb9.Visible = true;
            cb9.Text = fuIssueAttachmentUppload.FileName.ToString();
            cb9.Checked = true;
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (ComponentsValidate() == true)
        {

            SendEmail();
            EmptyForm();
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "Script", "alert('Success');", true);

        }
        else {ClientScript.RegisterClientScriptBlock(Page.GetType(), "Script", "alert('All required fields not filled');", true); }
    }
    private Boolean ComponentsValidate()
    {
        rfvIssueType.Validate();
        rfvComponent.Validate();
        rfvHeader.Validate();
        rfvDescription.Validate();

        bool b = true;
        if (!rfvIssueType.IsValid)
        {
            b = false;
        }
        if (!rfvComponent.IsValid)
        {
            b = false;
        }
        if (!rfvHeader.IsValid)
        {
            b = false;
        }
        if (!rfvDescription.IsValid)
        {
            b = false;
        }
        return b;
    }
    private void EmptyForm()
    {
        txtIssueHeader.Text = "";
        txtIssueDescription.Text = "";
        cb0.Text = "";
        cb1.Text = "";
        cb2.Text = "";
        cb3.Text = "";
        cb4.Text = "";
        cb5.Text = "";
        cb6.Text = "";
        cb7.Text = "";
        cb8.Text = "";
        cb9.Text = "";
        cb0.Text = "";
        cb0.Visible = false;
        cb1.Visible = false;
        cb2.Visible = false;
        cb3.Visible = false;
        cb4.Visible = false;
        cb5.Visible = false;
        cb6.Visible = false;
        cb7.Visible = false;
        cb8.Visible = false;
        cb9.Visible = false;
        ddlComponent.ClearSelection();
        ddlIssueType.ClearSelection();
        ddlPriority.ClearSelection();
        ddlYmparisto.ClearSelection();
        txtCompany.Text = "";
    }
    private string getUserEmail(string user) { 
        int le = user.Length -8;
        user = user.Substring(8, le);
        user = user.ToLower() + "@company.com";
        return user;
    }
    private string getMysite(string user) {
        int le = user.Length - 8;
        user = user.Substring(8, le);
        string s = mysiteAddress + user.ToLower();
        return s;
    }
    public void SendEmail()
    {

        if (ddlIssueType.SelectedValue == "Bug") {
            project = "bugs";
        }
        else if (ddlIssueType.SelectedValue == "Feature Request")
        {
            project = "company";
        }
        else if (ddlIssueType.SelectedValue == "User Rights")
        {
            project = "USER";
        }
        else {
            project = "company";
        }
        MailAddress from = new MailAddress(getUserEmail(userEmail).ToString());
        MailAddress to = new MailAddress("ASD@company.com");
        MailMessage ASDMail = new MailMessage(from,to);
        ASDMail.Subject = txtIssueHeader.Text.ToString();
        ASDMail.Body = "@Ext.Creator = " + getUserEmail(userEmail).ToString() + "\n"  +
                        "@Ext.Creator.URL = " + getMysite(userEmail).ToString() + "\n" +
                        "@Environment = " + ddlYmparisto.SelectedItem.Text.ToString() + " : " + txtCompany.Text.ToString() + "\n" +
                        "@project = " + project.ToString() + "\n" +
                        "@issueType = " + ddlIssueType.SelectedItem.Value.ToString() + "\n" +
                        "@components = " + ddlComponent.SelectedItem.Text.ToString() + "\n" + "\n" +
                        txtIssueDescription.Text.ToString();
        if (attnumber > 0)
        {
            if (cb9.Text.Length > 0)
            {
                if (cb9.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb9.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb8.Text.Length > 0)
            {
                if (cb8.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb8.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb7.Text.Length > 0)
            {
                if (cb7.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb7.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb6.Text.Length > 0)
            {
                if (cb6.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb6.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb5.Text.Length > 0)
            {
                if (cb5.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb5.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb4.Text.Length > 0)
            {
                if (cb4.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb4.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb3.Text.Length > 0)
            {
                if (cb3.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb3.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb2.Text.Length > 0)
            {
                if (cb2.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb2.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb1.Text.Length > 0)
            {
                if (cb1.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb1.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
            if (cb0.Text.Length > 0)
            {
                if (cb0.Checked)
                {
                    ASDMail.Attachments.Add(new Attachment(attUrl.ToString() + cb0.Text.ToString(), MediaTypeNames.Application.Octet));
                }
            }
        }
        SmtpClient client = new SmtpClient();
        client.Send(ASDMail);
        if (DeleteAttachments()) { 

        }
    }
    private Boolean DeleteAttachments() {
        bool r = true;
        if (attnumber == 1)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(cb0.Text.ToString()));
                if (TheFile.Exists)
                {
                    try {File.Delete(cb0.Text.ToString());}
                    catch
                    {
                    }
                }
                else
                {
                    r = false;
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }
        else if (attnumber == 2)
        {
            try
            {
                FileInfo TheFile = new FileInfo(cb1.Text.ToString());
                if (TheFile.Exists)
                {
                    try { File.Delete(cb1.Text.ToString()); }
                    catch
                    {
                    }
                }
                else
                {
                    r = false;
                    throw new FileNotFoundException();
                }
            }

            catch (FileNotFoundException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }
        else if (attnumber == 3)
        {
            try
            {
                FileInfo TheFile = new FileInfo(cb2.Text.ToString());
                if (TheFile.Exists)
                {
                    try { File.Delete(cb2.Text.ToString()); }
                    catch
                    {
                    }
                }
                else
                {
                    r = false;
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException ex)
            {
            }
            catch (Exception ex)
            {
            }

        }
        else if (attnumber == 4)
        {
            try
            {
                FileInfo TheFile = new FileInfo(cb3.Text.ToString());
                if (TheFile.Exists)
                {
                    try { File.Delete(cb3.Text.ToString()); }
                    catch
                    {
                    }
                }
                else
                {
                    r = false;
                    throw new FileNotFoundException();
                }
            }

            catch (FileNotFoundException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }

        return r;
    }
    private void HideCheckBox() {
        if (attnumber == 0) {
            cb0.Visible = false;
        }
        if (attnumber == 1) {
            cb1.Visible = false;
        }
        if (attnumber == 2) {
            cb2.Visible = false;
        }
        if (attnumber == 3) {
            cb3.Visible = false;
        }
        if (attnumber == 4) {
            cb4.Visible = false;
        }
        if (attnumber == 5) {
            cb5.Visible = false;
        }
        if (attnumber == 6) {
            cb6.Visible = false;
        }
        if (attnumber == 7) {
            cb7.Visible = false;
        }
        if (attnumber == 8) {
            cb8.Visible = false;
        }
    }
    protected void cb0_CheckedChanged(object sender, EventArgs e)
    {
        if (!cb0.Checked)
        {
            attnumber = attnumber - 1;
            cb0.Visible = false;
            cb0.Text = cb1.Text.ToString();
            cb1.Text = cb2.Text.ToString();
            cb2.Text = cb3.Text.ToString();
            cb3.Text = cb4.Text.ToString();
            cb4.Text = cb5.Text.ToString();
            cb5.Text = cb6.Text.ToString();
            cb6.Text = cb7.Text.ToString();
            cb7.Text = cb8.Text.ToString();
            cb8.Text = cb9.Text.ToString();
            if (attnumber >= 1) {
                cb0.Visible = true;
                cb0.Checked = true;
            }
            HideCheckBox();
        }
    }
    protected void cb1_CheckedChanged(object sender, EventArgs e)
    {
        if (!cb1.Checked)
        {
            attnumber = attnumber - 1;
            cb1.Visible = false;
            cb1.Text = cb2.Text.ToString();
            cb2.Text = cb3.Text.ToString();
            cb3.Text = cb4.Text.ToString();
            cb4.Text = cb5.Text.ToString();
            cb5.Text = cb6.Text.ToString();
            cb6.Text = cb7.Text.ToString();
            cb7.Text = cb8.Text.ToString();
            cb8.Text = cb9.Text.ToString();
            if (attnumber >= 2)
            {
                cb1.Visible = true;
                cb1.Checked = true;
            }
            HideCheckBox();
        }
    }
    protected void cb2_CheckedChanged(object sender, EventArgs e)
    {
        if (!cb2.Checked)
        {
            attnumber = attnumber - 1;
            cb2.Visible = false;
            cb2.Text = cb3.Text.ToString();
            cb3.Text = cb4.Text.ToString();
            cb4.Text = cb5.Text.ToString();
            cb5.Text = cb6.Text.ToString();
            cb6.Text = cb7.Text.ToString();
            cb7.Text = cb8.Text.ToString();
            cb8.Text = cb9.Text.ToString();
            if (attnumber >= 3)
            {
                cb2.Visible = true;
                cb2.Checked = true;
            }
            HideCheckBox();
        }
    }
}

aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" Theme="SkinStyleOfASDForm" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id="divHeader">
    <asp:Label ID="lblHeader" runat="server" 
            Text="IT stuff" Font-Bold="True" 
            Font-Size="Large"></asp:Label>
</div><br />
<div id="divContent">
    <div id="divIssueHeader">
        <asp:Label ID="lblIssueHeader" runat="server" Text="Header:"></asp:Label><br />
        <asp:TextBox ID="txtIssueHeader" runat="server" Width="394px" 
            CausesValidation="True" 
            ToolTip="Write header"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvHeader" runat="server" 
            ControlToValidate="txtIssueHeader" ErrorMessage="fill missing info" 
            ForeColor="Red"></asp:RequiredFieldValidator>
    </div><br />
    <div id="divIssueType" 
        style="height: 70px;">
    <asp:Label ID="lbIssueType" runat="server" Text="type: "></asp:Label><br />
        <asp:DropDownList ID="ddlIssueType" runat="server" DataValueField="ID" 
                SkinID="ddListSkinId" AppendDataBoundItems="True" 
            CausesValidation="True" 
            ToolTip="give type">
            <asp:ListItem Value="Bug">bug</asp:ListItem>
            <asp:ListItem Value="Feature Request">Feature</asp:ListItem>
            <asp:ListItem Value="User Rights">User</asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="rfvIssueType" runat="server" 
                ControlToValidate="ddlIssueType" ErrorMessage="fill missing info" 
                ForeColor="Red"></asp:RequiredFieldValidator>
    </div>
    <div id="divIssueComponent" 
    style="height: 70px;">
    <asp:Label ID="lbIssueComponent" runat="server" Text="system: "></asp:Label><br />
                <asp:DropDownList ID="ddlComponent" runat="server" 
            DataSourceID="dsComponents" DataTextField="cname" DataValueField="ComponentID" 
                    SkinID="ddListSkinId" AppendDataBoundItems="True" 
            CausesValidation="True" ToolTip="choose system">
            </asp:DropDownList>
                <asp:RequiredFieldValidator ID="rfvComponent" runat="server" 
                    ControlToValidate="ddlComponent" ErrorMessage="fill missing info" 
                    ForeColor="Red"></asp:RequiredFieldValidator>
        <asp:SqlDataSource ID="dsComponents" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ASDdbConnectionString %>"          

            SelectCommand="SELECT ID AS ComponentID, cname, PROJECT FROM ASDschema.component WHERE (PROJECT = '9999')">
        </asp:SqlDataSource>
    </div>
    <div id="divIssueAssignee" style="height: 70px;">
        <asp:Label ID="lblenvi" runat="server" Text="envi:"></asp:Label>
        <br />
        <asp:DropDownList ID="ddlYmparisto" runat="server" AppendDataBoundItems="True" 
            ToolTip="choose envi">
            <asp:ListItem Selected="True">basic</asp:ListItem>
            <asp:ListItem>test</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Label ID="lblcompany" runat="server" Text="envi "></asp:Label>
        <br />
        <asp:TextBox ID="txtcompany" runat="server"></asp:TextBox>
    </div>
    <br />
    <div id="divIssueDescription">
        <asp:Label ID="lblIssueDescription" runat="server" Text="desc "></asp:Label>
            <asp:RequiredFieldValidator ID="rfvDescription" runat="server" 
            ControlToValidate="txtIssueDescription" ErrorMessage="fill missing info" 
            ForeColor="Red"></asp:RequiredFieldValidator>
            <br />
        <asp:TextBox ID="txtIssueDescription" runat="server" Height="345px" 
            TextMode="MultiLine" Width="586px" CausesValidation="True" 
            EnableTheming="True" style="margin-right: 0px"></asp:TextBox>
            </div><br />
    <div id="divIssueAttachment">
        <asp:Label ID="lblIssueAttachment" runat="server" Text="attach file "></asp:Label><br />
        <asp:FileUpload ID="fuIssueAttachmentUppload" runat="server" Width="218px" 
            EnableViewState="False" ViewStateMode="Disabled" /><br />
        <asp:Button ID="btnSaveAttachment" runat="server" Text="attach" 
            onclick="btnSaveAttachment_Click" Width="69px" CausesValidation="False" /><br />
            <asp:CheckBox ID = "cb0" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb0_CheckedChanged"></asp:CheckBox><br />
                        <asp:CheckBox ID = "cb1" runat="server" visible= "false" 
            ViewStateMode="Enabled" AutoPostBack="True" 
            oncheckedchanged="cb1_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb2" runat="server" Visible="false" 
            AutoPostBack="True" oncheckedchanged="cb2_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb3" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb3_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb4" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb4_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb5" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb5_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb6" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb6_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb7" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb7_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb8" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb8_CheckedChanged">
            </asp:CheckBox><br />
                        <asp:CheckBox ID = "cb9" runat="server" visible= "false" 
            AutoPostBack="True" oncheckedchanged="cb9_CheckedChanged">
            </asp:CheckBox><br />
    </div>
    <div id="divSend">
        <asp:Button ID="btnSend" runat="server" Text="send" onclick="btnSend_Click" 
            Height="28px" Width="290px" CausesValidation="False"/>
    </div>
</div>
</asp:Content>

Don't use Auto Postback for when data is changed in your fields.

  • Postback - Should be in the dictionary for a pain, ugly, and harmful to user experience. Especially when abused.

  • Update Panel - To avoid that lovely screen flicker you'll end up implementing an Update Panel which will make a large chunk of your page a pain to work on. Also consume excessive memory, as it will take your page at that instance and commit to memory. Then refresh with the new changes, keeping both in memory.

I'm assuming your attempting to validate the users input. You could use several libraries such as Valid8 , or write a simple validation in Javascript yourself. This approach will alleviate the Postback as the Client will have the validation present.

Example:

$(document).ready(function () {
     if($("#<%= txtFirstName.ClientId %>").val() == '') {
          $(this).css({ border: "Red" });
     }
});

Simple, if the field is null make the border red. This will provide the functionality without a Postback just ensure you use OnClientClick and create a function. Also though a large chunk of this will be done client you should check the data on server. Just to ensure it hasn't been tampered.

Hope this explains why you should avoid the excessive postback approach, which your about to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM