Solved

Gridview with Hyperlink column questions

Posted on 2014-12-17
3
271 Views
Last Modified: 2014-12-19
I have a GridView with 3 columns.   One of those columns is a HyperlinkField, using another column as a QueryString entry.

Code looks like this...

      <Columns>
          <asp:BoundField DataField="ContactUs_intended_for" HeaderText="Team" ReadOnly="True">
                 <HeaderStyle HorizontalAlign="Center" />
                 <ItemStyle HorizontalAlign="Center" />
          </asp:BoundField>

          <asp:BoundField DataField="ContactUs_subject" HeaderText="Subject" ReadOnly="True" >
                  <ItemStyle HorizontalAlign="Left" />
          </asp:BoundField>

          <asp:HyperLinkField 
                    DataNavigateUrlFields="ContactUs_intended_for"
                    DataNavigateUrlFormatString="Contact_Us_Details.aspx?Team={0}"
                    Text="Details..."  >
          </asp:HyperLinkField>
      </Columns>

Open in new window


Works good now, as the QueryString field is filled from the column for that has the "ContactUs_intended_for" value.
Therefore, I am tjhinking that only entries that can used in the DataNavigateUrlFormatString property have to be from the GridView...  Is that true?

I need to change the source of the data for the QueryString entries to new DropDown boxes added for the following.
Team named "filterTeam"  and  Status named "filterStatus"

Can I fill the "DataNavigateUrlFields" with the  filterTeam.SelectedValue.Text, filterStatus.SelectedValue.Text  entries?
If not, is there an event that is used that I can create the DataNavigateUrlFormatString with the URL info I need?

I hope that made sense...

Bob
0
Comment
Question by:Bob Bender
3 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 40506654
You're correct about only being able to implicitly bind to properties of the data source. In order to do what you want, you will need to override the RowDataBound event of the grid and manually alter the URL for the hyperlink.

Something like:
<!-- Grid with two drop downs for filters -->
<label for="filterTeam">Team</label>
<asp:DropDownList ID="filterTeam" runat="server">
    <asp:ListItem Value="1" Text="Team 1" />
    <asp:ListItem Value="2" Text="Team 2" />
</asp:DropDownList>

<label for="filterStatus">Status</label>
<asp:DropDownList ID="filterStatus" runat="server">
    <asp:ListItem Value="1" Text="Open" />
    <asp:ListItem Value="2" Text="Approved" />
    <asp:ListItem Value="3" Text="Closed" />
</asp:DropDownList>

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ContactUs" HeaderText="Team" ReadOnly="true">
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField DataField="ContactUsSubject" HeaderText="Subject" ReadOnly="true">
            <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:HyperLinkField Text="Details" />
    </Columns>
</asp:GridView>

Open in new window

And the event itself:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // grab the values from the filter drop downs
        string team = filterTeam.SelectedValue;
        string status = filterStatus.SelectedValue;

        // rebuild the URL for the hyperlink column
        HyperLink link = e.Row.Cells[2].Controls[0] as HyperLink;
        link.NavigateUrl = string.Format("Details.aspx?team={0}&status={1}", team, status);
    }
}

Open in new window

0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 250 total points
ID: 40506660
no. can come from any field on the row.

I build 2 different hyperlinks in a row. one using a hyperlink template field, the other using a label and create the html manually,

from my working app.
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLinkDocId" runat="server" Text="_manual_load"></asp:HyperLink>
                    <asp:HiddenField ID="HF_DocNo" runat="server" Value='<%# Bind("DocNo") %>' />
                    <asp:HiddenField ID="HF_RevNo" runat="server" Value='<%# Bind("RevisionNo") %>' />
                </ItemTemplate>
            <asp:TemplateField HeaderText="File List" SortExpression="FileList">
                <ItemTemplate>
                    <asp:Label ID="LabelFileLinks" runat="server" Text="_manual_load"></asp:Label>
                </ItemTemplate>

                <ItemStyle HorizontalAlign="Center" Width="50px" />

            </asp:TemplateField>

Open in new window


in code behind...
    protected void GridViewProjDocs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            // determine the value of the Hyperlink field
            HyperLink hlDocId = ((HyperLink)e.Row.FindControl("HyperLinkDocId"));
            Label filelinks = (Label)e.Row.FindControl("LabelFileLinks");
            Label Title = (Label)e.Row.FindControl("lblTitle");
            
            CheckBoxList chkFiles = (CheckBoxList)e.Row.FindControl("cklstFiles");

            DataRowView row = (DataRowView)e.Row.DataItem;

            //build the link to Document details page
            hlDocId.NavigateUrl = String.Format("details_prjdoc.aspx?DocNo={0}", row["DocNo"]);
            hlDocId.Text = row["DocId"].ToString();

                        filelinks.Text += "<a href='" + Server.UrlPathEncode(thisURL) + string.Format("' target = '_blank'>" + "<img src='../images/icon-{0}.png' height='20' width='20' border='0' alt='{0}' /></a>", images[i]);
                        filelinks.Text += "&nbsp;";

Open in new window

0
 
LVL 5

Author Comment

by:Bob Bender
ID: 40509934
Thanx folks!   I understand most of that and moving forward.   NOW, to only get 2 QueryStrings (needed for a composite key on a SQL database in the detailsView page.  

I think I can use the RowBound event to build the URL and combine the fields into a single string and pass thru it.

Bob
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question