简体   繁体   English

仅在用户提交表单后如何显示EmptyDataText?

[英]How do I show EmptyDataText only after the user submits the form?

I just created a basic ASP.NET website using Microsoft's walkthrough here . 我刚刚使用Microsoft的演练创建了一个基本的ASP.NET网站。 It has one page with a form that takes in some text input, runs a database query based on that, and results the results in a GridView . 它具有一页的表单,该表单接受一些文本输入,并基于该表单运行数据库查询,并将结果显示在GridView I added the EmptyDataText property to my GridView to explicitly show users when their search returns no results. 我将EmptyDataText属性添加到我的GridView以在用户搜索未返回任何结果时显式显示用户。

It all works as expected with one exception: 一切都按预期工作,但有一个例外:

IIS shows my EmptyDataText of "No results found." IIS显示我的EmptyDataText为“未找到结果”。 even before the search form is submitted. 甚至提交搜索表单之前

This defeats my purpose behind using EmptyDataText , which is to indicate to the user that the webpage successfully submitted their search but found no results, as opposed to took their search and threw it into the ether. 这违背了我使用EmptyDataText目的,该目的是向用户指示该网页已成功提交了他们的搜索但没有找到结果,而不是接受他们的搜索并将其扔到了ether中。

For example, a user who searches for something that cannot be found will see "No results found." 例如,搜索无法找到的内容的用户将看到“未找到结果”。 both before and after their search, as opposed to nothing before and "No results found." 在搜索之前和之后,而不是之前和“未找到结果”。 after. 后。 The former behavior gives the impression that the search didn't work. 前一种行为给人的印象是搜索无效。

How can I configure my GridView to show the EmptyDataText only after the search form is submitted? 如何将GridView配置为仅提交搜索表单后才显示EmptyDataText

you are using sqldatasource that will bind automatically while loading the page. 您正在使用的sqldatasource将在加载页面时自动绑定。 Bind the gridview programatically while clicking the search button 单击搜索按钮时以编程方式绑定gridview

Don't bind your GridView until after the user has initiated a search. 在用户启动搜索之前,不要绑定GridView。 When you bind your GridView with a datasource that has 0 records, then the EmptyDataText will be displayed. 当您将GridView与具有0条记录的数据源绑定时,将显示EmptyDataText

Chances are that you are binding it on Page_Load. 您可能会将其绑定在Page_Load上。

EDIT 编辑

Wherever a DataBind() is performed in your code (other than the action handler), remove it. 在代码中执行DataBind()的任何位置(动作处理程序除外),都将其删除。 Your DataBind() should only occur in the handler that receives the user action. 您的DataBind()应该仅出现在接收到用户操作的处理程序中。

If Visual Studio is doing some voodoo behind the scenes with automatic binding, you can always default the grid to invisible. 如果Visual Studio通过自动绑定在幕后进行伏都教化,则始终可以将网格默认设置为不可见。 Make it visible when the user initiates a search. 当用户启动搜索时使其可见。

Just don't databind it before the search, it will effectively be completely invisible until then. 只是在搜索之前不对它进行数据绑定, 之前它实际上是完全不可见的。

UPDATE: maybe you are using a DataSourceID (which databinds automatically)? 更新:也许您正在使用DataSourceID(自动绑定数据)?

UPDATE 2: First off, for what ever reason was the downvote (at least have a decency to leave a comment)? 更新2:首先,出于什么原因投票(至少有体面的要发表评论)? If it weren't for me the OP would still not have known where the problem lies - and secondly, just remove the DataSourceID property from the declaration, and set it back from codebehind when the user makes a search (you might want to call the GridView DataBind() method manually after that, but only if it doesn't do that on its own - try it without first). 如果不是我,那么OP仍将不知道问题出在哪里-其次,只需从声明中删除DataSourceID属性,然后在用户进行搜索时将其从代码隐藏中设置回去(您可能希望调用之后,请手动GridView DataBind()方法,但GridView DataBind()是该方法不能自行执行-无需先尝试即可。

As the other answers suggested, the GridView is being bound before the user makes a search because it has a DataSourceID attribute. 正如其他答案所建议的那样,由于GridView具有DataSourceID属性,因此它是在用户进行搜索之前绑定的。 As explained on MSDN , this attribute causes the GridView to automatically bind to the specified source: MSDN上所述 ,此属性使GridView自动绑定到指定的源:

To bind to a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. 若要绑定到数据源控件,请将GridView控件的DataSourceID属性设置为数据源控件的ID值。 The GridView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform sorting, updating, deleting, and paging. GridView控件自动绑定到指定的数据源控件,并可以利用数据源控件的功能来执行排序,更新,删除和分页。 This is the preferred method to bind to data. 这是绑定数据的首选方法。

To get the behavior I was looking for, I removed that attribute from the GridView and instead added an OnClick attribute to the submit button for my search form. 为了获得所需的行为,我从GridView删除了该属性,而是将一个OnClick属性添加到了搜索表单的OnClick按钮。 The OnClick attribute refers to a method BindGridView that gets called only when the user submits the form. OnClick属性引用仅在用户提交表单时才调用的BindGridView方法。

All this method does is populate the DataSourceID with the same value it had before as an attribute: 此方法所做的全部工作是使用与属性相同的值填充DataSourceID

public void BindGridView(object sender, EventArgs e)
{
    GridView1.DataSourceID = "DataSourceID1";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM