riki
riki

Reputation: 2403

How to show/hide a button from server side?

I'm new to ASP.NET and C#, I tried few things but did not worked. I have an .aspx page where I have three buttons, as shown in below code:

<body>
<form id="htmlForm" runat="server">  
    <telerik:RadScriptManager ID="rsm" AsyncPostBackTimeout="1800" runat="server" />
    <telerik:RadAjaxPanel ID="rap" runat="server" LoadingPanelID="ralp">
        <table>
            <tr>
                <td colspan="2">
                    Invoice Download
                </td>
            </tr>
            <tr>
                <td>
                    Invoice Start #
                </td>
                <td>
                    <telerik:RadNumericTextBox ID="rntbStart" Runat="server" MaxValue="1000000" MinValue="1000" Width="75px" >
                        <NumberFormat DecimalDigits="0" GroupSeparator="" />
                    </telerik:RadNumericTextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Invoice End #
                </td>
                <td>
                    <telerik:RadNumericTextBox ID="rntbEnd" Runat="server" MaxValue="1000000" MinValue="1000" Width="75px" >
                        <NumberFormat DecimalDigits="0" GroupSeparator="" />
                    </telerik:RadNumericTextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="cmdDownload" runat="server" Text="Download" />
                    <asp:Button ID="cmdDownloadSAP" runat="server" Text="Download SAP" />
                </td>
            </tr>
        </table>
        <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
    </telerik:RadAjaxPanel>
    <telerik:RadAjaxLoadingPanel ID="ralp" Runat="server" Skin="Default" />
    <asp:Button ID="btnConform" Visible="false" runat="server" Text="Conform Download" OnClick="btnConform_Click" />
</form>

I'm trying to do that btnConform will be initially invisible while the page is loading and now if I click cmdDownload or cmdDownloadSAP, button btnConform must get visible. Now if I click again on btnConform it will become invisible.

I cannot use JavaScript because cmdDownload and cmdDownloadSAP have some other task to complete before I make btnConform visible (say database call) and the btnConform will become visible only if the database call is successful.

   partial class XPO_Accounting_InvoiceDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

protected void cmdDownload_Click(object sender, System.EventArgs e)
{
    Session["InvoiceStart"] = rntbStart.Value;
    Session["InvoiceEnd"] = rntbEnd.Value;
    try
    {
        //All DB call
                    btnConform.Visible = true;
    }
    catch (Exception ex)
    {
        lblResult.Text = ex.ToString();
    }
}

protected void cmdDownload0_Click(object sender, System.EventArgs e)
{
    Session["InvoiceStart"] = rntbStart.Value;
    Session["InvoiceEnd"] = rntbEnd.Value;
    try
    {
        //All DB call   
                    btnConform.Visible = true;
    }
    catch (Exception ex)
    {
        lblResult.Text = ex.ToString();
    }
}
override protected void OnInit(EventArgs e)
{
    base.OnInit(e);
    cmdDownload.Click += cmdDownload_Click;
    cmdDownloadSAP.Click += cmdDownload0_Click;
}
protected void btnConform_Click(object sender, EventArgs e)
{
    double InvoiceStart = (double)Session["InvoiceStart"];
    double InvoiceEnd = (double)Session["InvoiceEnd"];

    try
    {
        SqlHelper.ExecuteNonQuery(XPOStaticData.Instance.SQL, CommandType.Text, "update tblInvoiceNum set uploadedtoacct = 'Y' WHERE Status='I' AND InvoiceNum BETWEEN " + InvoiceStart + " AND " + InvoiceEnd + " and uploadedtoacct = 'N'");
        lblResult.Text = "Downloaded Invoiced has conformed as the correct one.";
        btnConform.Visible = false;
    }
    catch (Exception ex)
    {
        lblResult.Text = ex.ToString();
    }
}
}

This does not work. Sorry for change the content of the question (cause older content wasn't pointing the problem properly). This is actual code here I'm trying to do it. (it looks stupid to pest the complete code...)

Upvotes: 0

Views: 3310

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460380

You change visibility with the Visible property:

protected void btnOne_Click(object sender, EventArgs e)
{
    //require code to make it visible
    btnTwo.Visible = true;
}


protected void btnTwo_Click(object sender, EventArgs e)
{
    //require code to make it invisible
    btnTwo.Visible = false;
}

If you want to make a server control initially invisible you also have to use this property, so you can either use codebehind to change visibility programmatically ore declaratively on the aspx-page:

 <asp:Button ID="btnTwo" Visible="false" runat="server" Text="Two" OnClick="btnTwo_Click" />

One final note, Visible=false on serverside means that this control is not rendered at all on client-side. So it does simply not exist and can not be accessed (or made visible) on client-side.

If you need to access it on client-side you should make it invisible via stylesheets either by using

  • display: none(does not take up any space) or
  • visibility: hidden(still takes up space in the layout).

Update acc. to the last edit of your question the event handlers don't seem to be registered anymore.

So you need:

<asp:Button ID="cmdDownload" OnClick="cmdDownload_Click" runat="server" Text="Download" />
<asp:Button ID="cmdDownloadSAP" OnClick="cmdDownload0_Click" runat="server" Text="Download SAP" />

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157146

Add the Visible property to the Button using markup to initially set the second button's visibility:

 <asp:Button ID="btnOne" runat="server" Text="One" OnClick="btnOne_Click" />

 <asp:Button ID="btnTwo" runat="server" Visible="False" Text="Two" OnClick="btnTwo_Click" />

And on the event handlers, add this:

protected void btnOne_Click(object sender, EventArgs e)
{
    btnTwo.Visible = true;
}

protected void btnTwo_Click(object sender, EventArgs e)
{
    btnTwo.Visible = false;
}

Upvotes: 1

Related Questions