我有以下GridView:
<asp:Panel ID="pnlDropDown" runat="server" ClientIDMode="Static" CssClass="pnlDropDown">
<!-- TASK NAME -->
<asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- SERVICE -->
<asp:DropDownList ID="ddlService" CssClass="chosen-select" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- STATUS -->
<asp:DropDownList ID="ddlStatus" CssClass="chosen-select" DataSourceID="dsPopulateStatus" AutoPostBack="true" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlStatus_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- DUE DATE -->
<asp:DropDownList ID="ddlDueDate" CssClass="chosen-select" DataSourceID="dsPopulateDueDate" AutoPostBack="true" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlDueDate_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- OWNER -->
<asp:DropDownList ID="ddlOwner" CssClass="chosen-select" DataSourceID="dsPopulateOwner" AutoPostBack="true" DataValueField="Owner" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlOwner_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateOwner" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- CLIENT -->
<asp:DropDownList ID="ddlClient" CssClass="chosen-select" DataSourceID="dsPopulateClient" AutoPostBack="true" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlClient_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- SITE -->
<asp:DropDownList ID="ddlSite" CssClass="chosen-select" DataSourceID="dsPopulateSite" AutoPostBack="true" DataValueField="Site" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlSite_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateSite" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- PRACTICE -->
<asp:DropDownList ID="ddlPractice" CssClass="chosen-select" DataSourceID="dsPopulatePractice" AutoPostBack="true" DataValueField="Practice" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlPractice_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulatePractice" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<!-- PROVIDER -->
<asp:DropDownList ID="ddlProvider" CssClass="chosen-select" DataSourceID="dsPopulateProvider" AutoPostBack="true" DataValueField="Provider" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlProvider_onSelectIndexChanged">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateProvider" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
<asp:Label ID="lblDDLBlank" runat="server" ClientIDMode="Static" Text=""></asp:Label>
</asp:Panel>
<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
<Columns>
<asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblDep" runat="server" ClientIDMode="Static" Text="Dependencies"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Button runat="server" Text="Dependencies" ID="btnShowDepend" CommandArgument='<%#Eval("Object") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Practice" HeaderText="Practice" SortExpression="Practice" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" ItemStyle-CssClass="taskTableColumn" />
</Columns>
</asp:GridView>
以下填充GridView的函数:
public void PullData(string sortExp, string sortDir, bool blOnLoad)
{
query = "";
DataTable taskData = new DataTable();
connString = @"";
if (!blOnLoad)
{
//SET UP THE "query" QUERY STRING
}
else
{
//SET UP THE "query" QUERY STRING
}
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
SqlCommand cmd = new SqlCommand(query, conn);
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
myDataSet = new DataSet();
da.Fill(myDataSet);
myDataView = new DataView();
myDataView = myDataSet.Tables[0].DefaultView;
if (sortExp != string.Empty)
{
myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
yourTasksGV.DataSource = myDataView;
yourTasksGV.DataBind();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
}
protected void yourTasksGV_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView hGrid = (GridView)sender;
GridViewRow gvrRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableHeaderCell tcCellTask = new TableHeaderCell();
tcCellTask.Controls.Add(ddlTaskName);
gvrRow.Cells.Add(tcCellTask);
TableHeaderCell tcCellDep = new TableHeaderCell();
tcCellDep.Controls.Add(lblDDLBlank);
gvrRow.Cells.Add(tcCellDep);
TableHeaderCell tcCellSvc = new TableHeaderCell();
tcCellSvc.Controls.Add(ddlService);
gvrRow.Cells.Add(tcCellSvc);
TableHeaderCell tcCellStats = new TableHeaderCell();
tcCellStats.Controls.Add(ddlStatus);
gvrRow.Cells.Add(tcCellStats);
TableHeaderCell tcCellDD = new TableHeaderCell();
tcCellDD.Controls.Add(ddlDueDate);
gvrRow.Cells.Add(tcCellDD);
TableHeaderCell tcCellOwner = new TableHeaderCell();
tcCellOwner.Controls.Add(ddlOwner);
gvrRow.Cells.Add(tcCellOwner);
TableHeaderCell tcCellCli = new TableHeaderCell();
tcCellCli.Controls.Add(ddlClient);
gvrRow.Cells.Add(tcCellCli);
TableHeaderCell tcCellSit = new TableHeaderCell();
tcCellSit.Controls.Add(ddlSite);
gvrRow.Cells.Add(tcCellSit);
TableHeaderCell tcCellPra = new TableHeaderCell();
tcCellPra.Controls.Add(ddlPractice);
gvrRow.Cells.Add(tcCellPra);
TableHeaderCell tcCellPro = new TableHeaderCell();
tcCellPro.Controls.Add(ddlProvider);
gvrRow.Cells.Add(tcCellPro);
yourTasksGV.Controls[0].Controls.AddAt(0, gvrRow);
LinkButton btnSort;
System.Web.UI.WebControls.Image image;
//iterate through all the header cells
foreach (TableCell cell in e.Row.Cells)
{
//check if the header cell has any child controls
if (cell.HasControls())
{
//get reference to the button column
btnSort = (LinkButton)cell.Controls[0];
image = new System.Web.UI.WebControls.Image();
if (ViewState["sortExp"] != null)
{
//see if the button user clicked on and the sortexpression in the viewstate are same
//this check is needed to figure out whether to add the image to this heade column or not
if (btnSort.CommandArgument == ViewState["sortExp"].ToString())
{
//check what type of sort order it is
if (ViewState["sortOrder"].ToString() == "Asc")
{
image.ImageUrl = "../theImages/up.png";
image.CssClass = "hdrImage";
}
else
{
image.ImageUrl = "../theImages/down.png";
image.CssClass = "hdrImage";
}
cell.Controls.Add(image);
}
}
}
}
}
}
当我运行Web应用程序时,会迭代RowCreated,然后它会给我以下错误:
unable to cast object of type 'system.web.ui.literalcontrol' to type 'system.web.ui.webcontrols.linkbutton'
如何解决错误?
答案 0 :(得分:2)
问题可能是在一个标题单元格中添加了Label
:
TableHeaderCell tcCellDep = new TableHeaderCell();
tcCellDep.Controls.Add(lblDDLBlank);
gvrRow.Cells.Add(tcCellDep);
稍后浏览标题单元格时,请尝试将此Label
转换为LinkButton
,这是不可能的。
要解决此问题,请检查它是否真的是LinkButton
:
if (cell.HasControls())
{
//get reference to the button column
btnSort = cell.Controls[0] as LinkButton;
image = new System.Web.UI.WebControls.Image();
if (ViewState["sortExp"] != null && btnSort != null)
{
....
}
}
答案 1 :(得分:1)
问题可能就在这一行:
btnSort = (LinkButton)cell.Controls[0];
你单元格中的第一个控件很可能是Litteral(虽然我看到了#34; HyperlinkField&#34;),你正试图将其转换为linkButton,从而引发了这个错误。