Member 12324523 Ответов: 0

Нужно дважды нажать на кнопку, чтобы отредактировать и обновить данные gridview


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

`<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: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


    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


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

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

0 Ответов