根据条件使按钮可见

时间:2013-09-20 14:25:24

标签: c# button visibility

我是一名新手实习生试图“修复”内部表格背后的c#代码。代码使用我们数据库中的日期填充几个数据网格。

有一些导出按钮,如果其中一个网格返回没有行(有时会发生这种情况),我想让它不可见。

当您单击“转到”按钮以填充网格时,如果某个网格返回时没有行,则这些按钮将变为不可见;

然而它不起作用!

我发布了上下文的整个按钮点击事件,但是我对/ visible中的按钮的尝试是朝向最底层(出于安全原因,一些识别内容在代码中被删除)。

我做错了什么?

public void btn_go_Click(object sender, EventArgs e)
{
    try
    {
        string site = this.ddl_site.SelectedValue;
        string srn = this.ddl_al.SelectedValue;

        int siteID = Common.CommonFunctions.XXXXXX_SiteID(site);

        using (DBML.XXXXDataContext db = AuthUtils.Auth.getDataContext(Common.DataContextE.XXXXX, Common.CommonFunctions.getXXXXX_ServerFromString(site)))
        {

            var CHAL = (from gal in db.XXXXXXXAccessLevels
                        where gal.ServerID.Equals((short)siteID)
                        && gal.ServerRecordNumber.ToString().Equals(srn)

                        select gal.XXXXXccessLevels.FirstOrDefault());
            if (CHAL.First() == null)
            {
                throw new Exception("No corresponding CHAL for this GAL [SiteID=" + siteID + ",SRN=" + srn + "]");
            }

            BaseClasses.XXXXXXX_AccessLevel al = new BaseClasses.XXXXXXX_AccessLevel(CHAL.FirstOrDefault());

            this.lbl_al_tag.Text = "Access Level Name: ";
            this.lbl_al_name.Text = al.AccessLevelName;
            this.lbl_al_desc_tag.Text = "Access Level Descr: ";
            this.lbl_al_desc.Text = al.AccessLevelDesc;

            this.gv_people.DataSource = al.getPeople();
            this.gv_people.RowDataBound += new GridViewRowEventHandler(gv_people_RowDataBound);

            this.gv_people.DataBind();

            this.gv_doors.DataSource = al.getDoors();

            this.gv_doors.RowDataBound += new GridViewRowEventHandler(gv_doors_RowDataBound);

            this.gv_doors.DataBind();

            this.gv_doors.Visible = true;
            this.gv_people.Visible = true;
            this.lbl_al_name.Visible = true;
            this.lbl_al_desc.Visible = true;

            //attempt to hide export buttons for 
            ////Working on this right here
            int rowCount = this.gv_doors.Rows.Count;
            if (rowCount > 0)
            {
                export_excel.Visible = true;
                export_excell_both.Visible = true;
            }
            else
            {
                export_excel.Visible = false;
                export_excell_both.Visible = false;

            }




        }
    } 

1 个答案:

答案 0 :(得分:0)

如果您尝试使用int rowCount = al.getDoors().Count而不是网格的行数?当您尝试访问它们时,网格行计数可能还没有更新。