0

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

3 回答 3

5

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

linkButton.Visible = false;

或者

linkButton.Enabled = false;

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

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

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

于 2012-06-25T13:23:54.847 回答
0

在 if 条件之后编写以下代码。按钮名.可见=假;

于 2012-06-25T13:12:29.530 回答
0

你可以试试

valDropdown.Visible = false; //屏蔽控件

于 2012-06-25T13:08:22.933 回答