locked
TemplateField - as HyperlinkField RRS feed

  • Question

  • User220959680 posted

    Hi  

    I am using GridView control. One of the Columns in GridView converted as <Template Field>. In <ItemTemplate> i have used Lable Control binding with the method (GetCalc(Eval("Name")). It is  returning expected value for each row of the column in percentage say 60%, 90%, 0% etc.


    It should be easy to understand when you check the code below:

    <asp:TemplateField HeaderText="Presence Percentage">
                        <ItemTemplate>
                         
                          <asp:Label ID="Label1" runat="server" Text=' <%# GetCalc(Eval("Name").ToString()) %>'>
                          </asp:Label>
                       
                        </ItemTemplate>
                    </asp:TemplateField>

    I like to know

    1) How can i make this <ItemTemplate> column as Hyperlink. so that when the user clicks on any row of the column, it takes the user to another page depending on the row selected. It should redirect the user when the value is not 0% only. When it is 0%, clicking on the row should not redirect.

    Wednesday, November 21, 2007 1:50 PM

Answers

  • User1548900558 posted

    This is how it works:

    NavigationUrl='<%# GetCalc(Eval("Name").ToString()).Equal("0%")?"": "newpage" %>'

     In the NaviageUrl, I am using the <%# %> to write C# code. I am writing an if else statement using the syntax:

    condition ? return value if the condition is true : return value if the condition is false

    in the condition, I am evaluating the retrun value from the GetCalc function: GetCalc(Eval("Name").ToString()).Equal("0%")

    in the true condition: I am putting "" (empty string in the NavigationUrl

    in the false condition, I am putting "newpage" where "newpage" is the page that you want it to be redirected to.

    If you have furthur question, let me know

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 25, 2007 3:17 PM

All replies

  • User1548900558 posted

    Here is how you do it:

    <?xml:namespace prefix = asp /><asp:TemplateField class=st HeaderText="<ItemTemplate> 
        <ITEMTEMPLATE>                       
           <asp:HyperLink  <asp:HyperLink class=st id=""Label1" runat="server" Text=' <%# GetCalc(Eval("Name").ToString()) %>' NavigationUrl='<%# GetCalc(Eval("Name").ToString()).Equal("0%")?"": "newpage" %>' > </asp:HyperLink></asp:HyperLink></asp:TemplateField>
    <asp:TemplateField class=st HeaderText="<asp:HyperLink class=st></ItemTemplate></asp:HyperLink></asp:TemplateField>
    <asp:TemplateField class=st HeaderText="<asp:HyperLink class=st>where newpage: is the link of your new page
             
        </ITEMTEMPLATE> 
    </asp:TemplateField> 
    </asp:HyperLink>
    Wednesday, November 21, 2007 3:28 PM
  • User-195892214 posted

    I'd use a hyperlink column instead of a template column, and in the rowdatabound handler, I'd do the calculation and disable the link if it's 0%.

    Wednesday, November 21, 2007 3:33 PM
  • User220959680 posted

    Hi Tab Alleman,

    Could you be descriptive? can you explain with code?

    Can you explain how can I use Hyperlink column ( i think you mean to say 'Hyperlink Field') and use rowdatabound handler?

    Calculating and disabling link if it is 0%?

     

     

    Regards,

    Wednesday, November 21, 2007 4:59 PM
  • User-195892214 posted

    Hmmm, looking into it, I expected that there would be a way to programmatically "disable" a hyperlinkfield, but it seems there isn't.

    I guess in that case I'd use a blank template field in my grid, and in my grid's rowdatabound event handler, I'd do my calculation on my data column [using e.row.dataitem() ] and then create the contents of that cell based on the results. 

     

    Wednesday, November 21, 2007 5:27 PM
  • User78900492 posted

    Here's what I think you can do:

    Add a hyperlink control to it, define  its id as the record id of the row,implement the hyperlink onClick event. With in this event, if the page you want to click to is within your application, then use Server.Transfer and append the id value to end of the address, else use Reponse.Redirect and append the id to the end of the address.

     

    Let me know if that works.

     

    Thursday, November 22, 2007 6:13 PM
  • User-1227788583 posted

    Hi sukumarraju,

    Try this.

     <asp:HyperLink ID="lnkEdit" runat="server"

    NavigateUrl='<%# GetCalc(Eval("Name")).ToString()!="0%"?"urUrl.aspx":"" %>'

    Text='<%# Eval("Name")%>'>

    </asp:HyperLink>

    if u want have ur Navigate URL also in DB at a field "URL"

    then change the NavigateURL='<%# GetCalc(Eval("Name")).ToString()!="0%"? '<%#Eval("URL")%>:"" %>'

    Regards,

    Naveen

    Friday, November 23, 2007 12:20 AM
  • User1062752276 posted

    try following code 

                 <ItemTemplate>
                         
                          <a href='<%# GetCalc(Eval("Name").ToString()!="0"?"Page.aspx":"#") %>'  ><%# GetCalc(Eval("Name").ToString()) %> </a>
                         
                        
                 </ItemTemplate>

    Friday, November 23, 2007 2:23 AM
  • User220959680 posted

    Hello Naveen,

     Seems like it does not work in asp.net 2.0. Here are the error messages:

    1) System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:HyperLink' is of type 'System.Web.UI.WebControls.HyperLink'.

    2) Literal content ('</asp:HyperLink>') is not allowed within a 'System.Web.UI.WebControls.DataControlFieldCollection'.

    Thanks

    Friday, November 23, 2007 5:36 AM
  • User-1227788583 posted

    Hi sukumarraju,

    I am very preoccupied today.

    So, sorry for the errors.

    This will work.

    I have tested.

    I gather ur requirement is to navigate to another Page when conditio is not "0%";

    the url will be on the DB, right?

    If so try these

    On aspx Page

    <asp:HyperLink ID="lnkEdit" runat="server"

    NavigateURL='<%# GetCalc(Eval("Lock"),Eval("URL"))%>'

    Text='<%# Eval("fullname")%>'>

    On aspx.cs Page

    protected string GetCalc(object oLock, object oURL)

    {

    string mystr = String.Empty;

    string myID = (string)oLock;

    string myURL = String.Empty;

    switch (myID)

    {

    case "yes":

    mystr = "0%";

    break;

    case "no":

    mystr = "10%";

    break;

    default:break;

    }

    if (mystr != "0%")

    myURL = (string)oURL;

    return myURL;

    }

     if u want to navigate to a fixed page say "Home.aspx"

    if condition !=0 then try this

    On aspx Page

    protected string GetCalc(object oLock)

    {

    string mystr = String.Empty;

    string myID = (string)oLock;

    string myURL = String.Empty;switch (myID)

    {

    case "yes":

    mystr = "0%";

    break;

    case "no":

    mystr = "10%";

    break;

    default:

    break;

    }

    if (mystr != "0%")

    myURL = "Home.aspx";

    return myURL;

    }

    I have a confusion as to what ur field "Name" in your table returns.

    My lock is of type boolean returning "yes" and "no".

    Workaround this.

    hope this helps

    Regards,

    Naveen

    P.S I have checked both the codes.

    They are running [:)]

    Friday, November 23, 2007 6:08 AM
  • User220959680 posted

    Hi Naveen:

    Thank you. Please note url is not stored in Database. "Name" is taken from database as Hyperlink column, Persence Percentage Column calculates Presence of each row of "Name" Column using GetCalc(Eval("Name").ToString()) method.

    Just now "Name" Column is displayed as hyperlink field, clicking on that takes to another page. Persence Percentage column is also displaying 70%, 60% etc depending on the each row of "Name" column. 

    My Requirement is to make  Persence Percentage column as a Hyperlink , so that clicking on particualr row ( when it is not 0%) takes the user to same page as "Name" Hperlink field takes. Brielfly "if the Presence Percentage is not 0 (i.e. there is data to display) clicking the Name text, or the presence row, sends the user to the anotherpage.aspx appropriately. Here is my code just now.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" >
     
                <Columns>
                                   
                    <asp:HyperLinkField DataNavigateUrlFields="Name" DataNavigateUrlFormatString="~\anotherpage.aspx?Name={0}"
                        DataTextField="Name" DataTextFormatString="{0:C}" HeaderText="Data Type" Target="_blank" />
     
                    <asp:TemplateField HeaderText="Presence Percentage">
                   <ItemTemplate>
                         
                          <asp:Label ID="Label1" runat="server" Text=' <%# GetCalc(Eval("Name").ToString()) %>'>
                          </asp:Label>
                       
                   </ItemTemplate>
     </asp:TemplateField
                   

                </Columns>

    Thank You

    Friday, November 23, 2007 7:39 AM
  • User1062752276 posted


                <Columns>
                                   
                    <asp:HyperLinkField DataNavigateUrlFields="Name" DataNavigateUrlFormatString="~\anotherpage.aspx?Name={0}"
                        DataTextField="Name" DataTextFormatString="{0:C}" HeaderText="Data Type" Target="_blank" />
     
                    <asp:TemplateField HeaderText="Presence Percentage">
     

    You can not take servercontrol out side of ItemTemplate.You have to take it in ItemTemplate.

    try with Html Anchor Tag.It will work fine 

    Friday, November 23, 2007 7:54 AM
  • User220959680 posted

    Hello Impathan

    Thanks. But it does not work. Just now i am using  <Hyperlink field>, it works half the way. Indeed in shows the value(98%, 0%, etc) in column, but displayed as normal numbers. Not as Hyper Text. 

    <asp:TemplateField HeaderText="Presence Percentage">

    <ItemTemplate>

    <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# GetCalc(Eval("Name").ToString()) %>'></asp:HyperLink>

    </ItemTemplate>

    </asp:TemplateField>

    Regards,

    Friday, November 23, 2007 10:41 AM
  • User1062752276 posted

    asp:HyperLink ID="HyperLink1" runat="server" Text='<%# GetCalc(Eval("Name").ToString()) %>'></asp:HyperLink>
     

    Dude.Where is your  URL path?

    You have to set it. 

    Saturday, November 24, 2007 1:00 AM
  • User220959680 posted

    Hi Eagle,

    How does it works?  i think it takes to user to another page without passing the selected value ( clicking on particular row of the column).

    At the same time it should redirect ONLY when GetCalc(Eval("name")) method is  NOT 0%.

    Sunday, November 25, 2007 3:03 PM
  • User1548900558 posted

    This is how it works:

    NavigationUrl='<%# GetCalc(Eval("Name").ToString()).Equal("0%")?"": "newpage" %>'

     In the NaviageUrl, I am using the <%# %> to write C# code. I am writing an if else statement using the syntax:

    condition ? return value if the condition is true : return value if the condition is false

    in the condition, I am evaluating the retrun value from the GetCalc function: GetCalc(Eval("Name").ToString()).Equal("0%")

    in the true condition: I am putting "" (empty string in the NavigationUrl

    in the false condition, I am putting "newpage" where "newpage" is the page that you want it to be redirected to.

    If you have furthur question, let me know

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 25, 2007 3:17 PM
  • User-1227788583 posted

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" >
     
                <Columns>
                                   
                    <asp:HyperLinkField DataNavigateUrlFields="Name" DataNavigateUrlFormatString="~\anotherpage.aspx?Name={0}"
                        DataTextField="Name" DataTextFormatString="{0:C}" HeaderText="Data Type" Target="_blank" />
     
                    <asp:TemplateField HeaderText="Presence Percentage">
                   <ItemTemplate>
                         
                          <asp:Label ID="Label1" runat="server" Text=' <%# GetCalc(Eval("Name").ToString()) %>'>
                          </asp:Label>
                       
                   </ItemTemplate>
     </asp:TemplateField
                   

                </Columns>

    Hi sukumarraju,

    Try this instead of the HyperLinkField.

    <asp:TemplateField HeaderText="Presence Percentage">

     

    <ItemTemplate>

     

    <asp:HyperLink ID="Label1" runat="server"

    NavigateUrl=' <%# GetCalc(Eval("Name"))!= "0%" ? "~\anotherpage.aspx?Name=" + Eval("fName") : "" %>'

    Text ='<%# Eval("Name") %>'> </asp:HyperLink>

     

    </ItemTemplate>

     

    </asp:TemplateField>

     

    protected string GetCalc(object oLock)

    {

    string mystr = String.Empty;

    string myID = (string)oLock;

    switch (myID)

    {

    case "yes":

    mystr = "0%";

    break;

    case "no":

    mystr = "10%";

    break;

    default:break;

    }

    return mystr;

    }

    Hope this helps.

    Regards,

    Naveen

    Sunday, November 25, 2007 11:56 PM
  • User220959680 posted

    Naveen,

    Thank you. I would try that.

    But please note GetCalc() method takes 'Name' in GridView as a parameter and returns  Presence Percentage i.e., 10% or 90% or 0%. As i mentioned earlier, i like to redirect the user to another page when Presence percentage is not 0%.

    Hoping i am not confusing ... please ask for any clarification.

    Regards

     

    Monday, November 26, 2007 5:23 AM