kantagrawal Ответов: 1

Событие rowcommand Gridview, работающее с кодом позади, но не отражающее результат для пользователя.


Я использую gridview для отображения деталей, событие rowcommand для редактирования и удаления строк. в коде позади, во время шага отладки код работает правильно и удаляет значения из базы данных. но это не отражается на пользователе.

Что я уже пробовал:

// код gridview на странице aspx
<asp:GridView ID="GridDetails" runat="server" CssClass="GridviewSearch" BackColor="White"BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" TabIndex="11"

CellPadding="3" ForeColor="Black" GridLines="Vertical" OnRowCommand="GridDetails_RowCommand"                                      OnRowDataBound="GridDetails_RowDataBound" ><Columns>
<asp:TemplateField HeaderText="Action" >
<ItemTemplate><asp:LinkButton ID="LBtnEdit" runat="server" ForeColor="#FF944D" CommandName="Edit"

Text="Edit" CausesValidation="false" CssClass="gridCommand"></asp:LinkButton>
<asp:LinkButton ID="LBtnDelete" runat="server" ForeColor="#FF944D" CommandName="Delete"

Text="Delete" CausesValidation="false" CssClass="gridCommand"></asp:LinkButton>
</ItemTemplate></asp:TemplateField>
</Columns><FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle ForeColor="White" BackColor="Black" Font-Bold="True"/>
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>



//код на странице. cs


protected void GridDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int RowIndex = row.RowIndex;

            IDofSelectedItem.Value = row.Cells[1].Text;

            if (e.CommandName == "Edit")
            {
                PnlAddTask.Visible = true;
                if ((TxtTaskDetail.Text.Trim() != "") || (TxtCompletedOn.Text.Trim() != ""))
                {
                    TxtTaskDetail.Focus();
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('There is already some data. So, first save it.')", true);
                    return;
                }

                BtnSubmit.Text = "Update";
                TxtTaskDetail.Text = row.Cells[2].Text;
                TxtCompletedOn.Text = row.Cells[3].Text;
                //TxtTaskDetail.Focus();
            }

            if (e.CommandName == "Delete")
            {
                if (con.State == ConnectionState.Closed)
                { con.Open(); }

                IsInTransaction = true;
                trans = con.BeginTransaction();
                cmd = new SqlCommand("Delete from TblTaskManager where TaskID = " + Convert.ToInt16(IDofSelectedItem.Value) + " ", con, trans);
                cmd.ExecuteNonQuery();
                trans.Commit();
                IsInTransaction = false;
                con.Close();

                ChangeLinkButtonText();
                if (ClickOn == "UpComming")
                {
                    LBtnUpComming_Click(sender, e);
                }
                else if (ClickOn == "Pending")
                {
                    LBtnPending_Click(sender, e);
                }
                else if (ClickOn == "Completed")
                {
                    LBtnCompleted_Click(sender, e);
                }

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Delete Successfully.');", true);
            }
    }



protected void LBtnUpComming_Click(object sender, EventArgs e)
    {
            LBtnUpComming.ForeColor = System.Drawing.Color.Red;
            LBtnPending.ForeColor = System.Drawing.Color.Blue;
            LBtnCompleted.ForeColor = System.Drawing.Color.Blue;

            MyFunction.FillGridView("Select TaskID, Task As 'Task Detail', CONVERT(varchar(20),CompletedOn, 106) As 'Completed On' from TblTaskManager Where EmployeeID = " + ClsCommonVariable.LoginID + " and CompletedOn >= '" + DateTime.Now.Date + "' and Completed = 'False' Order by CompletedOn Asc", ref GridDetails);
            if (GridDetails.Rows.Count == 0)
            {
                PnlForGrid.Visible = false;
            }
            else
            {
                PnlForGrid.Visible = true;
            }
    }



void ChangeLinkButtonText()
    {
        if (con.State == ConnectionState.Closed)
        { con.Open(); }

        cmd = new SqlCommand("Select Count(*) from TblTaskManager Where EmployeeID = " + ClsCommonVariable.LoginID + " and CompletedOn >= '" + DateTime.Now.Date + "' and Completed = 'False'", con);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            LBtnUpComming.Text = "UpComming (" + dr[0].ToString() + ")";
        }
        else
        {
            LBtnUpComming.Text = "UpComming";
        }
        dr.Close();
        con.Close();
    }

1 Ответов

Рейтинг:
2

Mohtshm Zubair

Если изменения не отражаются на интерфейсе, в то время как были сделаны в БД, то вы должны установить источник данных, а затем снова вызвать databind() в событии row_command

GridDetails.DataSource = ...;
GridDetails.DataBind();