locked
when clicked on gridview button another button is hide only one button show one time? RRS feed

  • Question

  • User-1026236167 posted

    Hello

    in my code i have two requirements i describe

    1 when page is load only one button save button is show and update button is hide in that stage

    2 when clicked on gridview edit button save button is hide and update button is show

    i dont understand this code please execute them?

    css

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebApplication14
    {
    public partial class WebForm61 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();


    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }


    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FillGridview();

    }
    }

    public void FillGridview()
    {
    SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_package", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    GridView1.DataSource = DT;
    GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    TextBox2.Text = gvr.Cells[0].Text;
    TextBox3.Text = gvr.Cells[1].Text;
    TextBox4.Text = gvr.Cells[2].Text;

    }


    protected void Button2_Click(object sender, EventArgs e)
    {
    EstablishConnection("sp_insert_package");
    cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 255).Value = TextBox2.Text;
    cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = TextBox3.Text;
    cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 255).Value = TextBox4.Text;
    cmd.Parameters.Add("@by_whom", SqlDbType.VarChar, 255).Value = "1";
    cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 255).Value = System.DateTime.Now.ToString();
    cmd.Parameters.Add("@status", SqlDbType.VarChar, 255).Value = "1";

    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true);
    try { cmd.ExecuteNonQuery(); }
    catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }

    FillGridview();

    con.Close();
    CloseConnection();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
    EstablishConnection("sp_update_package");
    cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 255).Value = TextBox2.Text;
    cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = TextBox3.Text;
    cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 255).Value = TextBox4.Text;
    cmd.Parameters.Add("@by_whom", SqlDbType.VarChar, 255).Value = "1";
    cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 255).Value = System.DateTime.Now.ToString();
    cmd.Parameters.Add("@status", SqlDbType.VarChar, 255).Value = "1";

    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true);
    try { cmd.ExecuteNonQuery(); }
    catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }

    FillGridview();

    con.Close();
    CloseConnection();
    }

    }
    }

    aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm61.aspx.cs" Inherits="WebApplication14.WebForm61" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">

    <div >
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

    <table>

    <tr>

    <td><span style="margin-left:20px">Package name</span><br />
    <span style="margin-left:20px"><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </span>
    </td>

    <td ><span style="margin-left:100px">Reward</span><br />
    <span style="margin-left:100px"><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </span>
    </td>

    <td><span style="margin-left:100px">Remarks</span><br />
    <span style="margin-left:100px"><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    </span>
    </td>
    </tr>

    </table>

    <br />

    <br />

    <asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
    <asp:Button ID="Button3" runat="server" Text="update" OnClick="Button3_Click" />
    <br />
    <br />

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="both" AutoGenerateColumns="False"
    DataKeyNames="tbl_id" Height="264px" Width="100%"
    BorderColor="#FF0066" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>

    <asp:BoundField DataField="package_name" HeaderText="Package name" />
    <asp:BoundField DataField="reward" HeaderText="Reward" />
    <asp:BoundField DataField="remarks" HeaderText="Remarks" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID="Button1" runat="server" Text="Edit" OnClick="Button1_Click"/>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    <SelectedRowStyle BackColor="#FF66FF" />
    </asp:GridView>
    &nbsp;&nbsp;&nbsp;&nbsp;

    </ContentTemplate>
    </asp:UpdatePanel>

    </div>


    </asp:Content>

    Sunday, June 7, 2020 9:48 AM

Answers

  • User-719153870 posted

    Hi prabhjot1313,

    prabhjot1313

    1 when page is load only one button save button is show and update button is hide in that stage

    2 when clicked on gridview edit button save button is hide and update button is show

    To Hide and Show a control in Webform project is easy by changing this control's Visible attribute.

    In your case, change them in below two methods:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    FillGridview();
                    Button3.Visible = false;
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                TextBox2.Text = gvr.Cells[0].Text;
                TextBox3.Text = gvr.Cells[1].Text;
                TextBox4.Text = gvr.Cells[2].Text;
                Button2.Visible = false;
                Button3.Visible = true;
            }

    prabhjot1313

    i dont understand this code please execute them?

    The code is simply implementing ADO.NET with Stored procedure, please check ADO.NET code examples and Calling Stored procedures in ADO.NET for more detailed information.

    In your code sample, it takes above operation to 3 parts:

    1. EstablishConnection to open connection with db;
    2. Page events(Button2_Click, Button3_Click and FillGridview) to implement secific operations with db;
    3. CloseConnection to close the connection with db.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 2:28 AM

All replies

  • User-943250815 posted

    There are better ways to do what you want, check here for sample that might help you https://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx

    But if you can not change your code, you have to change button visibility at each stage.

    Sunday, June 7, 2020 3:35 PM
  • User-719153870 posted

    Hi prabhjot1313,

    prabhjot1313

    1 when page is load only one button save button is show and update button is hide in that stage

    2 when clicked on gridview edit button save button is hide and update button is show

    To Hide and Show a control in Webform project is easy by changing this control's Visible attribute.

    In your case, change them in below two methods:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    FillGridview();
                    Button3.Visible = false;
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                TextBox2.Text = gvr.Cells[0].Text;
                TextBox3.Text = gvr.Cells[1].Text;
                TextBox4.Text = gvr.Cells[2].Text;
                Button2.Visible = false;
                Button3.Visible = true;
            }

    prabhjot1313

    i dont understand this code please execute them?

    The code is simply implementing ADO.NET with Stored procedure, please check ADO.NET code examples and Calling Stored procedures in ADO.NET for more detailed information.

    In your code sample, it takes above operation to 3 parts:

    1. EstablishConnection to open connection with db;
    2. Page events(Button2_Click, Button3_Click and FillGridview) to implement secific operations with db;
    3. CloseConnection to close the connection with db.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 2:28 AM