How to turn a column in GridView to link

73 views Asked by At

I'm using a GridView to display a report that shows information from different tables in a database. One column in this report is displaying checklistNo which is gotten from the maintenance table in the database. I am trying to make the checklistNo a link so when clicked it open the checklist page.

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

Since the information I need is from the maintenance table I added Maintenance maint = (Maintenance)e.Row.DataItem; but that gives the error

Unable to cast object of type 'BaseClasses.lookupChecklist' to type 'BaseClasses.Maintenance'.

Can I not call the maintenance class from another class?

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

There are 1 answers

0
fnostro On

I can't speak for your data but creating a link...did you know that this:

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

Makes it available in the code behind where you can do something like this in the RowDataBoundEvent:

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

Which makes link creation far easier and more readable

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