如何从服务器端显示/隐藏按钮?

时间:2014-06-24 07:00:05

标签: c# asp.net

我是ASP.NET和C#的新手,我尝试了一些但没有用的东西。我有一个.aspx页面,其中有三个按钮,如下面的代码所示:

<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>

我尝试这样做btnConform在页面加载时最初是不可见的,现在,如果我点击cmdDownloadcmdDownloadSAP,按钮btnConform必须变得可见。现在,如果我再次点击btnConform,它将变得不可见。

我无法使用JavaScript,因为cmdDownloadcmdDownloadSAP还有其他任务需要完成才能使btnConform可见(例如数据库调用),而btnConform只会变得可见如果数据库调用成功。

   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();
    }
}
}

这不起作用。 很抱歉更改了问题的内容(导致较旧的内容并未正确指出问题)。这是实际的代码,我试图这样做。 (查看完整代码看起来很愚蠢...)

2 个答案:

答案 0 :(得分:1)

使用标记将Visible属性添加到Button以初始设置第二个按钮的可见性:

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

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

在事件处理程序上,添加:

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

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

答案 1 :(得分:1)

您可以使用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;
}

如果您想使服务器控件最初不可见,您还必须使用此属性,因此您可以使用代码隐藏在aspx页面上以编程方式更改可见性:

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

服务器端的最后一个注释Visible=false表示此控件根本不会在客户端呈现。因此它根本不存在,无法在客户端访问(或使其可见)。

如果您需要在客户端访问它,您应该使用

通过样式表使其不可见
  • display: none(不占用任何空间)或
  • visibility: hidden(仍占用布局中的空间)。

更新 acc。对于你的问题的最后编辑,事件处理程序似乎不再被注册。

所以你需要:

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