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

Bul*_*vak 0 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");

            ..............    
     }    
  }
Run Code Online (Sandbox Code Playgroud)

HTML:

 <ItemTemplate>
<asp:LinkButton ID="EditColLocation" runat="server" Text="Edit Collision Location" OnClick="CollisionLocation_Edit" />
 </ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我有这种性质的东西:

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());
        }
    }
Run Code Online (Sandbox Code Playgroud)

use*_*080 5

如果您的LinkBut​​ton在GridView中,则最有趣的问题是在其上获得一个句柄。拥有句柄后,可以将其设置为不可见或禁用:

linkButton.Visible = false;
Run Code Online (Sandbox Code Playgroud)

要么

linkButton.Enabled = false;
Run Code Online (Sandbox Code Playgroud)

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

<asp:GridView ID="myGridView" runat="server" OnRowDataBound="myGridView_RowDataBound">
...
</aspGridView>
Run Code Online (Sandbox Code Playgroud)

然后,在后面的代码中,您将拥有:

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; 
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这能使您朝正确的方向前进。