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

我对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:

     

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

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

 linkButton.Visible = false; 

要么

 linkButton.Enabled = false; 

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

  ...  

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

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

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

你可以试试

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

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