【问题标题】:row data bound problem行数据绑定问题
【发布时间】:2010-02-18 07:05:31
【问题描述】:

我正在使用 gridview 的 rowdatabound 事件在 gridview 列中进行一些格式化。但是当我使用即时窗口执行代码并调试它时,我在 e.Row.Cells[1].Text 中没有得到任何东西。我正在从数据表中填充网格视图。它正在显示记录,但我不知道为什么它没有进入 rowdatabound。
以下是我的绑定代码

<asp:GridView runat="server" AutoGenerateColumns="False"  
                        ID="gviewTemplate" onrowdatabound="gviewTemplate_RowDataBound" DataKeyNames="F1"
                        onrowcommand="gviewTemplate_RowCommand" 
                        onrowediting="gviewTemplate_RowEditing" 
                        onrowcancelingedit="gviewTemplate_RowCancelingEdit" 
                        onrowupdating="gviewTemplate_RowUpdating">
                        <Columns>
                            <asp:TemplateField>
                                <EditItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("F1") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Bind("F1") %>' ID="lblID1"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Uploaded Image">
                            <EditItemTemplate>
                                <asp:LinkButton Text="Reload" runat="server" CommandArgument='<%# Bind("F1") %>' CommandName="reload" ID="lbtnReloadImage"></asp:LinkButton>
                            </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Eval("Uploaded") %>' ID="lblUploaded"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Template Name">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtTemplateName" Width="50" Runat="server" Text='<%# Eval("F2") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtTemplateName">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblTemplateName" runat="server" Text='<%# Eval("F2") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Heading">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtHeading" Runat="server" Width="50" Text='<%# Eval("F3") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtHeading">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblHeading" runat="server" Text='<%# Eval("F3") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Coupon Text">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtCouponText" Runat="server" Width="50" Text='<%# Bind("F4") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtCouponText">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Bind("F4") %>' ID="lblCouponText"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

这就是我在 rowdatabound 中所做的事情

protected void gviewTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.Cells[1].Text != e.Row.Cells[2].Text)
        {
            e.Row.BackColor = System.Drawing.Color.Red;
        }
 }

我不明白记录是否显示在网格中,为什么我不能在 rowdatabound 中得到它

【问题讨论】:

    标签: gridview


    【解决方案1】:

    因为你使用的是网格中的模板字段,所以尝试调试并按住单元格,然后尝试在其中找到文本框,然后读取它的值

    【讨论】:

    • 但是我现在如何改变单元格背景的颜色
    • 您可以在表格或面板之类的模板中放置一个 cxontainer,然后在其上应用背景颜色
    • 嗨,泰坦,谢谢你的代码正在运行,你能告诉我一件事,有没有一种方法可以让表格占据网格内单元格的所有空间。我的意思是表格在一个单元格中,我希望它占据表格的所有空间。
    【解决方案2】:

    试试这个:
    您需要检查行类型,因为在行数据绑定中它还包括页眉、页脚

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow & (e.Row.RowState == DataControlRowState.Normal | e.Row.RowState == DataControlRowState.Alternate)) {
                //your code here
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 2017-02-16
      • 2011-10-18
      • 2021-01-04
      • 2011-09-12
      相关资源
      最近更新 更多