Show Hide Button based on condition using C# in ASP.Net

Last Reply 20 days ago By itsme

Posted 20 days ago

I am trying to show update button if company has filled his profile form once after login but when company has not filled profile form not at all then show button should be shown to the company.

    public partial class Company_profile : System.Web.UI.Page
    {
        SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ToString());
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select Name,email from Common_regn2 where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cname.Text = dr["Name"].ToString();
                        cname.ReadOnly = true;
                        txt_mail.Text = dr["email"].ToString();
                        txt_mail.ReadOnly = true;
                    }                       
                }               
                Session["name"] = cname.Text.ToString();
            }            
        }
    }

 

Posted 20 days ago

I found the answer

Check the below code:

 <div style="scrollbar-highlight-color:aqua" onscroll="true">
     <table align="center" style="position: relative; top: 20px;margin-left:35%;border-style:double;align-content:center">
            <tr>
                <td>
                    <table align="center">
                        <tr>
                            <td>
                                <b>Company Name</b>
                            </td>
                            <td>
                                <asp:TextBox ID="cname" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                               <b>City:</b>
                            </td>
                            <td>
                                <asp:DropDownList ID="city" runat="server" class="form-control input-lg">
                                    <asp:ListItem>--Select City--</asp:ListItem>
                                    <asp:ListItem>Anand</asp:ListItem>
                                    <asp:ListItem>Surat</asp:ListItem>
                                    <asp:ListItem>Ahmedabad</asp:ListItem>
                                    <asp:ListItem>Gandhinagar</asp:ListItem>
                                    <asp:ListItem>Rajkot</asp:ListItem>
                                    <asp:ListItem>Jamnagar</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Phone no.:</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_no" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <b>Contact Person:</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_Contact" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <b>Address:</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_address" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <b>Email:</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_mail" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                            </td>
                        </tr>
                        
                        <tr>
                            <td colspan="2" align="center">
                                <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" CssClass="btn btn-info" />
                                <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" CssClass="btn btn-info" Visible="false" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <br />
                    <asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="Blue"></asp:Label>
                </td>
            </tr>
        </table>
       <input type="hidden" runat="server" id="Id" />
   </div>

 

 public partial class Company_profile : System.Web.UI.Page
    {
        SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ToString());
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select Name,email from Common_regn2 where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cname.Text = dr["Name"].ToString();
                        cname.ReadOnly = true;
                        txt_mail.Text = dr["email"].ToString();
                        txt_mail.ReadOnly = true;
                    }                       
                }               
                Session["name"] = cname.Text.ToString();
            }

            if (!IsPostBack)
            {
                mycon.Close();
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select City,phone,Contact_person,Address from Company_profile where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        city.SelectedValue = dr["City"].ToString();
                        txt_no.Text = dr["phone"].ToString();
                        txt_Contact.Text = dr["Contact_person"].ToString();
                        txt_address.Text = dr["Address"].ToString();
                        btnsave.Visible = false;
                        btnUpdate.Visible = true;
                    }
                }
            }

        }
        
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("UPDATE Company_profile SET Company_name=@Company_name,City=@City,phone=@phone,Contact_person=@Contact_person,Address=@Address WHERE email='" + Session["mail"].ToString() + "'", con);
            cmd.Parameters.AddWithValue("@Id", Id.Value);
            cmd.Parameters.AddWithValue("@Company_name", cname.Text);
            cmd.Parameters.AddWithValue("@City", city.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@phone", txt_no.Text);
            cmd.Parameters.AddWithValue("@Contact_person", txt_Contact.Text);
            cmd.Parameters.AddWithValue("@Address", txt_address.Text);
            cmd.Parameters.AddWithValue("@email", txt_mail.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblMessage.Text = "Updated Successfully.";
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("Insert into Company_profile (Company_name,City,phone,Contact_person,Address,email) Values (@Company_name,@City,@phone,@Contact_person,@Address,@email)", con);
            cmd.Parameters.AddWithValue("@Id", Id.Value);
            cmd.Parameters.AddWithValue("@Company_name", cname.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@City", city.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@phone", txt_no.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@Contact_person", txt_Contact.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@Address", txt_address.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@email", txt_mail.Text.Trim().ToString());
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblMessage.Text = "Saved Successfully.";

        }
    }