后台为GridView设置链接

来源:互联网 发布:程序员不培训班 编辑:程序博客网 时间:2024/05/18 11:30

GridView链接列界面如下:


HTML代码:

 <asp:GridView ID="gv" runat="server" AllowSorting="True" AutoGenerateColumns="False"                CellPadding="4" PageSize="15" OnSorting="GridView1_Sorting" ShowFooter="True"                OnRowDataBound="GV_OrgGridView_RowDataBound">                <FooterStyle CssClass="GridViewFooterStyle" HorizontalAlign="Center" />                <RowStyle CssClass="GridViewRowStyle" HorizontalAlign="Center" />                <PagerStyle CssClass="GridViewPagerStyle" />                <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />                <HeaderStyle CssClass="GridViewHeaderStyle" />                <EditRowStyle CssClass="GridViewEditRowStyle" />                <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />                <Columns>                    <asp:BoundField HeaderText="序号" DataField="rn" ItemStyle-Width="3%"></asp:BoundField>                    <asp:BoundField HeaderText="养殖场名称" DataField="FARM_NAME" ItemStyle-Width="8%"></asp:BoundField>                    <asp:BoundField HeaderText="所属地区" DataField="REGION_NAME" ItemStyle-Width="8%"></asp:BoundField>                    <asp:BoundField HeaderText="负责人" DataField="PRINCIPAL" SortExpression="PRINCIPAL" ItemStyle-Width="5%"></asp:BoundField>                    <asp:BoundField HeaderText="联系电话" DataField="PRINCIPAL_TEL" SortExpression="PRINCIPAL_TEL" ItemStyle-Width="8%"> </asp:BoundField>                    <asp:BoundField HeaderText="存栏总数" DataField="STOCKCOUNT" Visible="false" SortExpression="STOCKCOUNT" ItemStyle-Width="6%"></asp:BoundField>                    <asp:HyperLinkField DataTextField="STOCKCOUNT" HeaderText="存栏总数" ItemStyle-Width="6%" ItemStyle-CssClass="FontC">                    </asp:HyperLinkField>                    <asp:HyperLinkField HeaderText="母猪" DataTextField="NUM3"  ItemStyle-Width="3%" ItemStyle-CssClass="FontC">                    </asp:HyperLinkField>                    <asp:HyperLinkField HeaderText="肉猪" DataTextField="NUM2" SortExpression="NUM2" ItemStyle-Width="3%" ItemStyle-CssClass="FontC">                    </asp:HyperLinkField>                    <asp:HyperLinkField HeaderText="仔猪" DataTextField="NUM1" SortExpression="NUM1"  ItemStyle-Width="3%" ItemStyle-CssClass="FontC">                    </asp:HyperLinkField>                     <asp:HyperLinkField HeaderText="公猪" DataTextField="NUM4" SortExpression="NUM4" ItemStyle-Width="3%" ItemStyle-CssClass="FontC">                    </asp:HyperLinkField>                    <asp:BoundField HeaderText="当月出栏数" DataField="MONTHSTOCK" SortExpression="MONTHSTOCK" ItemStyle-Width="6%"></asp:BoundField>                    <asp:BoundField HeaderText="当年出栏数" DataField="YEARSTOCK" SortExpression="YEARSTOCK" ItemStyle-Width="6%"></asp:BoundField>                    <asp:BoundField HeaderText="变更时间" DataField="CREATETIME" SortExpression="CREATETIME" ItemStyle-Width="10%"></asp:BoundField>                    <asp:BoundField HeaderText="状态" DataField="STATE" SortExpression="STATE" ItemStyle-Width="4%"></asp:BoundField>                    <asp:TemplateField HeaderText="操作" ItemStyle-Width="10%">                        <ItemTemplate>                            <asp:HyperLink ID="isEdit" runat="server"                                  Visible=<%# (Eval("STATE").ToString() == "注销") ? false : true %>                                Text="编辑" NavigateUrl=<%# "PigUserEdit.aspx?t=10&id="+Eval("id") %>></asp:HyperLink>                             <asp:LinkButton ID="delInfo" runat="server" CausesValidation="false" CommandArgument='<%# Eval("id") %>'                                Visible='<%# (Eval("STATE").ToString() == "注销") ? false : true %>' OnClick="delInfo_Click"                                Text="注销" OnClientClick="return confirm('确认要注销吗?');"></asp:LinkButton>                             <asp:HyperLink ID="isChange" runat="server"                                  Visible=<%# (Eval("STATE").ToString() == "注销") ? false : true %>                                Text="存栏数变更" NavigateUrl=<%# "PigUserChange.aspx?t=10&id="+Eval("id") %>></asp:HyperLink>                           <%--  <a id="isChange"  href="PigUserChange.aspx?t=10&id=<%# Eval("id")%>">                                <span>存栏数变更</span></a> --%>                        </ItemTemplate>                     </asp:TemplateField>                 </Columns>            </asp:GridView> 
后台代码:

  protected void GV_OrgGridView_RowDataBound(object sender, GridViewRowEventArgs e)        {            //控制excel中数字显示(不显示为数学归纳法)            for (int i = 0; i < e.Row.Cells.Count; i++)            {                if (e.Row.RowType == DataControlRowType.DataRow)                    e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");            }            if (e.Row.RowType == DataControlRowType.DataRow)            {                DataRowView myrows = (DataRowView)e.Row.DataItem;                //设置存栏总数链接                if (Convert.ToInt32(myrows["STOCKCOUNT"].ToString() == string.Empty ? "0" : myrows["STOCKCOUNT"].ToString()) > 0)                {                    //设置“存栏总数”的链接                    HyperLink hp = (HyperLink)e.Row.Cells[6].Controls[0];                    string url = string.Format("HistoryInfo.aspx?{0}={1}&{2}={3}",                        UrlParameter.STOCK_ID, myrows["ID"],//参数值                        UrlParameter.ANIMAL_TYPE, 10);                    hp.NavigateUrl = url;                }                //设置仔猪链接                if (Convert.ToInt32(myrows["NUM1"].ToString() == string.Empty ? "0" : myrows["NUM1"].ToString()) > 0)                {                    //设置“仔猪”的链接                    HyperLink hp = (HyperLink)e.Row.Cells[9].Controls[0];                    string url = string.Format("HistoryInfo.aspx?{0}={1}&{2}={3}&{4}={5}",                        UrlParameter.STOCK_ID, myrows["ID"],                        UrlParameter.STOCK_KIND, "仔猪",                        UrlParameter.ANIMAL_TYPE, 10);                    hp.NavigateUrl = url;                }            }            // 合计            if (e.Row.RowType == DataControlRowType.Footer)            {                e.Row.Cells[0].Text = "合计";                e.Row.Cells[6].Text = StockCount.ToString();                e.Row.Cells[7].Text = MuZhuCount.ToString();                e.Row.Cells[8].Text = RouZhuCount.ToString();                e.Row.Cells[9].Text = ZaiZhuCount.ToString();                e.Row.Cells[10].Text = GongZhuCount.ToString();                e.Row.Cells[11].Text = MonthCount.ToString();                e.Row.Cells[12].Text = YearCount.ToString();            }        }

UrlParameter参数键:

/// <summary>/// url参数键/// </summary>public class UrlParameter{    /// <summary>        ///ID        /// </summary>        public const string STOCK_ID = "sid";        /// <summary>        ///类型        /// </summary>        public const string STOCK_KIND = "skind";}

HistoryInfo取值:

 int id = RequestInt(UrlParameter.STOCK_ID);//id string kind = RequestString(UrlParameter.STOCK_KIND);//类型




0 0
原创粉丝点击