ASP.NET C#试图隐藏链接按钮 [英] ASP.NET C# trying to hide a link button

查看:163
本文介绍了ASP.NET C#试图隐藏链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点新的ASP.NET和我被迷惑的语法让我有点失落。我想根据if语句来隐藏/关闭按钮,但我不知道如何禁用或隐藏。我已经做了C#之前,但这个code看起来生疏了。

下面是一些code的:

C#组件:

 保护覆盖无效渲染(HtmlTextWriter的作家)
  {
     字符串PostCtrl = Request.Params [__ EVENTTARGET];     如果(PostCtrl ==AccColLocation_content $ collisionLocation $ EditColLocation)
     {
          valDropDownlist(((CustomControl.DropDownValidator)collisionLocation.FindControl(\"valLoc_municipality\")), CollisionLocation.municipality);            ..............
     }
  }

HTML

 <&ItemTemplate中GT;
< ASP:LinkBut​​ton的ID =EditColLocation=服务器文本=编辑碰撞位置的OnClick =CollisionLocation_Edit/>
 < / ItemTemplate中>

valDropDownList方式:

 保护无效valDropDownlist(CustomControl.DropDownValidator valDropdown,串DataElement)
{
    尝试
    {
        布尔mvarRequired,srRequired;
        数据表dtDataElement = DBFunctions.DBFunctions.getDataElement(RepDateTime,DataElement);
        。字符串s = dtDataElement.Rows [0] [mvarRequired]的ToString();
        mvarRequired =(dtDataElement.Rows [0] [mvarRequired]。的ToString()==真)?真假;
        srRequired =(dtDataElement.Rows [0] [srRequired]。的ToString()==真)?真假;
        valDropdown.HaveToSelect =(SelfReported)? srRequired:mvarRequired;
    }
    赶上(例外错误)
    {
        的MessageBox(在设置下拉验证规则时发生错误。+ err.ToString());
    }
}

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

我有这种性质的东西:

 保护无效deletedr(对象发件人,EventArgs的发送)
    {
        尝试
        {
            GridView控件GV =(GridView控件)的FindControl(DriverInfo);
            gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value,,2); ;
            gv.DataBind();            布尔isSelectedLast = FALSE;
            DataTable的DT = DBFunctions.DBFunctions.getInfo(ReportID.Value,,2);            如果(dlDriverNo.SelectedValue == dt.Rows [dt.Rows.Count - 1]。[DriverNo]的ToString())
            {
                isSelectedLast =真;
            }            如果(!(DBFunctions.DBFunctions.deleteDriver(ReportID.Value,dlDriverNo.SelectedValue,isSelectedLast)))
            {
                的MessageBox(NULL);
            }
            其他
            {
                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();
            }
        }
        赶上(例外错误)
        {
            的MessageBox(在删除驱动程序时出现错误。+ err.ToString());
        }
    }


解决方案

如果你的LinkBut​​ton的是在GridView,最有趣的问题越来越它的句柄。之后,你有一个手柄你可以将其设置为不可见或禁用:

  linkBut​​ton.Visible = FALSE;

  linkBut​​ton.Enabled = FALSE;

但要获得一个句柄LinkBut​​ton控件,你需要在适当的情况下使用 .FindControl 在GridView控件:

 < ASP:GridView控件ID =myGridView=服务器OnRowDataBound =myGridView_RowDataBound>
...
< / aspGridView>

然后在code后面你会:

 保护无效myGridView_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    VAR的LinkBut​​ton =(LinkBut​​ton的)e.Row.FindControl(EditColLocation);
    如果(LinkBut​​ton的!= NULL)
    {
        如果(*你的病情检查*)
            linkBut​​ton.Visible = FALSE;
    }
}

希望这将让你在正确的方向前进。

I am somewhat new to ASP.NET and I am confused by the syntax so I am a little lost. I am trying to hide/disable a button based on an if statement but I dont know how to disable or hide it. I have done C# before but this code looks unfamiliar to me.

Below is some of the code:

C# component:

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

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

All these buttons are in grid view.

I have something of this nature:

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

解决方案

If your LinkButton is in a GridView, the most interesting problem is getting a handle on it. After you have a handle you can just set it to invisible or disabled:

linkButton.Visible = false;

or

linkButton.Enabled = false;

But to get a handle to the LinkButton control you will need to use .FindControl in a suitable event on the GridView control:

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

Then in the code behind you would have:

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

Hope this will get you moving in the right direction.

这篇关于ASP.NET C#试图隐藏链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆