Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with conditional gridview HyperLinkField

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
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:
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;
                    }
                    
                }
            
        }
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!
 
I would create a template column with a hyperlink and a lable. Then, in the RowDataBound event, check for "Order" and set the visible property of each control in the template column and any other hyperlink properties if that condition is met.
 
I've made some progress, and it appears that I'm now properly handling my if statement. I added the use of the DataRowView and am checking against the actual LSTPC data now, instead of the DataTextField. This is giving me better results. It now displays the correct text for each record and the ones where LSTPC is 0, are in fact disabled. However, the others do NOT have a hyperlink. Meaning the records where LSTPC is not 0 should display the word Order, and it should be a link. Well, the word Order is displayed but not link exists. Here's my updated RowDataBound code:
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;
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    
                    if (Convert.ToInt32(drv["LSTPC"]) != 0)    
                    {
                        hlf.DataTextFormatString = "{0}";
                        hlf.DataNavigateUrlFields = new string[] { "PART_CODE" };
                        hlf.DataNavigateUrlFormatString = "/Capping%20Pages/CappingShoppingCart.aspx?item_id={0}";
                    }
                    else
                    {
                        hlf.DataTextFormatString = "";
                        hlf.DataNavigateUrlFields = new string[] { "" };
                        hlf.DataNavigateUrlFormatString = "";
                        (dcfc.Controls[0] as HyperLink).Enabled = false;
                    }
                    
                }
            
        }

Any ideas as to why my linking isn't working?
 
Okay, I was able to get it working. Thanks for participating jbenson001.

What I ended up doing was making the attributes of the HyperLinkField permanent...
Code:
<asp:HyperLinkField HeaderText="Add to Cart" ItemStyle-HorizontalAlign="Center"
                                    DataTextField="Availability" DataTextFormatString="{0}" DataNavigateUrlFields="PART_CODE"
                                    DataNavigateUrlFormatString="/Pages/ShoppingCart.aspx?item_id={0}">
                                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                </asp:HyperLinkField>
And just enabling/disabling the field in the code-behind. As usual, I simplified things, and got it to work...this seems to be a theme of mine. Here's the updated RowDataBound Event:
Code:
protected void gvPartSelection_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                    
                DataControlFieldCell dcfc = e.Row.Cells[3] as DataControlFieldCell;
                DataRowView drv = (DataRowView)e.Row.DataItem;
               
                if (Convert.ToDecimal(drv["LSTPC"]) <= 0)    
                {
                    (dcfc.Controls[0] as HyperLink).Enabled = false;
                }
                else
                {
                    (dcfc.Controls[0] as HyperLink).Enabled = true;
                }
            }
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top