【问题标题】:How to refresh all label after deleting a record in the gridview?删除gridview中的记录后如何刷新所有标签?
【发布时间】:2014-05-11 14:06:13
【问题描述】:

在左侧,我有 3 个标签,根据数据库显示数据。我想每当我在gridview中删除一条记录时,标签都会自动刷新。我不想点击浏览器刷新按钮来刷新标签。我只想刷新标签。我已经创建了一个名为UpdateLabel() 的方法,并将它放在grdEvent_RowUpdating 中,它可以工作,每当我编辑时,它都会刷新标签。但是如果我把UpdateLabel()方法放在grdEvent_RowDeleting里面就不行了,为什么?帮助。帮助

点击gridview上的delete按钮,会弹出这个

点击弹出窗口中的确定按钮后。

点击刷新后,标签会刷新

protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            UpdateLabels(); // Update labels without refreshing page

            string strConnString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
            string str;
            SqlCommand com;

            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select * from EVENT_ANNOUNCE where getdate() >= STARTDATE and cast(getdate() as Date) <= ENDDATE";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();



            var events = new List<string>();

            if (reader.HasRows)
            {
                while (reader.Read())
                    events.Add(reader["EVENTNAME"].ToString());
            }

            if (events.Count >= 1)
                lblEvent1.Text = events[0];
            if (events.Count >= 2)
                lblEvent2.Text = events[1];
            if (events.Count >= 3)
                lblEvent3.Text = events[2];

            reader.Close();
            con.Close();

        }


        if (Page.IsPostBack == false)
        {
            bindEventGridView();

        }



    }

    // Update labels without refreshing page
    public void UpdateLabels()
    {

        string strConnString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
        string str;
        SqlCommand com;

        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        str = "select * from EVENT_ANNOUNCE where getdate() >= STARTDATE and cast(getdate() as Date) <= ENDDATE";
        com = new SqlCommand(str, con);
        SqlDataReader reader = com.ExecuteReader();

        var events = new List<string>();

        if (reader.HasRows)
        {
            while (reader.Read())
                events.Add(reader["EVENTNAME"].ToString());
        }

        if (events.Count >= 1)
            lblEvent1.Text = events[0];
        if (events.Count >= 2)
            lblEvent2.Text = events[1];
        if (events.Count >= 3)
            lblEvent3.Text = events[2];

        reader.Close();
        con.Close();
    }

    protected void grdEvent_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // get row selected by user
        int selectedRow = e.RowIndex;
        int ID = (int)grdEvent.DataKeys[selectedRow].Value;
        // Delete Record
        deleteEventRecord(ID);
        UpdateLabels();
    }

    private void deleteEventRecord(int ID)
    {

        string strConnectionString =
                ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "DELETE EVENT_ANNOUNCE WHERE ID=@ID";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        cmd.Parameters.AddWithValue("ID", ID);

        myConnect.Open();
        int result = cmd.ExecuteNonQuery();

        if (result > 0)
        {
            lblSuccess.Visible = true;
            lblSuccess.Text = "Record deleted";
            lblError.Visible = false;
        }
        else
        {
            lblError.Visible = true;
            lblError.Text = "Update fail";
            lblSuccess.Visible = false;
        }

        bindEventGridView();
        myConnect.Close();

    }

    protected void grdEvent_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdEvent.EditIndex = e.NewEditIndex;
        bindEventGridView();
    }

    protected void grdEvent_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int selectedRow = e.RowIndex;   //get selected row
        //  get product id from data key
        int id = (int)grdEvent.DataKeys[selectedRow].Value;

        //  get current grid view row
        GridViewRow row = (GridViewRow)grdEvent.Rows[selectedRow];
        TextBox eventtype = (TextBox)row.FindControl("txtEventType");
        //  find text box for txtPrice
        TextBox eventname = (TextBox)row.FindControl("txtEventName");
        TextBox startdate = (TextBox)row.FindControl("txtStartDate");
        TextBox enddate = (TextBox)row.FindControl("txtEndDate");
        //  Remove $ sign
        string strEventType = eventtype.Text;
        string strEventName = eventname.Text;
        string strStartDate = startdate.Text;
        string strEndDate = enddate.Text;
        DateTime datStartDate;
        DateTime datEndDate;
        if (DateTime.TryParseExact(strStartDate, new string[] { "dd/MM/yyyy" },
                               System.Globalization.CultureInfo.InvariantCulture,
                               System.Globalization.DateTimeStyles.None, out datStartDate)
    &&
    DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
                               System.Globalization.CultureInfo.InvariantCulture,
                               System.Globalization.DateTimeStyles.None, out datEndDate)
   )
        {
            updateEventGridviewRecord(id, strEventType, strEventName, datStartDate, datEndDate);
        }

            /*
             || DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
                               System.Globalization.CultureInfo.InvariantCulture,
                               System.Globalization.DateTimeStyles.None, out datEndDate
             */

        else
        {
            lblError.Visible = true;
            lblError.Text = "Invalid Date";
            lblSuccess.Visible = false;
        }
        UpdateLabels();
    }

    private void updateEventGridviewRecord(int id, string strEventType, string strEventName, DateTime datStartDate, DateTime datEndDate)
    {
        try
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
            SqlConnection myConnect = new SqlConnection(strConnectionString);

            string strCommandText = "UPDATE EVENT_ANNOUNCE SET [EVENTTYPE]=@EVENTTYPE, [EVENTNAME]=@EVENTNAME, [STARTDATE]=@STARTDATE, [ENDDATE]=@ENDDATE WHERE [ID]=@ID";

            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@EVENTTYPE", strEventType);
            cmd.Parameters.AddWithValue("@EVENTNAME", strEventName);
            cmd.Parameters.AddWithValue("@STARTDATE", datStartDate);
            cmd.Parameters.AddWithValue("@ENDDATE", datEndDate);
            myConnect.Open();

            int result = cmd.ExecuteNonQuery();

            if (result > 0)
            {
                lblSuccess.Visible = true;
                lblSuccess.Text = "Record updated!";
                lblError.Visible = false;
            }
            else
            {
                lblSuccess.Visible = true;
                lblError.Text = "Update fail";
                lblError.Visible = false;
            }

            myConnect.Close();


            //Cancel Edit Mode
            grdEvent.EditIndex = -1;
            bindEventGridView();
        }

        catch
        {
            lblError.Visible = true;
            lblError.Text = "Please Enter Approximate data";
            lblSuccess.Visible = false;
        }
    }

【问题讨论】:

    标签: c# asp.net gridview webforms


    【解决方案1】:

    将左侧面板上的标签包装在自己的更新面板中。

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Label ID="LabelEvent1" runat="server" Width="99%" />
    <asp:Label ID="LabelEvent2" runat="server" Width="99%" />
    <asp:Label ID="LabelEvent3" runat="server" Width="99%" />
       </contenttemplate>
    </asp:UpdatePanel>
    

    在您的已删除事件中调用您的 updateLabels 方法

     protected void grdEvent_RowDeleted(object sender, GridViewEditEventArgs e)
    {
       if(e.Exception == null)
    {
        UpdateLabels();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2011-11-06
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多