简体   繁体   English

需要单击两次按钮以编辑和更新gridview数据

[英]Need to click the button twice to edit and update gridview data

In my application i need to edit the gridview rows on clicking the edit button of that particular row everything works fine edit update and cancel the problem is when i click edit button my gridview disappears again to see the gridview i should click the below button 在我的应用程序中,我需要在单击特定行的编辑按钮时编辑gridview行,一切正常,可以进行编辑更新,并取消问题是当我单击编辑按钮时,我的gridview再次消失以查看gridview,我应该单击以下按钮

<asp:Button ID="Button1" runat="server" style="border:1px solid #456879;border-radius:5px;height: 22px;Width:150px" OnClick="Button1_Click" Text="Get Uploaded Data" Width="132px" /> and similarly after updating my gridview disappears but update happens so to again see the gridview i should again click the above button Get Uploaded Data,Finally in order to edit gridview data i need to click the button twice and in order to see the updated data again i need to click the button twice.How can i solve this <asp:Button ID="Button1" runat="server" style="border:1px solid #456879;border-radius:5px;height: 22px;Width:150px" OnClick="Button1_Click" Text="Get Uploaded Data" Width="132px" /> ,类似地,更新后,我的gridview消失了,但是更新发生了,所以再次看到gridview时,我应该再次单击上面的按钮Get Uploaded Data,最后,为了编辑gridview数据,我需要单击两次按钮并在为了再次查看更新的数据,我需要单击两次按钮。如何解决此问题?

<asp:GridView ID="GridView2" runat="server" CellPadding="3" Font-Size="12px" Width="300px" Visible="false" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" OnRowCommand="GridView2_RowCommand" OnRowCancelingEdit="GridView2_RowCancelingEdit" AutoGenerateColumns="False"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"> 
                    <Columns> 
                         <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                           <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="Images/icon-edit.png" Height="32px" Width="32px"/>
                        </ItemTemplate>
                        <EditItemTemplate>
                           <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="Images/update1.jpg"/>
                           <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="Images/cancel.jpg"/>
                        </EditItemTemplate>
                    </asp:TemplateField>             
                        <asp:templatefield headertext="sno">
                        <itemtemplate>
                            <asp:label id="lblid" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID") %>'></asp:label>
                        </itemtemplate>
                        <edititemtemplate>           
                            <asp:label id="lbleditid" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID") %>'></asp:label>           
                        </edititemtemplate>
                    </asp:templatefield>    
                      <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtEditName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Name") %>'></asp:TextBox>           
                        </EditItemTemplate>
                    </asp:TemplateField>
                          <asp:TemplateField HeaderText="Salary">
                        <ItemTemplate>
                            <asp:Label ID="lblSalary" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Salary") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtEditSalary" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Salary") %>'></asp:TextBox>           
                        </EditItemTemplate>
                    </asp:TemplateField>
                          <asp:TemplateField HeaderText="Designation">
                        <ItemTemplate>
                            <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtEditDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:TextBox>           
                        </EditItemTemplate>
                    </asp:TemplateField>
                          <asp:TemplateField HeaderText="Location">
                        <ItemTemplate>
                            <asp:Label ID="lblLocation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Location") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtEditLocation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Location") %>'></asp:TextBox>           
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>           
                 </asp:GridView>

.Cs Code .Cs代码

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        GridView2.DataBind();
        GridView2.Visible = true;

    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblEditID = (Label)GridView2.Rows[e.RowIndex].FindControl("lblEditID");
        TextBox txtEditName = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditName");
        TextBox txtEditSalary = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditSalary");
        TextBox txtEditDesignation = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditDesignation");
        TextBox txtEditLocation = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditLocation");
        con.Open();
        string cmdstr = "update CodingLog1 set Name=@Name,Salary=@Salary,Designation=@Designation,Location=@Location where ID=@ID";
        SqlCommand cmd = new SqlCommand(cmdstr, con);
        cmd.Parameters.AddWithValue("@ID", lblEditID.Text);
        cmd.Parameters.AddWithValue("@Name", txtEditName.Text);
        cmd.Parameters.AddWithValue("@Salary", txtEditSalary.Text);
        cmd.Parameters.AddWithValue("@Designation", txtEditDesignation.Text);
        cmd.Parameters.AddWithValue("@Location", txtEditLocation.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView2.EditIndex = -1;
        GridView2.Visible = true;
        GridView2.DataBind();
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
          GridView2.EditIndex = -1;
          GridView2.Visible = true;
          GridView2.DataBind();
    }

Button code- 按钮代码

 protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null; GridView1.DataBind();
        string desi = Session["Role"].ToString();
        string user = Session["Username"].ToString();
        string selecteduser = ddlusers.SelectedItem.Text;

        if (TextBox1.Text != "")
        {
            if (rdupldeddate.Checked == true)
            {
                DataTable dt = adm.GetRecordsByUploadedDate(user, TextBox1.Text, desi, selecteduser);  //Uploaded date

                if (dt.Rows.Count > 0)
                {
                    if (desi == "Supervisor")
                    {
                        BtnExport.Visible = true;
                        GridView2.Visible = true;
                        GridView1.Visible = false;
                        GridView2.DataSource = dt;
                        GridView2.DataBind();
                    }
                    else
                    {
                        GridView2.Visible = false;
                        BtnExport.Visible = false;
                        GridView1.Visible = true;
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
                else { lblMsg.Visible = true; GridView1.Visible = false; GridView2.Visible = false; lblMsg.Text = "No Data Present!!!"; }
            }
            else
            {
                DataTable dt = adm.GetRecordsByCodedDate(user, TextBox1.Text, desi, selecteduser);  //CodedDate
                if (dt.Rows.Count > 0)
                {
                    if (desi == "Supervisor")
                    {
                        BtnExport.Visible = true;
                        GridView2.Visible = true;
                        GridView1.Visible = false;
                        GridView2.DataSource = dt;
                        GridView2.DataBind();
                    }
                    else
                    {
                        GridView2.Visible = false;
                        BtnExport.Visible = false;
                        GridView1.Visible = true;
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
                else { lblMsg.Visible = true; GridView1.Visible = false; GridView2.Visible = false; lblMsg.Text = "No Data Present!!!"; }
            }
        }
        else { lblMsg.Visible = true; lblMsg.Text = "Please Enter Date!!!"; }

    }

I had tried all the possibilties by taking editindex=0 instead of -1, by calling my gridview2.databind in !ispostback moreover i had placed my gridview in update panel also but still its not working 我已经尝试通过采用editindex = 0而不是-1来尝试所有可能性,方法是在!ispostback中调用我的gridview2.databind,而且我也将自己的gridview放在了更新面板中,但仍然无法正常工作

Based on the code you pasted into the comments, the problem lies in your Page_Load event handler. 根据您粘贴到注释中的代码,问题出在您的Page_Load事件处理程序中。 The code that sets the initial visibility of the GridViews should be inside the if (!IsPostBack) block. 设置GridViews的初始可见性的代码应该在if (!IsPostBack)块内。

lblMsg.Text = ""; 

if (!IsPostBack) 
{ 
    BindUsers();

    // Set the initial visibility of grids here
    string desi = Session["Role"].ToString(); 

    if (desi == "Supervisor") 
    { 
        GridView2.Visible = true; 
        GridView1.Visible = false; 
        ddlusers.Visible = true; 
        BtnExport.Visible = false; 
    } 
    else 
    { 
        GridView2.Visible = false; 
        GridView1.Visible = true; 
        ddlusers.Visible = false; 
        BtnExport.Visible = false; 
    }
} 

Once you have set the visibility for those grids on the first page load, that setting will be maintained by the ViewState across postbacks. 设置了第一页加载时这些网格的可见性后,该设置将由ViewState在回发之间维护。 From that point on, it appears that your control events are managing the visibility of the grids (based on whether there are results to show, etc). 从那时起,您的控制事件似乎正在管理网格的可见性(基于是否要显示结果等)。

In your GridView2_RowUpdating, you are updating your data on the database and binding your GridView2 to nothing. 在GridView2_RowUpdating中,您正在更新数据库上的数据并将GridView2绑定为空。 That's why the GridView disappears. 这就是GridView消失的原因。 It has no data to show. 没有要显示的数据。 While the button you click fetch the updated data and binds it to the GridView that's why it shows again. 当您单击按钮时,获取更新的数据并将其绑定到GridView,这就是为什么它再次显示的原因。 When you update the data, you should fetch the updated data and rebind it to your GridView again. 更新数据时,应获取更新的数据,然后再次将其重新绑定到GridView。

I suggest using DataSet and assign as Datasource before calling GridView2.DataBind(); 我建议在调用GridView2.DataBind()之前使用DataSet并指定为Datasource。

SqlDataAdapter da = new SqlDataAdapter(cmdstr ,con);
    da.SelectCommand.CommandType = CommandType.Text;
    da.SelectCommand.Parameters.AddWithValue("@ID", lblEditID.Text);
    da.SelectCommand.Parameters.AddWithValue("@Name", txtEditName.Text);
    da.SelectCommand.Parameters.AddWithValue("@Salary", txtEditSalary.Text);
    da.SelectCommand.Parameters.AddWithValue("@Designation", txtEditDesignation.Text);
    da.SelectCommand.Parameters.AddWithValue("@Location", txtEditLocation.Text);   

DataSet dsResult = new DataSet();

da.Fill(dsResult);
con.Close();

GridView2.DataSource = dsResult;
GridView.DataBind();

Or Call the codes in your button through a method after the update. 或通过更新后的方法调用按钮中的代码。 Try this if this works. 如果可行,请尝试此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM