zoukankan      html  css  js  c++  java
  • asp.net customdatagrid 绑定数据用到的ItemDataBound 中的HyperLinkColumn 的跳转链接 通过C#后台 处理的方法

    一.前台html代码:

    <tittle:CustomDataGrid id="grdResult" runat="server" Width="1000px" GridWidth="785px" DataKeyField="ID"
    AutoGenerateColumns="False" AllowSorting="true" CellPadding="0" FreezeColumns="0" FreezeHeader="False"
    FreezeRows="0" PageSize="20" 
    onitemdatabound="grdResult_ItemDataBound">
    <PagerStyle HorizontalAlign="Center" ForeColor="#C5D7ED" Mode="NumericPages"></PagerStyle>
    <Columns>
    <asp:BoundColumn DataField="MaterialCode" HeaderText="部品代码" SortExpression="MaterialCode" HeaderStyle-Width="150px" >
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="SnNum" HeaderText="SN" SortExpression="SnNum" HeaderStyle-Width="110px" >
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="InvoiceId"></asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="Url"></asp:BoundColumn>
    <asp:HyperLinkColumn DataTextField="RelatedRecordNo" HeaderText="关联单据编号" SortExpression="RelatedRecordNo" HeaderStyle-Width="180px" >
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:HyperLinkColumn>
    </Columns>
    </tittle:CustomDataGrid>

    二、C#后台处理:

    protected void grdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Pager|| e.Item.ItemType == ListItemType.Header
    || e.Item.ItemType == ListItemType.Footer) //判断是否为页眉,页脚
    {
    return;
    }
    else
    {

    HyperLink link = (HyperLink)e.Item.Cells[4].Controls[0];//第3行HyperLinkColumn对象
    link.NavigateUrl = string.Format("javascript:window.location('{0}.aspx?MasterID={1}');", e.Item.Cells[3].Text, e.Item.Cells[2].Text);
    //e.Item.Cells[3].Text 为url路径,前台对应为:<asp:BoundColumn Visible="False" DataField="Url"></asp:BoundColumn> Visible="False"为不可见
    //e.Item.Cells[2].Text 为ID 前台对应为:<asp:BoundColumn Visible="False" DataField="InvoiceId"></asp:BoundColumn>
    }
    }

    //大家也可以试试用这个:

    if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    HyperLink link = (HyperLink)e.Item.Cells[4].Controls[0];//第3行HyperLinkColumn对象
    link.NavigateUrl = string.Format("javascript:window.location('{0}.aspx?MasterID={1}');", e.Item.Cells[3].Text, e.Item.Cells[2].Text);
    //e.Item.Cells[3].Text 为url路径,前台对应为:<asp:BoundColumn Visible="False" DataField="Url"></asp:BoundColumn> Visible="False"为不可见
    //e.Item.Cells[2].Text 为ID 前台对应为:<asp:BoundColumn Visible="False" DataField="InvoiceId"></asp:BoundColumn>
    }

     注意:千万不要在ItemDataBound里面存在查询数据库的方法,因为会执行很多遍,不合理。

  • 相关阅读:
    TCP/IP 基础知识
    30 岁的码农人生 ——人生至暗时,你依然能窥见光明
    巨经典论文!推荐系统经典模型Wide & Deep
    带你领略拼多多2020校招笔试题,这样的难度你可以搞定吗?
    做业务、做技术和打杂,你的职场现状是哪种?
    内卷预警,本科生真的很不适合算法岗位吗?
    codeforces 1424J,为了过这题,我把祖传的C++都用上了!
    有了Git这个操作,我再也不怕代码混乱了!
    学会了这一招,距离Git大神不远了!
    好端端的数据结构,为什么叫它SB树呢?
  • 原文地址:https://www.cnblogs.com/allenhua/p/3043826.html
Copyright © 2011-2022 走看看