在DataList

时间:2019-03-25 19:02:53

标签: asp.net data-binding datalist

我在网页上有一个ASP.Net 4.6.1 DataList,其中包含用于OnItemDataBound和OnItemCreated的方法:

<asp:DataList runat="server" ID="dlAssocDetail" HorizontalAlign="Center" OnEditCommand="dlAssocDetail_EditCommand" OnUpdateCommand="dlAssocDetail_UpdateCommand"
    OnCancelCommand="dlAssocDetail_CancelCommand" OnItemCommand="dlAssocDetail_ItemCommand" OnItemDataBound="dlAssocDetail_ItemDataBound" OnItemCreated="dlAssocDetail_ItemCreated">
    <ItemTemplate>
        <asp:Table runat="server" Font-Names="Arial" HorizontalAlign="Center" CellPadding="20" CellSpacing="20" BackColor="WhiteSmoke" BorderColor="LightGray">
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>User ID: </b><asp:Label runat="server" ID="lbEID" Text=<%# Bind("EmployeeID") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Attorney Name: </b><asp:Label runat="server" ID="lbName" Text=<%# Bind("AttorneyName") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Title: </b><asp:Label runat="server" ID="lbTitle" Text=<%# Bind("Title") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Anniversary Date: </b><asp:Label runat="server" ID="lbADate" Text=<%# Bind("AnniversaryDate") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Balance: </b><asp:Label runat="server" ID="lbBalance" Text=<%# Bind("Balance") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Allotment Amount: </b><asp:Label runat="server" ID="lbAAmt" Text=<%# Bind("AllotmentAmount") %> />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table><br />
        <asp:Table runat="server" Font-Names="Arial" HorizontalAlign="Center" CellPadding="20" CellSpacing="20" BackColor="WhiteSmoke" BorderColor="LightGray">
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Description: </b><asp:Label runat="server" ID="lbDesc" Text=<%# Bind("Description") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Approved: </b><asp:CheckBox runat="server" ID="cbApproved" Enabled="false" Checked=<%# Bind("Approved") %> />
                    <b>Rejected: </b><asp:CheckBox runat="server" ID="cbRejected" Enabled="false" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Status: </b><asp:Label runat="server" ID="lbStatus" Text=<%# Bind("OrderStatus") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Request Date: </b><asp:Label runat="server" ID="lbReqDate" Text=<%# Bind("RequestDate") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Order Date: </b><asp:Label runat="server" ID="lbOrderDate" Text=<%# Bind("OrderDate") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Received Date: </b><asp:Label runat="server" ID="lbReceivedDate" Text=<%# Bind("ReceivedDate") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid/Reimb Date: </b><asp:Label runat="server" ID="lbPaidDate" Text=<%# Bind("Paid_ReimbDate") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Total Amount: </b><asp:Label runat="server" ID="lbTotal" Text=<%# Bind("TotalAmount") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid By Firm: </b><asp:Label runat="server" ID="lbByFirm" Text=<%# Bind("Paid_Reimb_Amt_ByFirm") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid By Attorney: </b><asp:Label runat="server" ID="lbByAttorney" Text=<%# Bind("PaidAmountByAttorney") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Operating System: </b><asp:RadioButtonList runat="server" ID="rblOS" Enabled="false" SelectedValue=<%# Bind("OSID") %> 
                                                RepeatDirection="Horizontal">
                                                <asp:ListItem Text="Apple" Value="1" />
                                                <asp:ListItem Text="Windows" Value="2" />
                                             </asp:RadioButtonList>
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Device Type: </b><asp:RadioButtonList runat="server" ID="rblDType" Enabled="false" SelectedValue=<%# Bind("DeviceTypeID") %> 
                                        RepeatDirection="Horizontal">
                                        <asp:ListItem Text="Desktop" Value="1" />
                                        <asp:ListItem Text="Laptop" Value="2" />
                                        <asp:ListItem Text="iPad" Value="3" />
                                        <asp:ListItem Text="Peripheral" Value="4" />
                                      </asp:RadioButtonList>
                </asp:TableCell>
                <asp:TableCell runat="server" ID="tcSpecs">
                    <b>Specs: </b><br /><asp:HyperLink runat="server" ID="hlSpecs" Enabled="false" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Upload: </b><asp:FileUpload runat="server" ID="fluUpload" AllowMultiple="true" Enabled="false" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <asp:Button runat="server" ID="btnEdit" CommandName="Edit" Text="Edit" Font-Bold="true" Font-Names="Arial" 
                                    BackColor="WhiteSmoke" BorderStyle="Outset" Height="40" Width="100" Font-Size="Large" />
                    <asp:Button runat="server" ID="btnNew" CommandName="New" Text="New" Font-Bold="true" Font-Names="Arial" 
                                    BackColor="WhiteSmoke" BorderStyle="Outset" Height="40" Width="100" Font-Size="Large" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server" ColumnSpan="3" HorizontalAlign="Center">
                    <asp:Label runat="server" ID="lbSuccess" Font-Bold="true" Font-Names="Arial" Font-Size="X-Small" ForeColor="Red" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Table runat="server" Font-Names="Arial" HorizontalAlign="Center" CellPadding="20" CellSpacing="20" BackColor="WhiteSmoke" BorderColor="LightGray">
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>User ID: </b><asp:Label runat="server" ID="lbEID" Text=<%# Bind("EmployeeID") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Attorney Name: </b><asp:Label runat="server" ID="lbName" Text=<%# Bind("AttorneyName") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Title: </b><asp:Label runat="server" ID="lbTitle" Text=<%# Bind("Title") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Anniversary Date: </b><asp:Label runat="server" ID="lbADate" Text=<%# Bind("AnniversaryDate") %> />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Balance: </b><asp:Label runat="server" ID="lbBalance" Text=<%# Bind("Balance") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Allotment Amount: </b><asp:Label runat="server" ID="lbAAmt" Text=<%# Bind("AllotmentAmount") %> />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table><br />
        <asp:Table runat="server" Font-Names="Arial" HorizontalAlign="Center" CellPadding="20" CellSpacing="20" BackColor="WhiteSmoke" BorderColor="LightGray">
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Description: </b><asp:TextBox runat="server" ID="tbDesc" Text=<%# Bind("Description") %> />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Approved: </b><asp:CheckBox runat="server" ID="cbApproved" Checked=<%# Bind("Approved") %> />
                    <b>Rejected: </b><asp:CheckBox runat="server" ID="cbRejected" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Status: </b><asp:DropDownList runat="server" ID="ddlStatus" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Request Date: </b><asp:ImageButton runat="server" ID="ibReqDate" ImageUrl="~/images/Calendar-icon.png" AlternateText="Click here to display calendar" />
                    <asp:TextBox runat="server" ID="tbReqDate" Text=<%# Bind("RequestDate") %> />
                    <ajax:CalendarExtender runat="server" ID="ceReqDate" TargetControlID="tbReqDate" PopupButtonID="ibReqDate" />
                    <asp:ImageButton runat="server" ID="ibDelReqDate" ImageUrl="~/images/cal_nodate.gif" /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+[-/][0-9]+[-/][0-9][0-9][0-9][0-9]$" 
                        ControlToValidate="tbReqDate" ErrorMessage="Correct Format is MM/DD/YYYY" Font-Bold="true" ForeColor="Red" 
                        SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Order Date: </b><asp:ImageButton runat="server" ID="ibOrderDate" ImageUrl="~/images/Calendar-icon.png" AlternateText="Click here to display calendar" />
                    <asp:TextBox runat="server" ID="tbOrderDate" Text=<%# Bind("OrderDate") %> />
                    <ajax:CalendarExtender runat="server" ID="ceOrderDate" TargetControlID="tbOrderDate" PopupButtonID="ibOrderDate" />
                    <asp:ImageButton runat="server" ID="ibDelOrderDate" ImageUrl="~/images/cal_nodate.gif" /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+[-/][0-9]+[-/][0-9][0-9][0-9][0-9]$" 
                        ControlToValidate="tbOrderDate" ErrorMessage="Correct Format is MM/DD/YYYY" Font-Bold="true" ForeColor="Red" 
                        SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Received Date: </b><asp:ImageButton runat="server" ID="ibReceivedDate" ImageUrl="~/images/Calendar-icon.png" AlternateText="Click here to display calendar" />
                    <asp:TextBox runat="server" ID="tbReceivedDate" Text=<%# Bind("ReceivedDate") %> />
                    <ajax:CalendarExtender runat="server" ID="ceReceivedDate" TargetControlID="tbReceivedDate" PopupButtonID="ibReceivedDate" />
                    <asp:ImageButton runat="server" ID="ibDelReceivedDate" ImageUrl="~/images/cal_nodate.gif" /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+[-/][0-9]+[-/][0-9][0-9][0-9][0-9]$" 
                        ControlToValidate="tbReceivedDate" ErrorMessage="Correct Format is MM/DD/YYYY" Font-Bold="true" ForeColor="Red" 
                        SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid/Reimb Date: </b><asp:ImageButton runat="server" ID="ibPaidDate" ImageUrl="~/images/Calendar-icon.png" AlternateText="Click here to display calendar" />
                    <asp:TextBox runat="server" ID="tbPaidDate" Text=<%# Bind("Paid_ReimbDate") %> />
                    <ajax:CalendarExtender runat="server" ID="cePaidDate" TargetControlID="tbPaidDate" PopupButtonID="ibPaidDate" />
                    <asp:ImageButton runat="server" ID="ibDelPaidDate" ImageUrl="~/images/cal_nodate.gif" /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+[-/][0-9]+[-/][0-9][0-9][0-9][0-9]$" 
                        ControlToValidate="tbPaidDate" ErrorMessage="Correct Format is MM/DD/YYYY" Font-Bold="true" ForeColor="Red" 
                        SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Total Amount: </b><asp:TextBox runat="server" ID="tbTotal" Text=<%# Bind("TotalAmount") %> /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+\.{0,1}[0-9]{0,2}$" ControlToValidate="tbTotal" 
                        ErrorMessage="Must be a number" Font-Bold="true" ForeColor="Red" SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid By Firm: </b><asp:TextBox runat="server" ID="tbByFirm" Text=<%# Bind("Paid_Reimb_Amt_ByFirm") %> /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+\.{0,1}[0-9]{0,2}$" ControlToValidate="tbByFirm" 
                        ErrorMessage="Must be a number" Font-Bold="true" ForeColor="Red" SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Paid By Attorney: </b><asp:TextBox runat="server" ID="tbByAttorney" Text=<%# Bind("PaidAmountByAttorney") %> /><br />
                    <asp:RegularExpressionValidator runat="server" ValidationExpression="^[0-9]+\.{0,1}[0-9]{0,2}$" ControlToValidate="tbByAttorney" 
                        ErrorMessage="Must be a number" Font-Bold="true" ForeColor="Red" SetFocusOnError="true" Font-Size="XX-Small" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <b>Operating System: </b><asp:RadioButtonList runat="server" ID="rblOS" SelectedValue=<%# Bind("OSID") %> 
                                                RepeatDirection="Horizontal">
                                                <asp:ListItem Text="Apple" Value="1" />
                                                <asp:ListItem Text="Windows" Value="2" />
                                             </asp:RadioButtonList>
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Device Type: </b><asp:RadioButtonList runat="server" ID="rblDType" SelectedValue=<%# Bind("DeviceTypeID") %> 
                                        RepeatDirection="Horizontal">
                                        <asp:ListItem Text="Desktop" Value="1" />
                                        <asp:ListItem Text="Laptop" Value="2" />
                                        <asp:ListItem Text="iPad" Value="3" />
                                        <asp:ListItem Text="Peripheral" Value="4" />
                                      </asp:RadioButtonList>
                </asp:TableCell>
                <asp:TableCell runat="server" ID="tcSpecs">
                    <b>Specs: </b><asp:HyperLink runat="server" ID="hlSpecs" />
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <b>Upload: </b><asp:FileUpload runat="server" ID="fluUpload" AllowMultiple="true" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <asp:Button runat="server" ID="btnUpdate" CommandName="Update" Text="Update" Font-Bold="true" Font-Names="Arial" 
                                    BackColor="WhiteSmoke" BorderStyle="Outset" Height="40" Width="100" Font-Size="Large" />
                    <asp:Button runat="server" ID="btnCancel" CommandName="Cancel" Text="Cancel" Font-Bold="true" Font-Names="Arial" 
                                    BackColor="WhiteSmoke" BorderStyle="Outset" Height="40" Width="100" Font-Size="Large" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
                <asp:TableCell runat="server" ColumnSpan="3" HorizontalAlign="Center">
                    <asp:Label runat="server" ID="lbSuccess" Font-Bold="true" Font-Names="Arial" Font-Size="X-Small" ForeColor="Red" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </EditItemTemplate>
</asp:DataList>

我正在尝试在DataList中找到TableCell。 TableCell的ID为tcSpecs。当ItemDataBound或ItemCreated事件触发时,它看不到tcSpecs,即tcSpecs保持为空。这是我的代码:

protected void dlAssocDetail_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        TableCell tc = (TableCell)e.Item.FindControl("tbSpecs");
    }

    protected void dlAssocDetail_ItemCreated(object sender, DataListItemEventArgs e)
    {
        TableCell tc = (TableCell)e.Item.FindControl("tbSpecs");
    }

我在做什么错?我如何找到这个TableCell?

备份一下,我要完成的工作是在页面上动态创建一堆HyperLink控件。我试图找到TableCell,所以我有地方要放它们。如果有人可以建议使用TableCell的替代方法,我也很乐意。非常感谢。

1 个答案:

答案 0 :(得分:0)

我知道了。首先,您必须找到表格,然后是TableRow,然后是TableCell。所以:

protected void dlAssocDetail_ItemDataBound(object sender, DataListItemEventArgs e)
{
    Table ta = (Table)e.Item.FindControl("myTable");
    foreach (TableRow row in ta.Rows)
       foreach (TableCell cell in row.Cells)
         //do something
}