自定义创建的复选框、事件处理程序等-vb.NET asp.NET



我试图在网格视图中管理checkall控件,但我遇到了几个问题。

这是我的网格视图控件:

<asp:GridView ID="gvShow" runat="server" AutoPostback="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Height="224px" HorizontalAlign="Center" Width="761px" CellPadding="4" ForeColor="#333333" DataSourceID="sid_db">

<AlternatingRowStyle BackColor="White" ForeColor="#284775" />

<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<HeaderTemplate>
<asp:CheckBox ID="chkCheckAll" runat="server" AutoPostBack="False" OnCheckedChanged="chkCheckAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkCheck" runat="server" />
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="Scope" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
<asp:BoundField DataField="Site ID" HeaderText="Site ID" SortExpression="Site ID" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name" SortExpression="Site Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="CAP" HeaderText="CAP" SortExpression="CAP" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Master_ID" HeaderText="Master_ID" SortExpression="Master_ID" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
</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>

第一个问题:复选框控件是动态创建的,老实说,我觉得我应该为它们创建一个事件处理程序,但我不知道如何做到这一点,也不知道将代码放在哪里/写在哪里。我只是看到,一旦我试图在代码中引用这些控件,它们就不会被识别。

第二个问题:由于我几乎是一个新手,如果我能够在页面加载时对所有复选框进行常规检查,我会后退一步,用一些垃圾代码进行测试。。。这是我的测试。。。结果完全失败了,什么也没发生。我甚至尝试将代码放入init事件中,但结果是一样的。

For Each row As GridViewRow In gvShow.Rows
DirectCast(row.FindControl("chkCheck"), CheckBox).Checked = True
Next

如果有人能帮我解决这些问题,我将不胜感激。提前感谢您抽出时间。

问候,

我自己找到了答案。我不得不更改代码如下:

<asp:GridView ID="gvShow" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Height="224px" HorizontalAlign="Center" Width="761px" CellPadding="4" ForeColor="#333333">

<AlternatingRowStyle BackColor="White" ForeColor="#284775" />

<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<HeaderTemplate>
<asp:CheckBox ID="ChkSelectAll" runat="server" Autopostback="True" OnCheckedChanged="ChkSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChkSigleItem" runat="server" OnCheckedChanged="ChkSigleItem_CheckedChanged" />
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="Scope" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
<asp:BoundField DataField="Site ID" HeaderText="Site ID" SortExpression="Site ID" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name" SortExpression="Site Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="CAP" HeaderText="CAP" SortExpression="CAP" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Master_ID" HeaderText="Master_ID" SortExpression="Master_ID" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
</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>

然后:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
''Set value for default SQL query
Dim query As String
query = "SELECT Sites.Master_ID AS Master_ID, Scopes.Name AS Scope, Brands.Extension AS [Brand], Sites.ID AS [Site ID], Sites.Name AS [Site Name], Sites.Address, Sites.CAP, Sites.City, Countries.Name AS Country FROM Sites INNER JOIN Scopes ON Sites.scope_ID = Scopes.ID INNER JOIN Brands ON Sites.brand_ID = Brands.ID INNER JOIN Countries ON Sites.country_ID = Countries.ID"
sid_db.SelectCommand = query
gvShow.DataSource = sid_db
gvShow.DataBind()
End If
End Sub
Protected Sub ChkSelectAll_CheckedChanged(sender As Object, e As EventArgs)
Dim a As Boolean = DirectCast(sender, CheckBox).Checked
For Each row As GridViewRow In gvShow.Rows
Dim cbx As CheckBox = row.FindControl("ChkSigleItem")
cbx.Checked = a
Next
End Sub

最新更新