[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Gridview with Hyperlink column questions

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
Bob Bender
Asked:
Bob Bender
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Robberbaron (robr)Commented:
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
 
Bob BenderAuthor Commented:
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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now