结构不工作的Gridview数据绑定

时间:2014-03-27 16:06:31

标签: asp.net gridview

我有一个在类中定义结构的结构,在填充结构后,我将它绑定到gridview。在调试gridview_OnRowDataBound(...)时,gridview单元格不显示任何数据。但是,UI上的gridview显示数据。不确定它是否是回发,但我确认在回发时page_load没有变化。任何指针都会有所帮助。

public partial Class1
{
....
....
        [Serializable]
        public struct StructSelectedSiteList
        {
            public string SiteName
            {
                get;
                set;
            }

            public string RncName
            {
                get;
                set;
            }

            public string Status
            {
                get;
                set;
            }

            public int TaskId
            {
                get;
                set;
            }

            public string IssueType
            {
                get;
                set;
            }

            public string ReworkSource
            {
                get;
                set;
            }

            public string ReworkReason
            {
                get;
                set;
            }
        };
....

声明当地人

var lstSites = new List<StructSelectedSiteList>();

填充结构

// populate the struct for selected sites
                        StructSelectedSiteList structSelectedSiteList = new StructSelectedSiteList();
                        structSelectedSiteList.SiteName = lblSiteName.Text;
                        structSelectedSiteList.Status = ddSiteStatusUpdate.SelectedValue.Trim();
                        structSelectedSiteList.TaskId = taskId;
                        structSelectedSiteList.IssueType = ddIssues.SelectedValue.Trim();
                        structSelectedSiteList.ReworkSource = reworkGrp.SelectedValue;
                        structSelectedSiteList.ReworkReason = ddReworkReason.SelectedValue;
                        structSelectedSiteList.RncName = lblRNCName.Text;

lstSites.Add(structSelectedSiteList);

在每个gridview行数据绑定中

      protected void gvSelectedSites_OnRowDataBound(object sender, GridViewRowEventArgs e)
        { 

....
....
// check if task id is bound
             if (e.Row.RowType == DataControlRowType.DataRow)
             {
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[0].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[1].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[2].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[3].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[4].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[5].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[6].Text);
                 System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[7].Text);
             }
}

UI gridview设计

<asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True"
                                CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI"
                                Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center"
                                EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>                                        
                                    <asp:TemplateField HeaderText="Site Name">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SiteName")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="RNC Name">
                                        <ItemTemplate>
                                            <asp:Label ID="lblRNCName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "RncName")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Status">
                                        <ItemTemplate>
                                            <asp:Label ID="lblStatus" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Status")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Issue Type">
                                        <ItemTemplate>
                                            <asp:Label ID="lblIssueType" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "IssueType")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Task" Visible="false">
                                        <ItemTemplate>
                                            <asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "TaskId")%>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>                                        
                                    <asp:TemplateField HeaderText="Rework Source">
                                        <ItemTemplate>
                                            <asp:Label ID="lblReworkSource" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ReworkSource")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Rework Reason">
                                        <ItemTemplate>
                                            <asp:Label ID="lblReworkReason" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ReworkReason")%>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Site Comments">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtSiteComments" runat="server" Width="350px" Height="50px" TextMode="MultiLine"
                                                Rows="5"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>

1 个答案:

答案 0 :(得分:2)

我相信网格有数据,你只是看错了地方。请注意,对于您使用模板的每个字段,意味着单元格本身确实有文本,而是一些内部控件。尝试访问这些控件,很可能会看到数据:

// example for site name
Label label = (Label)e.Row.FindControl("lblSiteName");
System.Diagnostics.Debug.WriteLine("data contents - " + label.Text);