如何将GridView中的列转换为链接

时间:2015-06-18 10:20:02

标签: c# html asp.net gridview unhandled-exception

我正在使用GridView显示一个报告,该报告显示数据库中不同表的信息。此报告中的一列显示checklistNo,它来自数据库中的维护表。我正在尝试使核对清单没有链接,所以点击它时打开清单页面。

        protected void gvResults_OnItemDataBound(object sender, GridViewRowEventArgs e)
                    {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            lookupChecklist main = (lookupChecklist)e.Row.DataItem;
                            Maintenance maint = (Maintenance)e.Row.DataItem;
                            GridView gv = (GridView)sender;
                            Literal litChecklistNo = e.Row.FindControl("litChecklistNo") as Literal;

                            if (maint.ChecklistID.HasValue)
                            {
                                switch (maint.VehicleTrailer)
                                {
                                    case "Truck":
                                        {
                                            litChecklistNo.Text = "<a href=\"#\" onclick=\"OpenNew('/lookups/Vehicle.aspx?VehicleID=" + maint.LinkedID.ToString() + "&ChecklistNo=" + maint.CheckListNo + "', 'Vehicle');\">" + maint.CheckListNo + "</a>";
                                            break;
                                        }
                                    case "Trailer":
                                        {
                                            litChecklistNo.Text = "<a href=\"#\" onclick=\"OpenNew('/lookups/Trailer.aspx?TrailerID=" + maint.LinkedID.ToString() + "&ChecklistNo=" + maint.CheckListNo + "', 'Trailer');\">" + maint.CheckListNo + "</a>";
                                            break;
                                        }
                                    case "MSQ":
                                    default:
                                        {

                                            break;
                                        }
                                }
                            }
            }

由于我需要的信息来自维护表,我添加了Maintenance maint = (Maintenance)e.Row.DataItem;,但这会产生错误

  

无法将类型为“BaseClasses.lookupChecklist”的对象强制转换为类型   'BaseClasses.Maintenance'。

我可以不从另一个类调用维护类吗?

public static List<lookupChecklist> SearchCheckListItems(int CompanyID,
                                                    string[] SearchTerms,
                                                    string[] SearchFieldKey,
                                                    string DateTypeKey,
                                                     int? ServiceTypeID,
                                                     DateTime? FromDate,
                                                     DateTime? ToDate,
                                                     string ResolveKey,
                                                     string MaintenanceKey
                                                               )
        {

            if (SearchTerms != null &&
                  SearchFieldKey != null &&
                  SearchTerms.Length > 0 &&
                  SearchFieldKey.Length > 0 &&
                  SearchTerms.Length != SearchFieldKey.Length)
                throw new Exception("Search Error: Search Terms must equal Search fields");

            List<MySqlParameter> param = new List<MySqlParameter>{ new MySqlParameter("compid", CompanyID) };
            StringBuilder SQL = new StringBuilder(SearchSQL);

            List<lookupChecklist> main = new List<lookupChecklist>();
            DataTable dtRes = lookupChecklist.CustomFill(SQL.ToString(), param);

            foreach (DataRow dr in dtRes.Rows)
            {
                main.Add(new lookupChecklist(dr));
            }

            return main;
}

1 个答案:

答案 0 :(得分:0)

我不能代表您的数据,但创建链接......您知道吗:

<asp:TemplateField>
  <ItemTemplate>
    <a id="SomeLinkId" runat="server" ></a>
  </ItemTemplate>
</asp:TemplateField>

在后面的代码中提供它,您可以在RowDataBoundEvent中执行类似的操作:

HtmlAnchor  anchor = e.Row.FindControl("SomeLinkId") as HtmlAnchor ;

这使链接创建更容易,更易读

string FormatString = "";

if(maint.VehicleTrailer == "Truck")
  FormatString = "OpenNew('/lookups/Vehicle.aspx?VehicleID={0}&ChecklistNo={1}', 'Vehicle');";

else if(maint.VehicleTrailer == "Trailer" )
  FormatString = "OpenNew('/lookups/Trailer.aspx?TrailerID={0}&ChecklistNo={1}', 'Trailer');";

anchor.Attributes("href") = "#" ;
anchor.InnerText = maint.CheckListNo ;
anchor.Attributes("onclick") = string.Format(FormatString, 
                                             maint.LinkedID.ToString(),
                                             maint.CheckListNo );