Solved

Gridview with Hyperlink column questions

Posted on 2014-12-17
3
261 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now