conditional hyperlink in datagrid

G

Guest

Hi Folks,

I have a question regarding conditional hyperlink in datagrid.

I want to display Hyperlink if my QID values in (1,4,5,6) other wise i
want to display just Qdescription with out hyperlink.


<asp:hyperlinkcolumn headertext="Question" SortExpression="QDescription"
datatextfield="QDescription"
datanavigateurlformatstring="Answers.aspx?QID={0}"
datanavigateurlfield="QID">
</asp:hyperlinkcolumn>


How can I achieve that kind of functionality?

I looked at the following URL

http://www.dotnet247.com/247reference/msgs/45/226672.aspx


But I didn't get solution for my problem.


Any kind of help is greatly appreciated.


Thanking you
Kumar
 
S

Siva M

One option is to use template-based columns. Eg:

<asp:DataGrid...>
...
<ItemTemplate>
<%# ShowQDescription (DataBinder.Eval(Container.DataItem, "QID"),
DataBinder.Eval(Container.DataItem, "QDescription")) %>
</ItemTemplate>
...
</asp:DataGrid>

In your code-behind, define a function named ShowQDescription() as

protected string ShowQDescription (int qID, string qDesc)
{
string linkTag = null;
if (qID == 1 || qID == 4 || qID == 5 || qID == 6)
{
linkTag = "<a href='answers.aspx?QID=" + ownerID.ToString() + "'>" +
qDesc + "</a>";
}
else
{
linkTag = qDesc;
}
return linkTag;
}

Other option is to have the hyperlinkcoulmn as you have now, but enable or
disable it based on the QID. Use the above-shown technique to determine the
value for Enabled attribute of the hyperlinkcolumn

<asp:HyperLinkColumn Enabled=<%#
IsEnabled(DataBinder.Eval(Container.DataItem, "QID")) %> />

IsEnabled will return True of False based on the QID passed to it.

Yet another option is to handle ItemDataBound event of the grid and handle
enabling hyperlink in the event handler.

Hope this helps.

-Siva

Hi Folks,

I have a question regarding conditional hyperlink in datagrid.

I want to display Hyperlink if my QID values in (1,4,5,6) other wise i
want to display just Qdescription with out hyperlink.


<asp:hyperlinkcolumn headertext="Question" SortExpression="QDescription"
datatextfield="QDescription"
datanavigateurlformatstring="Answers.aspx?QID={0}"
datanavigateurlfield="QID">
</asp:hyperlinkcolumn>


How can I achieve that kind of functionality?

I looked at the following URL

http://www.dotnet247.com/247reference/msgs/45/226672.aspx


But I didn't get solution for my problem.


Any kind of help is greatly appreciated.


Thanking you
Kumar
 
G

Guest

Hi siva,

Thank you for your help.
I did exactly what you told me.
(Write a separate function ShowQDescription).
It's working fine.

Initially I got syntax errors.
I need to put ".Tostring()"
ShowQDescription (DataBinder.Eval(Container.DataItem, "QID").ToString(),
DataBinder.Eval(Container.DataItem, "QDescription").ToString())

To find this syntax problem it took me 30 mts.
How do you debug this kind of errors in html page using visual studio.net
2003?
I tried for it with out luck.

Anyway, Finally my conditional hyperlink is working perfectly.
Thanks again for all your help.
Kumar
 
S

Siva M

This is probably because ShowQDescription method was declared to accept
string parameters whereas the DataBinder.Eval() passed other type of data
from the data source.


Hi siva,

Thank you for your help.
I did exactly what you told me.
(Write a separate function ShowQDescription).
It's working fine.

Initially I got syntax errors.
I need to put ".Tostring()"
ShowQDescription (DataBinder.Eval(Container.DataItem, "QID").ToString(),
DataBinder.Eval(Container.DataItem, "QDescription").ToString())

To find this syntax problem it took me 30 mts.
How do you debug this kind of errors in html page using visual studio.net
2003?
I tried for it with out luck.

Anyway, Finally my conditional hyperlink is working perfectly.
Thanks again for all your help.
Kumar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top