双人余公 2021-09-01 20:36 采纳率: 75%
浏览 80
已结题

C# asp.net GridView中BoundField改为TemplateField后,部分列后台获取不到

原本是用GridView的BoundField属性展示列,现在因为需要增加删除和编辑GridView的行,改为TemplateField展示。但是原来部分更改单元格颜色的代码失效了,找不到原因。
原来的前端代码:

 <asp:GridView ID="grid_01" runat="server"  OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%"  Font-Names="Arial" AllowSorting="True">
                      <Columns>
                       <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField> 
                      
                        <asp:BoundField DataField="col1" ShowHeader="true" HeaderText="col1" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col2" ShowHeader="true" HeaderText="col2" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col3" ShowHeader="true" HeaderText="col3" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col4" ShowHeader="true" HeaderText="col4" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                    </Columns>
                </asp:GridView>

更改后前端代码:

<asp:GridView ID="grid_01" runat="server"  OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%"   Font-Names="Arial" AllowSorting="True">
                      <Columns>
                       <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField> 

                        <asp:TemplateField ShowHeader="true" HeaderText="col1" HeaderStyle-BackColor="SkyBlue"
                            ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col1" runat="server" Text='<%# Eval("col1") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col1" runat="server" Text='<%# Eval("col1") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField ShowHeader="true" HeaderText="col2" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col2" runat="server" Text='<%# Eval("col2") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col2" runat="server" Text='<%# Eval("col2") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField ShowHeader="true" HeaderText="col3" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col3" runat="server" Text='<%# Eval("col3") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col3" runat="server" Text='<%# Eval("col3") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
                     
                         <asp:TemplateField ShowHeader="true" HeaderText="col4" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col4" runat="server" Text='<%# Eval("col4") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col4" runat="server" Text='<%# Eval("col4") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
                               
                    </Columns>
                </asp:GridView>


后台更改颜色代码:

protected void grid_01_RowDataBound(object sender, GridViewRowEventArgs e) {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (e.Row.RowType == DataControlRowType.DataRow)    //不包括第一行表头
            {
                if (e.Row.Cells[1].Text == "Y")
                {
                    e.Row.Cells[1].Attributes.Add("style", "background-color:DodgerBlue  ");
                }
                if (!string.IsNullOrEmpty(drv["number"].ToString()))   //单元格不为空
                {
                    if (int.Parse(e.Row.Cells[2].Text) < 0)
                    {
                        e.Row.Cells[2].Attributes.Add("style", "color:red  ");
                    }
                }
              Response.Write(e.Row.Cells[4].Text);
                if (!string.IsNullOrEmpty(drv["x"].ToString()) && !string.IsNullOrEmpty(drv["y"].ToString()))   //单元格不为空
                {
                    if (DateTime.Compare(Convert.ToDateTime(e.Row.Cells[4].Text), Convert.ToDateTime(e.Row.Cells[3].Text)) > 0)     //y大于x
                    {
                        e.Row.Cells[4].Attributes.Add("style", "color:red  ");
                    }
                }
            }
        }

代码太长,这里做了简化处理,更改前端代码后,col1和col2的颜色能更改,col1值是Y或N,col2值是整数。
但是日期型就不行了,col3,col4都是日期,前端数据正常展现,Response就获取不到值了,没有东西打印出来,更改颜色更不行,报格式错误。而用原来的前端代码是可以取出日期数据的,实在是奇怪。

  • 写回答

2条回答 默认 最新

  • Yijing Sun 2021-09-02 10:30
    关注

    您好,
    当你使用TemplateField时, 不能使用e.Row.Cell[] 去得到值. 你可以使用findcontrol. 请参考以下代码:

    <asp:GridView ID="grid_01" runat="server" OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%" Font-Names="Arial" AllowSorting="True" AutoGenerateEditButton="true" OnRowEditing="grid_01_RowEditing" OnRowCancelingEdit="grid_01_RowCancelingEdit">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="true" HeaderText="col1" HeaderStyle-BackColor="SkyBlue"
                        ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col1" runat="server" Text='<%# Eval("col1") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col1" runat="server" Text='<%# Eval("col1") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col2" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col2" runat="server" Text='<%# Eval("col2") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col2" runat="server" Text='<%# Eval("col2") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col3" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col3" runat="server" Text='<%# Eval("col3") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col3" runat="server" Text='<%# Eval("col3") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col4" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col4" runat="server" Text='<%# Eval("col4") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col4" runat="server" Text='<%# Eval("col4") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("col1", typeof(string)),
                        new DataColumn("col2", typeof(int)),
                        new DataColumn("col3", typeof(DateTime)),
                        new DataColumn("col4", typeof(DateTime))
                    });
                    dt.Rows.Add("Y", 1, "2020/5/2", "2021/8/5");
                    dt.Rows.Add("N", 2, "2022/4/4", "2023/9/8");
                    grid_01.DataSource = dt;
                    grid_01.DataBind();
                }
            }
    
            protected void grid_01_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (e.Row.RowType == DataControlRowType.DataRow)    //不包括第一行表头
                {
                    Label lab1 = (Label)e.Row.FindControl("lbl_col1");
                    Label lab2 = (Label)e.Row.FindControl("lbl_col2");
                    Label lab3 = (Label)e.Row.FindControl("lbl_col3");
                    Label lab4 = (Label)e.Row.FindControl("lbl_col4");
                    if (lab1.Text == "Y")
                    {
                        lab1.Attributes.Add("style", "background-color:DodgerBlue");
                    }
                    if (!string.IsNullOrEmpty(drv["col2"].ToString()))   //单元格不为空
                    {
                        if (Convert.ToInt32(lab2.Text) < 0)
                        {
                            lab2.Attributes.Add("style", "color:red");
                        }
                    }
                    Response.Write(lab3.Text);
                    if (!string.IsNullOrEmpty(drv["col3"].ToString()) && !string.IsNullOrEmpty(drv["col4"].ToString()))   //单元格不为空
                    {
                        if (DateTime.Compare(Convert.ToDateTime(lab4.Text), Convert.ToDateTime(lab3.Text)) > 0)     //y大于x
                        {
                            lab3.Attributes.Add("style", "color:red");
                        }
                    }
    
                }
            }
    
            protected void grid_01_RowEditing(object sender, GridViewEditEventArgs e)
            {
    
            }
    
            protected void grid_01_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
    
            }
    
    

    结果:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 创建了问题 9月1日

悬赏问题

  • ¥15 gwas 分析-plink 检查人口分层出现下面问题
  • ¥15 关于#matlab#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 vue3使用swiper轮播图循环失败
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音
  • ¥20 树莓派4b使用Camera Module 3时出现the system should be configured for the legacy camera stack问题
  • ¥200 GitHub开源程序配置在VScode调试