Hi there, after struggeling so much for a simple proble I really request you to put your that task side for a while and study the follwing code in detail.
Your master Page would be like
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder></head><body>
<form id="form1" runat="server"> <div>
<asp:LinkButton ID="LinkButtonExport" Text="Export to Excel" runat="server" OnClick="LinkButtonExport_Click" Visible="false"/>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder> </div> </form>
</body></html>
Then your Create page ShowMasterPageLink.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
<asp:Button runat="server" Text="With Data" onclick="Unnamed1_Click" CommandArgument="WithData"/>
<asp:Button runat="server" Text="NoData" onclick="Unnamed1_Click" CommandArgument="NoData" />
</asp:Content>
and in the Code of this page appli the following code.
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) { bindGrid("withData");
//bindGrid("noData"); } }
protected void Unnamed1_Click(object sender, EventArgs e) {
bindGrid((sender as Button).CommandArgument);
//Page.Master.FindControl("LinkButtonExport").Visible = true;
} private void bindGrid(string binder) {
DataTable dt = new DataTable();
dt.Columns.Add("Column1"); dt.Columns.Add("Column2");
dt.Columns.Add("Column3"); dt.Rows.Add("a", "b", "c");
dt.Rows.Add("e", "r", "t"); dt.Rows.Add("g", "f", "s");
GridView1.DataSource = new DataTable();
//Your grid view bindig code you may not necessarilly have the same ID of the girdView as
// "GridView1" so replace it as per requirement.
if (binder == "WithData") {
GridView1.DataSource = dt; } else
{ GridView1.DataSource = new DataTable();
} GridView1.DataBind();
// The line of code which make the link visible only if data is available in the Grid.
Page.Master.FindControl("LinkButtonExport").Visible = GridView1.Rows.Count > 0;
}
there are two statements in the PageLoad one of them is commented you may toggel them to study. study this article and tell me that is it working or not ...?