首页 > 解决方案 > 如何清除网格视图、文字和面板值?

问题描述

我有一个名为 customer id 的文本框,如果 id 无效,它应该显示一条警告消息,并且需要返回到用户再次尝试输入 Id 的主页。如果 ID 有效,它可以正常工作。如果输入的 Id 无效,则面板和网格视图将使用一些随机值进行更新。我怎样才能清除它们?有什么建议么。

       DataTable dt = oRecord.GetCustomerName(sId);

        if (dt.Rows.Count == 0)
        {
           // making panel and gridview visible false does not work.
            pnlDetails.visible = false;
            gvInfo.visible= false
            pnlSuccess.Visible = false;
            btnOk.Visible = false;
            txtId.Text = string.Empty;
            txtIdentifier.Focus();

            ViewState["sId"] = string.Empty;              

        }

标签: asp.net

解决方案


您不应设置 visible false ,因为它将与现有数据一起将控件设置为不可见,但数据可用并与内存中的这些控件绑定。您应该将这些控件的内容清除为空或 null。

    DataTable dt = oRecord.GetCustomerName(sId);

    if (dt.Rows.Count == 0)
    {
        gvInfo.DataSource = null;
        txtId.Text = string.Empty;
        txtIdentifier.Focus();
        ViewState["sId"] = string.Empty;              
    }

推荐阅读