I'm populating a gridview and one of the colums is a HyperLinkField. If LSTPC from the Query is 0, then I need to display some static text, otherwise I need to display a standard dynamic link.
Maybe I'm not going about this the right way but here's what I'm doing...that I can't quite get working.
In my sqldatasource for the gridview in question, my SelectCommand includes the following sql: "(CASE WHEN LSTPC <= 0 THEN 'Call 1-800-xxx-xxxx for current price' ELSE 'Order' END) as Availability" Basically I'm creating a variable that I can then use for the HyperLinkField. Then I figured I could do some checking on the RowDataBound event, and if the DataTextField equals Order, show the dynamic url. If not, just show the text "'Call 1-800-xxx-xxxx for current price".
Here's the gridview hyperlinkfield column:
<asp:HyperLinkField HeaderText="Add to Cart" DataTextField="" DataTextFormatString="" DataNavigateUrlFields="" DataNavigateUrlFormatString="" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:HyperLinkField>
Here's the RowDataBound code, with explanation of what's happening to follow:
The code works, but I'm not getting the desired result. I think the problem lies with my if statement: if (hlf.DataTextField == "Order") I'm guessing that this doesn't actually look at the DataTextField's value...and that it just sees 'Availability' and this is why the results only show hyperlinkfield's properties that I've set in the else statement. Is there a better way to go about this, or a way to get the 'value' of the datatextfield?
Thanks!
Maybe I'm not going about this the right way but here's what I'm doing...that I can't quite get working.
In my sqldatasource for the gridview in question, my SelectCommand includes the following sql: "(CASE WHEN LSTPC <= 0 THEN 'Call 1-800-xxx-xxxx for current price' ELSE 'Order' END) as Availability" Basically I'm creating a variable that I can then use for the HyperLinkField. Then I figured I could do some checking on the RowDataBound event, and if the DataTextField equals Order, show the dynamic url. If not, just show the text "'Call 1-800-xxx-xxxx for current price".
Here's the gridview hyperlinkfield column:
<asp:HyperLinkField HeaderText="Add to Cart" DataTextField="" DataTextFormatString="" DataNavigateUrlFields="" DataNavigateUrlFormatString="" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:HyperLinkField>
Here's the RowDataBound code, with explanation of what's happening to follow:
Code:
protected void gvPartSelection_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataControlFieldCell dcfc = e.Row.Cells[3] as DataControlFieldCell;
HyperLinkField hlf = dcfc.ContainingField as HyperLinkField;
if (hlf.DataTextField.ToString() == "Order")
{
hlf.DataTextField="Availability";
hlf.DataTextFormatString="{0}";
hlf.DataNavigateUrlFields = new string[] { "PART_CODE" };
hlf.DataNavigateUrlFormatString = "/Pages/ShoppingCart.aspx?item_id={0}";
}
else
{
hlf.DataTextField = "Availability";
hlf.DataTextFormatString = "";
hlf.DataNavigateUrlFields = new string[] { "" };
hlf.DataNavigateUrlFormatString = "";
(dcfc.Controls[0] as HyperLink).Enabled = false;
}
}
}
Thanks!