vb.net 包含 3 个按钮的单选按钮列表在选定索引在一种情况下从索引 2 更改为 0 时不触发
vb.net radiobuttonlist containing 3 buttons not firing when selectedindexchanging from index 2 to 0 under one condition
我的代码使用 sqldataadapter 和数据集填充 DataGrid 控件(vb.net 3.5 框架),并根据其他控件组合的选择进行过滤。一切正常,除了 RadioButtonList1 在一种情况下有 3 个 rbtns:
在先前选择了 rbtn 3 @ index 2 并且正确返回 NO DATA 之后选择单选按钮 1 @ index 0 时会出现此问题。
如果之前选择了 rbtn 3 @ index 2 并且它正确返回了一些数据,则不会发生这种情况。 radiobuttonlist 选择的所有其他组合都正确运行
我尝试过使用不同的 sqldataadapter 填充数据网格 - 请参阅代码,包括使用存储过程(但如果我这样做,我将无法自定义排序)。似乎没有什么能改变这种奇怪的行为。
如果没人能帮我看清情况,我可能不得不求助于将单选按钮 1 更改为复选框控件并使用单独的绑定方法。谁能看出我遗漏了什么?
ASPX(为简洁起见,数据网格仅显示 2 列)
<asp:radiobuttonlist id="RadioButtonList1" runat="server" AutoPostBack="True"
Font-Names="Arial"
Font-Size="X-Small"
BorderColor="Black"
ForeColor="Black"
RepeatDirection="Vertical"
TextAlign="Right" CssClass="radioWithProperWrap" RepeatColumns="1">
<asp:ListItem Value="0" >All Projects</asp:ListItem>
<asp:ListItem Value="1" Selected="True" >Sort By Project#</asp:ListItem>
<asp:ListItem Value="2">Sort By Project Name</asp:ListItem>
</asp:radiobuttonlist>
<!--------------------
DataGrid1
-------------------->
<asp:datagrid id="DataGrid1" runat="server"
Font-Names="Tahoma,Sans Serif,arial"
Font-Size="Small"
ShowFooter="True"
DataMember="vwPersonSummary"
AllowSorting="True"
OnPageIndexChanged="DataGrid1_Paging" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
UseAccessibleHeader="True" AutoGenerateColumns="False"
CssClass="nowrap" AllowPaging="True" PageSize="110" Visible="False" >
<FooterStyle HorizontalAlign="Right" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></FooterStyle>
<SelectedItemStyle HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="Left" Mode="NumericPages"
Position="TopAndBottom" PageButtonCount="75" />
<AlternatingItemStyle BackColor="#EBF5FF"></AlternatingItemStyle>
<headerStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"
Font-Size="Small" Font-Strikeout="False" Font-Underline="False"
Width="12em" BackColor="#333333" ForeColor="White" ></headerStyle>
<ItemStyle Font-Size="Smaller" Width="12em" />
<Columns>
<asp:TemplateColumn HeaderText="Line#" FooterText="Line#">
<FooterStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Names="Tahoma" Font-Overline="False" Font-Size="Small"
Font-Strikeout="False" Font-Underline="False" ForeColor="White"></headerStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<%#(DataGrid1.PageSize * DataGrid1.CurrentPageIndex) + Container.ItemIndex + 1%> <!--generate row numbers-->
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Person" SortExpression="Employee" HeaderText="Person" >
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Size="Small" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma"
ForeColor="White" > </headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
HorizontalAlign="Left" Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Project#" SortExpression="Project#" HeaderText="Proj#">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma" Font-Size="Small"
ForeColor="White" ></headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left"
Wrap="False" />
</asp:BoundColumn>
</Columns>
</asp:datagrid>
ASPX.VB(仅相关代码)
Sub BindData(ByVal strSortField As String)
''clear grid
DataGrid1.DataSource = Nothing 'original
' TimeCodesSelected variable is populated based on Checkbox selections here.
' Code removed for brevity.
Try
' Selected project
Dim ProjectSelected As String = Left(ddwnProjList.SelectedItem.Text, 7) 'Project#
' convert string dates to DateTime data types
PPBegDate = Convert.ToDateTime(txtBegDate.Text)
PPEndDate = Convert.ToDateTime(txtEndDate.Text)
' Check which radio button is selected
If RadioButtonList1.SelectedValue = 0 Then '0 = All projects
ProjectSelected = String.Empty
'inline sql used to provide sort parameters to allow retaining sort order when paging through grid. sqlQuery is a global variable
sqlQuery = "SELECT Person, Project# WHERE dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
''1=by number, or 2=by name
Else
sqlQuery = "SELECT Person, Project#, WHERE Project#='" & ProjectSelected & "' AND dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
'Verify selected project exists, if not hide grid
Dim blnRecordExists As Boolean
Dim oRecord As DataAccessLib.SqlConn = New DataAccessLib.SqlConn()
blnRecordExists = oRecord.VerifyProjectExists(ProjectSelected, PPBegDate, PPEndDate)
If (blnRecordExists = True) Then
DataGrid1.Visible = True
lblErrorMsg.Text = Nothing
Else
DataGrid1.Visible = False
lblErrorMsg.Text = "No records found for this project number during selected dates."
End If
End If
Using conn As SqlConnection = New SqlConnection(conStr)
Using cmd As New SqlCommand(sqlQuery, conn)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using ds As New DataSet()
sda.Fill(ds)
DataGrid1.DataSource = ds.Tables(0)
End Using
End Using
End Using
''Orignal code that works as well and also has the switch from index 2 to index 0 issue
'SqlDataAdapter1.Fill(DsDetailByProj1, "vwPersonSummary")
'Dim objDataView As DataView = DsDetailByProj1.Tables("vwPersonSummary").DefaultView
'DataGrid1.DataSource = objDataView
DataGrid1.DataBind()
End Using 'end using closes conn and goes before 'Catch'
Catch ex As SqlException
sqlErrorMsg = sqlErrorMsg + ": " + ex.Message.ToString
lblErrorMsg.Visible = True
lblErrorMsg.Text = sqlErrorMsg
Catch e As Exception
'reset grid to 1st page after grid refresh (databind)
DataGrid1.CurrentPageIndex = 0
Finally 'the using statement closes objects. Kept here for reference only.
''close and dispose of database connection
'SqlConnection1.Close()
'SqlConnection1.Dispose()
''close sqldatadapter if applicable
'SqlDataAdapter1.Dispose()
End Try
End Sub
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = 0 Then
ddwnProjList.Enabled = False
Else 'if 1 or 2
ddwnProjList.Enabled = True
End If
GetProjectList()'fills ddwnProjList by Project Number or Project Name
BindData("dtmDate")
End Sub
Private Sub ddwnProjList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddwnProjList.SelectedIndexChanged
If (ViewState("sortField") <> Nothing) Then
BindData(ViewState("sortField").ToString())
Else
BindData("dtmDate")
End If
End Sub
enter code here
正如我在评论中提到的,将 DataGrid 的可见性 属性 设置为 true 是解决此问题的第一步。
然后我使用 Scott Mitchell 的答案 here 来验证网格是否为空。通过消除对数据库的另一次往返的需要,这似乎比我这样做的方式更有效。
If (DataGrid1.Items.Count = 0) Then
lblNoRecordsMessage.Text = "Yo Adrian, there are no records for the selected dates."
lblNoRecordsMessage.Visible = True
DataGrid1.Visible = False
Else
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True
End If
我还确保在 DataBind() 方法开始时将 DataGrid 和 Label 设置为可见,因为它有可能在最后一次数据绑定后处于不可见状态。
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True
我的代码使用 sqldataadapter 和数据集填充 DataGrid 控件(vb.net 3.5 框架),并根据其他控件组合的选择进行过滤。一切正常,除了 RadioButtonList1 在一种情况下有 3 个 rbtns:
在先前选择了 rbtn 3 @ index 2 并且正确返回 NO DATA 之后选择单选按钮 1 @ index 0 时会出现此问题。
如果之前选择了 rbtn 3 @ index 2 并且它正确返回了一些数据,则不会发生这种情况。 radiobuttonlist 选择的所有其他组合都正确运行
我尝试过使用不同的 sqldataadapter 填充数据网格 - 请参阅代码,包括使用存储过程(但如果我这样做,我将无法自定义排序)。似乎没有什么能改变这种奇怪的行为。
如果没人能帮我看清情况,我可能不得不求助于将单选按钮 1 更改为复选框控件并使用单独的绑定方法。谁能看出我遗漏了什么?
ASPX(为简洁起见,数据网格仅显示 2 列)
<asp:radiobuttonlist id="RadioButtonList1" runat="server" AutoPostBack="True"
Font-Names="Arial"
Font-Size="X-Small"
BorderColor="Black"
ForeColor="Black"
RepeatDirection="Vertical"
TextAlign="Right" CssClass="radioWithProperWrap" RepeatColumns="1">
<asp:ListItem Value="0" >All Projects</asp:ListItem>
<asp:ListItem Value="1" Selected="True" >Sort By Project#</asp:ListItem>
<asp:ListItem Value="2">Sort By Project Name</asp:ListItem>
</asp:radiobuttonlist>
<!--------------------
DataGrid1
-------------------->
<asp:datagrid id="DataGrid1" runat="server"
Font-Names="Tahoma,Sans Serif,arial"
Font-Size="Small"
ShowFooter="True"
DataMember="vwPersonSummary"
AllowSorting="True"
OnPageIndexChanged="DataGrid1_Paging" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
UseAccessibleHeader="True" AutoGenerateColumns="False"
CssClass="nowrap" AllowPaging="True" PageSize="110" Visible="False" >
<FooterStyle HorizontalAlign="Right" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></FooterStyle>
<SelectedItemStyle HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="Left" Mode="NumericPages"
Position="TopAndBottom" PageButtonCount="75" />
<AlternatingItemStyle BackColor="#EBF5FF"></AlternatingItemStyle>
<headerStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"
Font-Size="Small" Font-Strikeout="False" Font-Underline="False"
Width="12em" BackColor="#333333" ForeColor="White" ></headerStyle>
<ItemStyle Font-Size="Smaller" Width="12em" />
<Columns>
<asp:TemplateColumn HeaderText="Line#" FooterText="Line#">
<FooterStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Names="Tahoma" Font-Overline="False" Font-Size="Small"
Font-Strikeout="False" Font-Underline="False" ForeColor="White"></headerStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<%#(DataGrid1.PageSize * DataGrid1.CurrentPageIndex) + Container.ItemIndex + 1%> <!--generate row numbers-->
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Person" SortExpression="Employee" HeaderText="Person" >
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Size="Small" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma"
ForeColor="White" > </headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
HorizontalAlign="Left" Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Project#" SortExpression="Project#" HeaderText="Proj#">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma" Font-Size="Small"
ForeColor="White" ></headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left"
Wrap="False" />
</asp:BoundColumn>
</Columns>
</asp:datagrid>
ASPX.VB(仅相关代码)
Sub BindData(ByVal strSortField As String)
''clear grid
DataGrid1.DataSource = Nothing 'original
' TimeCodesSelected variable is populated based on Checkbox selections here.
' Code removed for brevity.
Try
' Selected project
Dim ProjectSelected As String = Left(ddwnProjList.SelectedItem.Text, 7) 'Project#
' convert string dates to DateTime data types
PPBegDate = Convert.ToDateTime(txtBegDate.Text)
PPEndDate = Convert.ToDateTime(txtEndDate.Text)
' Check which radio button is selected
If RadioButtonList1.SelectedValue = 0 Then '0 = All projects
ProjectSelected = String.Empty
'inline sql used to provide sort parameters to allow retaining sort order when paging through grid. sqlQuery is a global variable
sqlQuery = "SELECT Person, Project# WHERE dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
''1=by number, or 2=by name
Else
sqlQuery = "SELECT Person, Project#, WHERE Project#='" & ProjectSelected & "' AND dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
'Verify selected project exists, if not hide grid
Dim blnRecordExists As Boolean
Dim oRecord As DataAccessLib.SqlConn = New DataAccessLib.SqlConn()
blnRecordExists = oRecord.VerifyProjectExists(ProjectSelected, PPBegDate, PPEndDate)
If (blnRecordExists = True) Then
DataGrid1.Visible = True
lblErrorMsg.Text = Nothing
Else
DataGrid1.Visible = False
lblErrorMsg.Text = "No records found for this project number during selected dates."
End If
End If
Using conn As SqlConnection = New SqlConnection(conStr)
Using cmd As New SqlCommand(sqlQuery, conn)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using ds As New DataSet()
sda.Fill(ds)
DataGrid1.DataSource = ds.Tables(0)
End Using
End Using
End Using
''Orignal code that works as well and also has the switch from index 2 to index 0 issue
'SqlDataAdapter1.Fill(DsDetailByProj1, "vwPersonSummary")
'Dim objDataView As DataView = DsDetailByProj1.Tables("vwPersonSummary").DefaultView
'DataGrid1.DataSource = objDataView
DataGrid1.DataBind()
End Using 'end using closes conn and goes before 'Catch'
Catch ex As SqlException
sqlErrorMsg = sqlErrorMsg + ": " + ex.Message.ToString
lblErrorMsg.Visible = True
lblErrorMsg.Text = sqlErrorMsg
Catch e As Exception
'reset grid to 1st page after grid refresh (databind)
DataGrid1.CurrentPageIndex = 0
Finally 'the using statement closes objects. Kept here for reference only.
''close and dispose of database connection
'SqlConnection1.Close()
'SqlConnection1.Dispose()
''close sqldatadapter if applicable
'SqlDataAdapter1.Dispose()
End Try
End Sub
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = 0 Then
ddwnProjList.Enabled = False
Else 'if 1 or 2
ddwnProjList.Enabled = True
End If
GetProjectList()'fills ddwnProjList by Project Number or Project Name
BindData("dtmDate")
End Sub
Private Sub ddwnProjList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddwnProjList.SelectedIndexChanged
If (ViewState("sortField") <> Nothing) Then
BindData(ViewState("sortField").ToString())
Else
BindData("dtmDate")
End If
End Sub
enter code here
正如我在评论中提到的,将 DataGrid 的可见性 属性 设置为 true 是解决此问题的第一步。
然后我使用 Scott Mitchell 的答案 here 来验证网格是否为空。通过消除对数据库的另一次往返的需要,这似乎比我这样做的方式更有效。
If (DataGrid1.Items.Count = 0) Then
lblNoRecordsMessage.Text = "Yo Adrian, there are no records for the selected dates."
lblNoRecordsMessage.Visible = True
DataGrid1.Visible = False
Else
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True
End If
我还确保在 DataBind() 方法开始时将 DataGrid 和 Label 设置为可见,因为它有可能在最后一次数据绑定后处于不可见状态。
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True