我在我的Asp.Net应用程序中使用.NET Framework 4.6.1
并尝试将GridView.ShowHeaderWhenEmpty
属性应用于我的gridview以在页面加载时显示标题(在数据表中有数据填充行并且为空之前)。当我加载此页面时,只有一个空格,直到用户单击其他一些控件。
ASPX
<div class="col-md-12" style="overflow: auto; width: 1150px; max-height: 800px; height: 800px; border-style:solid; border-color: darkblue; border-width:thin;">
<asp:GridView ID="uxSearchGridView" runat="server" ShowHeaderWhenEmpty="true" CssClass="GridView" HeaderStyle-BackColor="#ADD8E6" BorderStyle="Solid" onRowDataBound="uxSearchGridView_RowDataBound" AutoGenerateColumns="False" OnSorting="uxSearchGridView_Sorting" BackColor="White" BorderColor="#D6D2D2" BorderWidth="1px" CellPadding="3" SelectedIndex="-1" DataKeyNames="TicketNumber" AllowSorting="True" Font-Size="Small" Width="100%" Visible="True" EnableModelValidation="True" style=" margin-top: 10px; margin-bottom: 10px;">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Details" ButtonType="Button" HeaderText="Select" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" SortExpression="Ticket Number" />
<asp:BoundField DataField="Complexity" HeaderText="Complexity" SortExpression="Complexity" />
<asp:BoundField DataField="NatureOfInquiry" HeaderText="Nature of Inquiry" SortExpression="NatureOfInquiry" />
<asp:BoundField DataField="SMEResponseDetail" HeaderText="Response" SortExpression="SMEResponseDetail" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
_dtMgr = new DataAccessManager()
string staffName = _dtMgr.GetStaffNameByUser(Session["UserNameSession"].ToString());
if (staffName == string.Empty)
{
//error
}
else
{
Session["StaffName"] = staffName;
if (!Page.IsPostBack)
{
uxSearchGridView.DataSource = null;
uxSearchGridView.DataBind();
}
}
}
有什么东西我会遗漏或者是否有另一个原因导致我的标题不会在页面加载时显示?
我终于意识到我必须创建一个空数据表并将其绑定,以便ShowHeaderWhenEmpty
工作。在页面加载时,我将空参数(允许空参数)添加到我的sp
,它将返回dataset
,我最终将用作Datasource
的gridview
。这返回了一个空表,但允许显示标题。我还添加了空行(仅用于审美目的):
protected void Page_Load(object sender, EventArgs e)
{
_searchDT = _dtMgr.GetTicketsByKeyword(uxKeywordTextBox.Text, null);
....
if (!Page.IsPostBack)
{
DataRow dr = null;
dr = _searchDT.NewRow();
_searchDT.Rows.Add();
uxSearchGridView.DataSource = _searchDT;
uxSearchGridView.DataBind();
}
}
...
}