ASP.NET TextChanged不会被激怒

时间:2014-10-27 21:28:19

标签: c# asp.net

我正在做表格,用户会发送您可以看到的某些信息。但是我想制作一个事件,当用户将文字示例写入描述并点击一些事件时,示例流行消息说" Hello"。我已将TextChanged函数添加到文本框中,甚至AutoPostBack也设置为true。我是ASP.net的新手,但在我检查的每个教程中都说我必须只添加这些东西才能做我想做的事情。我也注意到它在某种程度上注册了文本更改,因为当我在Internet Explorer上调试网站并在描述中写入内容然后单击时。然后在IE的左下角显示"错误页面"当我在描述中写了一些东西然后点击了,否则会读到"完成"。所以它正在解雇,但不知何故,我在TextChanged中放入的代码并不重要,它不会运行它。

以下是我的文件:

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>

1 个答案:

答案 0 :(得分:1)

在您的字段中更改数据时,请勿使用自动回发

  • 回发 - 应该在字典中出现痛苦,丑陋和有害用户体验。特别是在被滥用时。

  • 更新面板 - 为了避免这种可爱的屏幕闪烁,您最终会实现一个更新面板,这将占您网页的一大部分努力工作。还会消耗过多的内存,因为它会将您的页面放在该实例并提交到内存。然后使用新更改进行刷新,同时保留在内存中。

我假设您尝试验证用户输入。您可以使用多个库,例如 Valid8 ,或者自己在Javascript中编写一个简单的验证。这种方法将缓解 Postback ,因为客户端将进行验证。

示例:

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

简单,如果字段为null,则边框为红色。这将提供没有 Postback 的功能,只需确保使用OnClientClick并创建一个功能。此外,虽然很大一部分将完成客户端,您应该检查服务器上的数据。只是为了确保它没有被篡改。

希望这可以解释为什么你应该避免过度的回发方法,你将要实施它。