0

我有一个 Web 表单,它使用文本框来更新数据库中的评论字段。我想在更新前检查一个空白,并有一个提示框,上面写着“评论部分空白,继续吗?”,然后有一个是/否选项。这将允许用户返回更新而不进行更改。当用户在 gridview 的行上选择编辑时,文本框可见。我将在下面提供代码,以便任何人都可以看到我在做什么。如果不能以这种方式完成,只需一个消息框说明评论部分为空白就足够了。这是后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;

namespace AnnoTracker
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public static class MyVariables
        {
            public static int PI = 0;
            public static string JN = "";
            public static string ST = "";
            public static string AT = "";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();
                dlAnnoType.SelectedValue = "Agency Error";
            }
        }

        protected void EditSummary(object sender, GridViewEditEventArgs e)
        {

            gvSummary.EditIndex = e.NewEditIndex;
            string _custName = gvSummary.DataKeys[e.NewEditIndex].Value.ToString();
            BindData();
        }

        protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvSummary.EditIndex = -1;
            BindData();
        }

        protected void gvSummary_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSummary.PageIndex = e.NewPageIndex;
            MyVariables.PI = e.NewPageIndex;
            BindData();
        }


        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
            {

                DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");

                string _custName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
                string BUquery = "select distinct Unit from vw_BU where Business='" + _custName + "'";
                SqlCommand BUcmd = new SqlCommand(BUquery);
                dlBU.DataSource = GetData(BUcmd);
                dlBU.DataTextField = "Unit";
                dlBU.DataValueField = "Unit";
                dlBU.DataBind();
                dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;

                DropDownList dlPA = (DropDownList)e.Row.FindControl("dlPA");

                string _PAcustName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
                string PAquery = "select PA from PA where Business='" + _PAcustName + "' order by PA";
                SqlCommand PAcmd = new SqlCommand(PAquery);
                dlPA.DataSource = GetData(PAcmd);
                dlPA.DataTextField = "PA";
                dlPA.DataValueField = "PA";
                dlPA.DataBind();
                dlPA.Items.FindByValue((e.Row.FindControl("lblPA") as Label).Text).Selected = true;

                DropDownList dlET = (DropDownList)e.Row.FindControl("dlET");

                string ETquery = "select distinct ErrorType from ErrorType order by ErrorType";
                SqlCommand ETcmd = new SqlCommand(ETquery);
                dlET.DataSource = GetData(ETcmd);
                dlET.DataTextField = "ErrorType";
                dlET.DataValueField = "ErrorType";
                dlET.DataBind();
                dlET.Items.FindByValue((e.Row.FindControl("lblET") as Label).Text).Selected = true;

                DropDownList dlAA = (DropDownList)e.Row.FindControl("dlAA");

                string AAquery = "select distinct AAA from ActualAgencyError";
                SqlCommand AAcmd = new SqlCommand(AAquery);
                dlAA.DataSource = GetData(AAcmd);
                dlAA.DataTextField = "AAA";
                dlAA.DataValueField = "AAA";
                dlAA.DataBind();
                dlAA.Items.FindByValue((e.Row.FindControl("lblAA") as Label).Text).Selected = true;

            }
        }

        protected void UpdateSummary(object sender, GridViewUpdateEventArgs e)
        {

            string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
            string ET = (gvSummary.Rows[e.RowIndex].FindControl("dlET") as DropDownList).SelectedItem.Value;
            string AA = (gvSummary.Rows[e.RowIndex].FindControl("dlAA") as DropDownList).SelectedItem.Value;
            string PA = (gvSummary.Rows[e.RowIndex].FindControl("dlPA") as DropDownList).SelectedValue;            
            string AnnotationNumber = gvSummary.DataKeys[e.RowIndex].Value.ToString();
            string sgkComments = (gvSummary.Rows[e.RowIndex].FindControl("tbsgkComm") as TextBox).Text;
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                string query = "update vw_GridviewSource set [BusinessUnit] = @BU, [ErrorType] = @ET, [sgkComments] = @sgk, [ActualAgencyError] = @AA, [PA] = @PA where [AnnotationNumber] = @AnnoNum";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@BU", BU);
                    cmd.Parameters.AddWithValue("@AnnoNum", AnnotationNumber);
                    cmd.Parameters.AddWithValue("@ET", ET);
                    cmd.Parameters.AddWithValue("@AA", AA);
                    cmd.Parameters.AddWithValue("@sgk", sgkComments);
                    cmd.Parameters.AddWithValue("@PA", PA);


                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }

            //gvSummary.DataBind();
            BindData();
        }




        private void BindData()
        {

            if (MyVariables.JN != "" && MyVariables.ST != "" && MyVariables.AT != "")
            {
                dlJobName.SelectedValue = MyVariables.JN;
                dlJobName.DataBind();
                dlStage.SelectedValue = MyVariables.ST;
                dlStage.DataBind();
                dlAnnoType.SelectedValue = MyVariables.AT;
                dlAnnoType.DataBind();
            }

            String conString = System.Configuration.ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            string query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource order by [Page_ID]";

            SqlCommand cmd = new SqlCommand();

            if (dlJobName.SelectedValue != "" & dlStage.SelectedValue != "")
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
                //cmd.Parameters.AddWithValue("@Name", dlJobName.SelectedValue);
                //cmd.Parameters.AddWithValue("@Stage", dlStage.SelectedValue);
            }

            if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue != "" && dlStage.SelectedValue != ""))
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' and Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
            }

            if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue.Length < 2 && dlStage.SelectedValue.Length < 2) )
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' order by [Page_ID]";
            }


            cmd.CommandText = query;
            SqlConnection con = new SqlConnection(conString);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            gvSummary.DataSource = ds;
            gvSummary.PageIndex = MyVariables.PI;
            gvSummary.DataBind();
            //string @Name;
            //string @Stage;

            //@Name = dlJobName.SelectedValue;
            //@Stage = dlStage.SelectedValue;

            //string query = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust from vw_GridviewSource where Name = '" + @Name + "' and AnnotationDate = '" + @Stage + "'";
            //SqlCommand cmd = new SqlCommand(query);
            //gvSummary.DataSource = GetData(cmd);
            //gvSummary.DataBind();
        }

        private DataTable GetData(SqlCommand cmd)
        {
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        protected void dlJobName_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.JN = dlJobName.SelectedValue;
            dlStage.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            gvSummary.DataBind();
            BindData();
        }

        protected void dlStage_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.JN = dlJobName.SelectedValue;
            //dlStage.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            MyVariables.AT = dlAnnoType.SelectedValue;
            gvSummary.DataBind();
            BindData();
        }
        protected void dlAnnoType_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.AT = dlAnnoType.SelectedValue;
            MyVariables.JN = dlJobName.SelectedValue;
            //dlStage.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            gvSummary.DataBind();
            BindData();
        }
    }
}
4

1 回答 1

0

您只显示服务器端代码,在服务器端进行检查并在客户端显示消息更复杂。通过javascript在客户端检查的简单示例:

    <script type="text/javascript">
            function MyConfirm() {
                if (document.getElementById("tb1").value == '')
                    if (confirm("Comment section blank, continue?"))
                        return true;
                    else
                        return false;
                else true;
            }
        </script>
        <asp:TextBox runat="server" ID="tb1" />
        <asp:Button runat="server" ID="btn1" onclick="btn1_Click" OnClientClick="return MyConfirm();" Text="Save" />
于 2013-10-21T17:18:20.467 回答