Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add navigateurl to hyperlink in gridview

Every row in my gridview is supposed to have a button/hyperlink that navigates to Page?id=something Then there's javascript that catches the redirect and prints the page as an iframe. This something is and already available in the row (but hidden). So i'd want to do something like NavigateUrl = "~/Page.aspx?id=" + Id.ToString() for each field, but the thing is the Id is populated in DataBind() from a LINQ datasource.

<asp:GridView ID="gridleitNidurstada" runat="server" GridLines="None" AllowPaging="True"
    CssClass="mGrid" PagerStyle-CssClass="pgr" DataKeyNames="Id" OnRowDataBound="gridLeit_RowDataBound"
    AllowSorting="True" SortedAscendingHeaderStyle-CssClass="sortasc-header" SortedDescendingHeaderStyle-CssClass="sortdesc-header"
    OnSorting="gridleitNidurstada_Sorting" CurrentSortField="Id" CurrentSortDir="Ascending"
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" Visible="false"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField HeaderText="Print" Text="Print" **anything here?**/>
    </Columns>
    <PagerStyle CssClass="pgr"></PagerStyle>
    <SortedAscendingHeaderStyle CssClass="sortasc-header"></SortedAscendingHeaderStyle>
    <SortedDescendingHeaderStyle CssClass="sortdesc-header"></SortedDescendingHeaderStyle>
</asp:GridView>

Binding is something like this: Could i do it there? Or in the rowbound event?

IQueryable<model.SomeClass> someClass = 
    from m in preparePredicate()
    select new model.SomeClass
    {
        Id = m.id,
        ...
    };
        gridleitNidurstada.DataSource = someClass;
        gridleitNidurstada.DataBind();
like image 383
fogedi Avatar asked Jun 29 '12 14:06

fogedi


2 Answers

You can use the following syntax:

<asp:HyperLinkField DataNavigateUrlFields="col1,col2" DataNavigateUrlFormatString="gotopage.aspx?p1={0}&p2={1}" Text="Print">

More information here and here on MSDN.

EDIT:


If you don't know the exact url when creating the grid, you'll have to do a bit more work. An example I've worked on this week, using an itemtemplate. But in order to use this, you have to set EnableSortingAndPagingCallbacks to false. And do some extra work in code behind for this. I've only used paging on my grid, so this sample I can provide. Markup:

<asp:GridView ID="gridViewTicketsClosed" runat="server" CellPadding="4" ForeColor="#333333"
    GridLines="None" EnableSortingAndPagingCallbacks="False" AutoGenerateColumns="False"
    AllowPaging="True" OnRowDataBound="gridViewTicketsClosed_RowDataBound" OnPageIndexChanging="GridViewPageIndexChanging">
    <PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Previous" />
    <Columns>
        <asp:BoundField DataField="TicketId" HeaderText="Nr.">
            <ItemStyle Width="20px" />
        </asp:BoundField>
        <asp:BoundField DataField="DateStarted" DataFormatString="{0:dd/MM/yyyy HH:mm}" HeaderText="Date">
            <ItemStyle Width="100px" />
        </asp:BoundField>
        <asp:BoundField DataField="President" HeaderText="President">
            <ItemStyle Width="100px" />
        </asp:BoundField>
        <asp:BoundField DataField="TicketTypeId" HeaderText="TicketType"></asp:BoundField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="lnkActionLog" runat="server" Text="Log"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind:

protected void gridViewTicketsClosed_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var hyperLink = e.Row.FindControl("lnkActionLog") as HyperLink;
        if (hyperLink != null)
            hyperLink.NavigateUrl = CreateShowActionLogUrl(e.Row) + "?id="+ DataBinder.Eval(e.Row.DataItem, "TicketId");
    }
}
like image 101
Koen Avatar answered Sep 27 '22 19:09

Koen


Include following in your NavigateUrl attribute

NavigateUrl='<%# Eval("Sl_no", 
              "frmAddIntake.aspx?id=Dashboard&intake_id={0}") %>'
like image 27
Rupesh More Avatar answered Sep 27 '22 19:09

Rupesh More