Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a button (btn_del) on my masterpage (MasterPage.master) and whose visibility is initially set to false. I have a contentpage (inbox.aspx) on which I have a gridview (gv_inbox). I have a checkbox (CheckBox_selall) on the header of gv_inbox, to select all the checkboxes of the rows, when checked. Also when I check the CheckBox_selall, I need to set the visibility of btn_del to true and to false when I uncheck CheckBox_selall.
I have tried a code but it is not changing the visibility of btn_del.. I am new to c#.net

Please help me in this regard.. Thanx in advance..:) My code is shown below.
C#
protected void CheckBox_selall_CheckedChanged(object sender, EventArgs e)
    {
        Button btn = this.Master.FindControl("btn_del") as Button;
               
         CheckBox chkAll = (CheckBox)gv_inbox.HeaderRow.FindControl("CheckBox_selall");
         if (chkAll.Checked == true)
         {             
             btn.Visible = true;
             foreach (GridViewRow gvRow in gv_inbox.Rows)
             {
                 CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
                 chkSel.Checked = true;
             }
         }
         else
         {
             btn.Visible = false;
             foreach (GridViewRow gvRow in gv_inbox.Rows)
             {
                 CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
                 chkSel.Checked = false;
             }
         }
    }
Posted
Updated 12-Sep-12 21:56pm
v4

Button deletebutton=(Button)this.Page.Master.FindControl("btn_del")).Visible = true;


or more defensive approch

C#
<pre> Button deletebutton=(Button)this.Page.Master.FindControl("btn_del"))
 if (deletebutton !=null)
{
deletebutton.Visible=false;
}
 
Share this answer
 
Comments
vj.bvnair 13-Sep-12 4:04am    
I am not getting a null value for the button in my contentpage.. Is there anything to do with the postback property?? Thank you..
Rickin Kane 13-Sep-12 5:06am    
no i am just checking for defensive purpose , is the code working or giving you a error
vj.bvnair 13-Sep-12 5:12am    
There is no error... I ran the code by putting a breaker.. It sets btn.visible=false to btn.visible=true in the if condition.. But the button is not getting displayed..
Rickin Kane 13-Sep-12 5:25am    
do u have any update panel on the page or master page , can u show the code u implemented with above solution
vj.bvnair 13-Sep-12 8:08am    
I don't have any update panel on any of the pages..

this is my inbox.aspx code..


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.Sql;
using System.Data.SqlClient;



public partial class _Default : System.Web.UI.Page
{

SqlConnection con = new SqlConnection("Data Source=USER-23490BFF83\\SQLEXPRESS;Initial Catalog=MailDB;Integrated Security=True;");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
show();

}

}
private void show()
{
int id = Convert.ToInt32(Session["userId"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from Contents where UserId='" + id + "' and Status='" + 1 + "' ORDER BY Date DESC", con);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
lbl_inbox_dis.Text = "There is no data in your Inbox!";
}
else
{
gv_inbox.DataSource = ds;
gv_inbox.DataBind();
con.Close();
}
}

protected void gv_inbox_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowIndex >= 0)
{
if ((e.Row.RowState) == (DataControlRowState.Normal) || (e.Row.RowState) == (DataControlRowState.Alternate))
{
Label read = (Label)e.Row.FindControl("lblReadstatus");
if (read.Text == "True")
{
e.Row.BackColor = System.Drawing.Color.LightGray;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
}


}

}

}


protected void lnk_subject_Click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnk.Parent.Parent;
int index = row.RowIndex;
string id = (gv_inbox.Rows[index].FindControl("lblID") as Label).Text.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE Contents SET ReadStatus='" + 1 + "' WHERE ID='" + id + "'", con);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("openmail.aspx?value="+id);

}

protected void CheckBox_selall_CheckedChanged(object sender, EventArgs e)
{
Button btn = this.Master.FindControl("btn_del") as Button;

CheckBox chkAll = (CheckBox)gv_inbox.HeaderRow.FindControl("CheckBox_selall");
if (chkAll.Checked == true)
{
btn.Visible = true;
foreach (GridViewRow gvRow in gv_inbox.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
chkSel.Checked = true;
}
}
else
{
btn.Visible = false;
foreach (GridViewRow gvRow in gv_inbox.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
chkSel.Checked = false;
}
}
}
protected void gv_inbox_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv_inbox.PageIndex = e.NewPageIndex;
show();
}
}
Hi,
At run time, the master page is merged with the content page, but the controls are not directly accessible as master page members because they are protected. The server controls on a master page are local to the master page, which are not accessible by a content page. To make them accessible, the server controls need to be exposed as public properties or methods. Please check the code below.

In your master page make the button visibility to 'false'
XML
<asp:Button ID="btnMaster" runat="server" Text="Show Me" Visible="false" />

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>



In the masterpage.cs page, create public property to change the button's visibility.
C#
public bool VisibleValue
        {
            set { btnMaster.Visible = value; }
        }


Now in the inbox.aspx page, on the checkbox checked changed event call the masterpage property and set the value to 'true'. This will set the buutton's visibility to 'true' in the master page.
Master property is of type System.Web.UI.Page, and that does not contain properties of your derived class. So you can use the below code.

C#
protected void  
CheckBox_selall_CheckedChanged(object sender, EventArgs e)
        {
            MasterPage mymaster = Master as MasterPage;
            mymaster .VisibleValue= true;
        }


Now, when you run the code you can see that the button becomes visible when the checkbox is checked.

Happy coding.. :)
 
Share this answer
 
Comments
vj.bvnair 13-Sep-12 8:18am    
Thank you for your response.. I used the code you mentioned.. But still the button is not getting displayed..:(.. Does it have anything to do with the postback property??
SruthiR 13-Sep-12 13:37pm    
I wonder why this code does not work for you. It works perfect!! Could you please show the code.
Use the below code , i checked on my side , its working , i am able to find the control , check if its working , else debug and find out if its goes inside chkAll.Checked method




protected void CheckBox_selall_CheckedChanged(object sender, EventArgs e)
{
    Button btn = (Button)this.Page.Master.FindControl("btn_del");

     CheckBox chkAll = (CheckBox)gv_inbox.HeaderRow.FindControl("CheckBox_selall");
     if (chkAll.Checked)
     {
         btn.Visible = true;
         foreach (GridViewRow gvRow in gv_inbox.Rows)
         {
             CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
             chkSel.Checked = true;
         }
     }
     else
     {
         btn.Visible = false;
         foreach (GridViewRow gvRow in gv_inbox.Rows)
         {
             CheckBox chkSel = (CheckBox)gvRow.FindControl("chkbx_sel");
             chkSel.Checked = false;
         }
     }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900