Gridview的rowcommand事件处理后面的代码但是没有将结果反映给用户。 [英] Gridview's rowcommand event working on code behind but, does not reflect the result to user.

查看:58
本文介绍了Gridview的rowcommand事件处理后面的代码但是没有将结果反映给用户。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gridview显示详细信息,使用rowcommand事件编辑和删除行。在后面的代码中,在步骤调试期间代码正常工作并从数据库中删除值。但它并没有反映给用户。



我尝试过:



// aspx页面中的gridview代码

I am using gridview for display detail, rowcommand event for edit and delete rows. in code behind, during step debugging code is working properly and deletes values from database. but it doesnot reflect to user.

What I have tried:

//gridview code in aspx page

<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页面代码








//code in .cs page


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();
    }

推荐答案

如果在DB中完成后没有在前端反映更改,则必须设置数据源和然后在row_command事件中再次调用databind()



If changes are not reflecting on front end while have been done in DB then you have to set datasource and then call databind() again in the row_command event

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


这篇关于Gridview的rowcommand事件处理后面的代码但是没有将结果反映给用户。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆