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;
}
I can't speak for your data but creating a link...did you know that this:
Makes it available in the code behind where you can do something like this in the
RowDataBoundEvent
:Which makes link creation far easier and more readable