我是 ASP.NET 的新手。我整个早上都在尝试,但无法让它发挥作用。在我的 DataList1 中,有一个子数据列表 (childList),其中列出了公司名称和复选框。我还将 CompanyID 存储在隐藏字段 (hiddenCompanyID) 中。
我喜欢做的是当用户选中一个框(在子数据列表下)时,它将触发事件(chkChildCompany_CheckedChanged)并从隐藏字段中捕获 CompanyID,以便我可以处理数据库更新。我不知道如何在子 Datalist 中找到隐藏字段控件。
请帮忙,谢谢,
<asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyListPartialMatch" runat="server" Width="80%" DataKeyField="Company1Word"
UseAccessibleHeader="true"
CssClass="books"
HeaderStyle-CssClass="header"
ItemStyle-CssClass="item"
AlternatingItemStyle-CssClass="alternating"
GridLines="Both"
CellPadding="0"
CellSpacing="0" BorderColor="Black"
ItemStyle-BorderColor="Black" BorderWidth="0"
HorizontalAlign="Center"
RepeatDirection="Vertical"
>
<HeaderTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; "></th>
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">Num</th>
<th style="width: 70%; border-right:1px solid black; border-spacing:0; text-align:center; ">Company Name</th>
<th style="width: 10%; border-right:1px solid black; border-spacing:0; text-align:center; ">Add?</th>
</tr>
</table>
</HeaderTemplate>
<ItemStyle BorderColor="black" Font-Size="Medium" />
<ItemTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">
<asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>'
OnCommand="LinkButton1_Command"
Font-Underline="false"
Height="25"
Font-Bold="true"
></asp:LinkButton>
</td>
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:right; padding-right:10px;"><%#Eval("Row")%></td>
<td style="width: 70%"><asp:Literal ID="litFoo" runat="server" Text='<%#Eval("Company")%>' /> </td>
<td style="width: 10%;text-align:right;">
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() == "") ? "": "display: none" %>">
<asp:CheckBox id="check1" runat="server" />
</div>
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() != "") ? "": "display: none" %>">
<asp:HyperLink
ID="HyperLink1"
runat="server" ForeColor="Blue"
Text='<%# Eval("CompanyID")%>'
NavigateUrl='<%# Eval("CompanyID", "/Apps/ERP/Other/CompanyInfo.asp?CompanyID={0}")%>'
/>
</div>
</td>
<asp:Label ID="lblRow" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "Row") %>' />
</tr>
</table>
<asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;">
<asp:DataList ID="childList" runat="server" Width="100%">
<ItemTemplate>
<div class="div_hover">
<table class="table1" width="80%">
<tr>
<td style="width: 60%; border-right:0px solid black; border-spacing:0;">• <%#Eval("CompanyName")%></td>
<td style="width: 20%;text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td>
<td style="width: 20%;text-align:right;">
<asp:CheckBox id="chkChildCompany" runat="server" value="123Test"
AutoPostBack="true"
OnCheckedChanged="chkChildCompany_CheckedChanged" /></td>
<asp:HiddenField ID="hiddenCompanyID" runat="server" Value='<%#Eval("CompanyID") %>' />
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
复选框事件
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
//Process through child DataList (childList)
//Capture the CompanyID from hidden field value if checkbox is checked;
// Then I will update the database record with the ID....
}