如何在gridview中添加navurl到超链接

时间:2012-06-29 14:42:29

标签: asp.net gridview

我的gridview中的每一行都应该有一个导航到Page的按钮/超链接?id = 然后有javascript捕获重定向并将页面打印为iframe。 某事已经可以在行中使用(但隐藏)。 所以我想为每个字段做一些类似于NavigateUrl =“〜/ Page.aspx?id =”+ Id.ToString()的东西,但事实是Id在LINQ数据源的DataBind()中填充。

<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>

绑定是这样的:我可以在那里做吗? 或者在rowbound事件中?

IQueryable<model.SomeClass> someClass = 
    from m in preparePredicate()
    select new model.SomeClass
    {
        Id = m.id,
        ...
    };
        gridleitNidurstada.DataSource = someClass;
        gridleitNidurstada.DataBind();

4 个答案:

答案 0 :(得分:12)

您可以使用以下语法:

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

MSDN上的更多信息herehere

编辑:


如果您在创建网格时不知道确切的网址,则需要做更多的工作。 本周我使用itemtemplate工作的一个例子。但是,要使用此功能,您必须将EnableSortingAndPagingCallbacks设置为false。并为此做一些额外的代码工作。 我只在网格上使用了分页,所以我可以提供这个样本。 标记:

<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>

代码背后:

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(row.DataItem, "TicketId");
    }
}

答案 1 :(得分:5)

NavigateUrl属性

中加入以下内容
NavigateUrl='<%# Eval("Sl_no", 
              "frmAddIntake.aspx?id=Dashboard&intake_id={0}") %>'

答案 2 :(得分:0)

<asp:TemplateField HeaderText = "Id" ItemStyle-Width="30">
            <ItemTemplate>
                <asp:HyperLink runat="server" NavigateUrl='<%# Eval("TheQueryStringParameterComingFromDB", "~/PageYouWantTONavigateTo.aspx?Id={0}") %>'
                    Text='GiveTextYouWantToDisplay' />
            </ItemTemplate>
        </asp:TemplateField>

答案 3 :(得分:0)

这对我来说很有用。没有其他要求。

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <form method="POST" class="form-horizontal" role="form">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Test Heading
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-md-2 control-label" for="Column1Row1">Column1Row1</label>
                                    <div class="col-md-4">
                                        <input class="form-control input-sm" type="text" name="" value="" id="Column1Row1">
                                    </div> 
                                </div>

                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-md-2 control-label" for="Column2Row1">Column2Row1</label>
                                    <div class="col-md-4">
                                        <input class="form-control input-sm" type="text" name="" value="" id="Column2Row1">
                                    </div> 
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>