我在Page_PreRender事件中编写了一些代码,它获取DropDownList控件的值,然后执行一些条件逻辑。问题是,逻辑一定不是很好。
第一个If ..然后.. Else ..结构工作正常,但是,如果selectedItem 不&#34; Todos&#34; (葡萄牙语为&#34;所有&#34;)所有行&#39;可见性将设置为false。当If SelectedItem <> "Todos" Then
条件得到满足时,似乎会出现这个问题。
因此,除了&#34; Todos&#34;之外,DropDownList中的每个选项都是如此。将所有行的可见性设置为False,而不是将不匹配SelectedItem的可见性设置为False。
Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
'Getting the selected item
Dim SelectedItem As String = CType(GridView1.HeaderRow.Cells(3).FindControl("DropDownList1"), DropDownList).SelectedItem.Text
If SelectedItem <> "Todos" Then
'This is the part where the issue seems to be located.
For Each Row As GridViewRow In GridView1.Rows
'Get the Text value
Dim depar As String = Row.Cells(3).Text
'If its different from the SelectedItem then the row's visible property should be set to false, if it's a match, set it to true.
If depar <> SelectedItem Then
Row.Visible = False
ElseIf depar = SelectedItem Then
Row.Visible = True
End If
Next
'If "Todos" was selected, all rows should be visible
ElseIf SelectedItem = "Todos" Then
'Make all rows visible
For Each row As GridViewRow In GridView1.Rows
row.Visible = True
Next
End If
End Sub
GridView代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" PageSize="20" Width="100%" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#CType(Container, GridViewRow).DataItem("IdPedido") %>' OnClick="LinkButton1_Click" OnCommand="LinkButton1_Command">Ver</asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:BoundField DataField="Observacoes" HeaderText="Assunto" SortExpression="Observacoes" />
<asp:BoundField DataField="NomeHotel" HeaderText="Hotel" SortExpression="NomeHotel" />
<asp:TemplateField HeaderText="Departamento" SortExpression="NomeDepartamento">
<HeaderTemplate>
Departamento<br />
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSource1" DataTextField="NomeDepartamento" DataValueField="IdDepartamento" AutoPostBack="True" OnLoad="DropDownList1_Load" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Todos</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EnotelSuporteConnectionString1 %>" SelectCommand="SELECT [NomeDepartamento], [IdDepartamento] FROM [Departamentos]"></asp:SqlDataSource>
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NomeDepartamento") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeDepartamento") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DataPedido" HeaderText="Data" DataFormatString = "{0:dd, MMM yyyy}" SortExpression="DataPedido" />
<asp:BoundField DataField="DataFinalizado" HeaderText="Finalizado a" DataFormatString = "{0:dd, MMM yyyy}" SortExpression="DataFinalizado" />
<asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
<asp:BoundField DataField="Estado" HeaderText="Estado" SortExpression="Estado" Visible="False" />
<asp:BoundField DataField="IdPedido" HeaderText="IdPedido" InsertVisible="False" ReadOnly="True" SortExpression="IdPedido" Visible="False" />
</Columns>
</asp:GridView>
答案 0 :(得分:0)
Row.Cells(3).Text
中的问题是,您使用ItemTemplate
定义了gridview
中的Label
,并获得了ItemTemplates
值找到控件,然后从该控件中获取值。
如果您想要标签的值,请使用以下代码获取值
Label label = (Label)Row.Cells(3).FindControl("Label1");
if(label != null)
string depar = label.Text;
注意强>
代码在c#中,您可能希望将其转换为VB。