ASP.NET C#试图隐藏链接按钮

时间:2012-06-25 12:56:05

标签: c# asp.net

我对ASP.NET有些新手,我对语法感到困惑,所以我有点迷茫。我试图隐藏/禁用基于if语句的按钮,但我不知道如何禁用或隐藏它。我以前做过C#,但这段代码对我来说并不熟悉。

以下是一些代码:

C#组件:

  protected override void Render(HtmlTextWriter writer)
  {    
     string PostCtrl = Request.Params["__EVENTTARGET"];

     if (PostCtrl == "AccColLocation_content$collisionLocation$EditColLocation")
     {
          valDropDownlist(((CustomControl.DropDownValidator)collisionLocation.FindControl("valLoc_municipality")), "CollisionLocation.municipality");

            ..............    
     }    
  }

HTML:

 <ItemTemplate>
<asp:LinkButton ID="EditColLocation" runat="server" Text="Edit Collision Location" OnClick="CollisionLocation_Edit" />
 </ItemTemplate>

valDropDownList方法:

protected void valDropDownlist(CustomControl.DropDownValidator valDropdown, string DataElement)
{
    try
    {
        bool mvarRequired, srRequired;
        DataTable dtDataElement = DBFunctions.DBFunctions.getDataElement(RepDateTime, DataElement);
        string s = dtDataElement.Rows[0]["mvarRequired"].ToString();
        mvarRequired = (dtDataElement.Rows[0]["mvarRequired"].ToString() == "True") ? true : false;
        srRequired = (dtDataElement.Rows[0]["srRequired"].ToString() == "True") ? true : false;
        valDropdown.HaveToSelect = (SelfReported) ? srRequired : mvarRequired;
    }
    catch (Exception err)
    {
        MessageBox("An error occurred while setting drop down validation rules. " + err.ToString());
    }
}

所有这些按钮都在网格视图中。

我有这种性质的东西:

protected void deletedr(object sender, EventArgs e)
    {
        try
        {
            GridView gv = (GridView)FindControl("DriverInfo");
            gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2); ;
            gv.DataBind();

            bool isSelectedLast = false;
            DataTable dt = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);

            if (dlDriverNo.SelectedValue == dt.Rows[dt.Rows.Count - 1]["DriverNo"].ToString())
            {
                isSelectedLast = true;
            }

            if (!(DBFunctions.DBFunctions.deleteDriver(ReportID.Value, dlDriverNo.SelectedValue, isSelectedLast)))
            {
                MessageBox(null);
            }
            else
            {
                dlDriverNo.Visible = false;
                lblDelDriver.Visible = false;
                delDriverconfim.Visible = false;
                cancelDel.Visible = false;
                dlDriverNo.Items.Clear();
                gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);
                gv.DataBind();
            }
        }
        catch (Exception err)
        {
            MessageBox("An error occurred while deleting the driver. " + err.ToString());
        }
    }

3 个答案:

答案 0 :(得分:5)

如果您的LinkBut​​ton位于GridView中,最有趣的问题是获取它的句柄。拥有句柄后,您可以将其设置为不可见或禁用:

linkButton.Visible = false;

linkButton.Enabled = false;

但要获得LinkBut​​ton控件的句柄,您需要在GridView控件上的适当事件中使用.FindControl

<asp:GridView ID="myGridView" runat="server" OnRowDataBound="myGridView_RowDataBound">
...
</aspGridView>

然后在后面的代码中你会有:

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var linkButton = (LinkButton)e.Row.FindControl("EditColLocation");
    if (linkButton != null) 
    {
        if (*your condition to check*)
            linkButton.Visible = false; 
    }
}

希望这会让你朝着正确的方向前进。

答案 1 :(得分:0)

你可以尝试

valDropdown.Visible = false; //掩盖控件

答案 2 :(得分:0)

if条件后写下面的代码。 Buttonname.visible = FALSE;