How to add more than one item to hyperlink control?

Last post 05-31-2007 8:08 PM by justinatucker. 11 replies.

Sort Posts:

  • How to add more than one item to hyperlink control?

    05-09-2007, 12:31 PM

    Here's what I currently have.

    "ID" DataNavigateUrlFormatString="~/admin/admOLPageLink.aspx?ID={0}"
                                DataTextField="ID" DataTextFormatString="Link" HeaderText="Linking">
      

     

    How do I need more than just the ID to DataNavigateUrlFormatString? For example, how do I add the Name field in addition to the ID to the end of the DataNavigateUrlFormatString?
  • Re: How to add more than one item to hyperlink control?

    05-09-2007, 2:16 PM
    • Loading...
    • Liming
    • Joined on 01-09-2006, 8:21 PM
    • Mclean, VA
    • Posts 984
    i would like to to know that too if anybody did something like this. I figured it would be {1}{2}, but where do you supply the parameters?
  • Re: How to add more than one item to hyperlink control?

    05-10-2007, 3:35 AM
    • Loading...
    • Mohan.Raju
    • Joined on 08-28-2006, 9:04 AM
    • Bangalore
    • Posts 76
    supply the parameters in DataNavigateUrlFields property seperated by comma.
  • Re: How to add more than one item to hyperlink control?

    05-10-2007, 9:34 PM
    Answer
    mychucky:

    Here's what I currently have.

    "ID" DataNavigateUrlFormatString="~/admin/admOLPageLink.aspx?ID={0}"
                                DataTextField="ID" DataTextFormatString="Link" HeaderText="Linking">

       How do I need more than just the ID to DataNavigateUrlFormatString? For example, how do I add the Name field in addition to the ID to the end of the DataNavigateUrlFormatString?

    Hello,

    I've come up with a pretty good solution to the problem of needing to build
    the NavigateUrl using multiple data values. My solution is partially based
    on code from this article: http://tripleasp.net/tutorial.aspx?NavID=27

    You need to use a TemplateColumn instead of a HyperLinkColumn. But in order
    to build a TemplateColumn dynamically, you need a class that implements
    ITemplate. I've created such a class which I feel solves this problem quite
    nicely.

    Using the class is as simple as:

    TemplateColumn linkCol = new TemplateColumn();
    linkCol.ItemTemplate = new MultiSourceHyperLinkTemplate("orderItemName",
    "ShowItem.aspx?order={0}&item={1}",
    new string[]{"orderNumber", "orderItemNumber"});
    DataGrid1.Columns.Add(linkCol);

    The above example assumes that orderItemName, orderNumber, and
    orderItemNumber are all column names in the datasource for the datagrid.
    The majority of the work is done by the MultiSourceHyperLinkTemplate class,
    defined below:

    public class MultiSourceHyperLinkTemplate : System.Web.UI.ITemplate
    {
    string m_DataTextField;
    string m_DataNavigateUrlFormatString;
    string[] m_DataNavigateUrlFields;

    public MultiSourceHyperLinkTemplate(string dataTextField, string
    navigateUrlFormatString, string[] navigateUrlFields)
    {
    m_DataTextField = dataTextField;
    m_DataNavigateUrlFormatString = navigateUrlFormatString;
    m_DataNavigateUrlFields = navigateUrlFields;
    }

    private void BindData(object sender, EventArgs e)
    {
    HyperLink link = (HyperLink) sender;
    DataGridItem container = (DataGridItem) link.NamingContainer;
    DataRowView curRow = (DataRowView) container.DataItem;
    link.Text = curRow[m_DataTextField].ToString();
    // evaluate each of the data fields
    string[] navigateUrlValues = new string[m_DataNavigateUrlFields.Length];
    for (int i = 0; i < m_DataNavigateUrlFields.Length; ++i)
    navigateUrlValues[i] = curRow[m_DataNavigateUrlFields[i]].ToString();
    link.NavigateUrl =
    String.Format(System.Globalization.CultureInfo.Inv ariantCulture,
    m_DataNavigateUrlFormatString, navigateUrlValues);

    }

    #region ITemplate Members

    public void InstantiateIn(System.Web.UI.Control container)
    {
    HyperLink link = new HyperLink();
    link.DataBinding += new EventHandler(BindData);
    container.Controls.Add(link);
    }

    #endregion

    #region Property accessors
    string DataTextField { get { return m_DataTextField; } set {
    m_DataTextField = value; } }
    string DataNavigateUrlFormatString { get { return
    m_DataNavigateUrlFormatString; } set { m_DataNavigateUrlFormatString =
    value; } }
    string[] DataNavigateUrlFields { get { return m_DataNavigateUrlFields; }
    set { m_DataNavigateUrlFields = value; } }
    #endregion
    }

    Hope it helps,

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: How to add more than one item to hyperlink control?

    05-20-2007, 6:13 AM
    • Loading...
    • Jinglecat
    • Joined on 03-28-2005, 2:08 PM
    • chongqing city,China
    • Posts 53

    Hi, mychuncky

    I find this post occasionally. Have you resolved it properly ?

    If you are under ASP.NET 2.0,you can take the GridView and use the HyperLinkField that supports the NavigateUrlFields which can contain multi parameters.as following:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
                <Columns>                
                    <asp:HyperLinkField DataNavigateUrlFields="EmployeeID,PhotoPath" 
    DataNavigateUrlFormatString=
    "photo.aspx?empid={0}&path={1}" HeaderText="Photo" Text="View Photo" /> </Columns> </asp:GridView>
     Hope helpful!
     
    struggling for ever
  • Re: How to add more than one item to hyperlink control?

    05-21-2007, 8:43 AM

    Thanks Jinglecat. Now, that sounds much easier and less complicated. I'll give it a try.

  • Re: How to add more than one item to hyperlink control?

    05-21-2007, 9:22 AM

    Okay, here's what I have:

     

    1    <div>
    2            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>"
    3                SelectCommand="SELECT * FROM [Outlook]"></asp:SqlDataSource>
    4        
    5        </div>
    6            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
    7                DataSourceID="SqlDataSource1">
    8                <Columns>
    9                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
    10                       SortExpression="ID" />
    11                   <asp:CheckBoxField DataField="menu" HeaderText="menu" SortExpression="menu" />
    12                   <asp:BoundField DataField="mnuOrder" HeaderText="mnuOrder" SortExpression="mnuOrder" />
    13                   <asp:BoundField DataField="mnuLevel" HeaderText="mnuLevel" SortExpression="mnuLevel" />
    14                   <asp:CheckBoxField DataField="visible" HeaderText="visible" SortExpression="visible" />
    15                   <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
    16                   <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
    17                   <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
    18                   <asp:CheckBoxField DataField="approve" HeaderText="approve" SortExpression="approve" />
    19                   <asp:BoundField DataField="createDate" DataFormatString="{0:d}" HeaderText="createDate"
    20                       HtmlEncode="False" SortExpression="createDate" />
    21                   <asp:HyperLinkField DataNavigateUrlFields="ID,title" DataNavigateUrlFormatString="~/admin/detailPage.aspx?ID={0}&title={1}"
    22                       DataTextField="ID,title" DataTextFormatString="Details" HeaderText="Details" />
    23               </Columns>
    24           </asp:GridView>
    
      

    And I recieved this error on the masterPage.aspx page.

    Server Error in '/admin/Outlook' Application.

    A field or property with the name 'ID,title' was not found on the selected data source.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: A field or property with the name 'ID,title' was not found on the selected data source.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

  • Re: How to add more than one item to hyperlink control?

    05-21-2007, 9:25 AM

    Okay, so if I don't have the field:

     DataTextField="ID,title"
    Then the page displays fine..but the HyerperLink field control doesn't show up. There is no link to click.  
  • Re: How to add more than one item to hyperlink control?

    05-22-2007, 2:46 AM
    Answer
    • Loading...
    • Jinglecat
    • Joined on 03-28-2005, 2:08 PM
    • chongqing city,China
    • Posts 53
    Well, the HyperLinkField's DataTextField just supports only one field! If u indeed want to bind multi field to the HyperLink's Text property, u should use the TemplateField. as below: 
    <asp:gridvie id="gridview1" ruant="server">
    <columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:HyperLink runat="server"
        Text='<%# String.Format("photo.aspx?empid={0}&path={1}", Eval("EmployeeID"), Eval("PhotoPath")) %>'        
        NavigateUrl='<%# String.Format("photo.aspx?empid={0}&path={1}", Eval("EmployeeID"), Eval("PhotoPath")) %>'>
    </asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateField>
    ....
    struggling for ever
  • Re: How to add more than one item to hyperlink control?

    05-22-2007, 8:46 AM

    Thanks!

  • Re: How to add more than one item to hyperlink control?

    05-28-2007, 4:29 PM
    • Loading...
    • yayayo08
    • Joined on 07-17-2006, 9:34 PM
    • Posts 14

    this is really helpful, I have search on the internet find out about the multi field in a hyperlink also.
    And then I finally borswe back to asp.net forum.... lol

    Thanks tooSmile

    Muecs Web Developer
    ----------------------
    http://www.muecs.com
  • Re: How to add more than one item to hyperlink control?

    05-31-2007, 8:08 PM

    I have a similar problem, but it pertains to dynamically displaying images in a DataList. I need to bind (or eval) multiple fields to an image url. Is there a similar solution for the ImageUrl property of an ASP:Image control?

     Here is my code to show you an example of what I am trying to accomplish:

      

    <asp:DataList ID="AltPhotosDataList" DataSourceID="AltPhotoThumbsDataSource" runat="server" CellSpacing="5" HorizontalAlign="Center" RepeatColumns="5" RepeatDirection="Horizontal">
    <ItemTemplate>
       <a href='<%# Eval("ProductID", "/Products/viewProductPhotos.aspx?ProdID={0}") %>' target="viewProductPhotosViewer" onclick="openRequestedPopup(this.href, this.target); return false;">
       <asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("PhotoPath, ProductID, FileName", "/Products/ProdImg/{0}/{1}/viewProducts/altViews/{2}") %>' AlternateText="Click to see more views"/></a>
    </ItemTemplate>
    </asp:DataList>
     
Page 1 of 1 (12 items)
Microsoft Communities