【问题标题】:How to turn a column in GridView to link如何将 GridView 中的列转换为链接
【发布时间】:2015-06-18 10:20:01
【问题描述】:

我正在使用 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;
}

【问题讨论】:

  • 在我看来,您正在将 lookupChecklists 绑定到 gridview,然后尝试将绑定的对象强制转换为“维护”类型……对吗?
  • @FullTimeSkeleton 网格视图没有绑定到任何东西
  • gridview 的数据源是什么?以及它如何转换一行来输入lookupChecklist,然后输入Maintenance?
  • @FullTimeSkeleton 我在我的问题中添加了 gridview 的代码
  • 我很困惑,当我使用 Gridviews 时,你为它们提供了一个数据源,然后你可以将行转换为一个类型,一旦你知道源类型。鉴于此,我从未见过尝试转换为两种不同类型的 GV。如果删除此行:lookupChecklist main = (lookupChecklist)e.Row.DataItem;有用吗?

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


【解决方案1】:

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

<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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2020-02-14
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多