請原諒我,因為這是我的第一篇文章,我遠不是精通 IT,但我一直在為我的新作業做一個專案......
我正在使用 Visual Studio 2017 和 asp.net Web 表單模板。
我有幾個面板,我已將可見性設定為 false,并將每個面板設定為僅在從復選框串列中選擇某個選項時才可見。
我已經成功地對可見性進行了編碼,但是當從復選框串列中選擇了多個專案時,我希望允許用戶查看多個面板......不幸的是,看起來一次只有一個面板可見。我假設它們是重疊的。
我嘗試將每個面板放在一個表格中以充當占位符,但沒有成功。
任何建議將不勝感激??
aspx.vb
Protected Sub ListBox1_Changed(ByVal sender As Object, ByVal e As EventArgs) 處理 ListBox1.SelectedIndexChanged
If ListBox1.SelectedValue = "AA" Then
Panel1.Visible = True
Else
Panel1.Visible = False
如果 ListBox1.SelectedValue = "BB" 或 ListBoxLivewell.SelectedValue = "CC" 或 ListBoxLivewell.SelectedValue = "DD" 那么
Panel2.Visible = True
Else
Panel2.Visible = False
End If
uj5u.com熱心網友回復:
我建議你創建空白測驗頁,然后試試這個:
標記:
<asp:CheckBox ID="CheckBox1" runat="server" Text="Panel1" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="Panel2" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="Panel3" AutoPostBack="true" />
<asp:Panel ID="Panel1" runat="server">
<h2>Panel 1</h2>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server">
<h2>Panel 2</h2>
</asp:Panel>
<asp:Panel ID="Panel3" runat="server">
<h2>Panel 3</h2>
</asp:Panel>
后面的代碼:
Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
' we could use this one line
' Panel1.Visible = CheckBox1.Checked
' or
If CheckBox1.Checked Then
Panel1.Visible = True
Else
Panel1.Visible = False
End If
End Sub
Protected Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
Panel2.Visible = CheckBox2.Checked
End Sub
Protected Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
Panel3.Visible = CheckBox3.Checked
End Sub
我們得到這個:

請注意,由于頁面剛剛加載,因此我的復選框代碼都沒有運行。事實上,如果我選中一個框,我會得到:

所以,實際上要隱藏面板 1,我現在可以取消選中第一個框,然后得到:

請注意面板是如何“不同步”的,因為我并沒有真正檢查或在啟動時對面板做任何事情,因此它們不能反映正確的狀態。
事實上,直到我檢查并取消選中所有 3 個,然后在那時,它們都可以正常作業。
所以,這樣做可能會更好。
在第一頁加載(并且只有第一次加載)時,我會這樣做:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ShowPans()
End If
End Sub
Sub ShowPans()
Panel1.Visible = CheckBox1.Checked
Panel2.Visible = CheckBox2.Checked
Panel3.Visible = CheckBox3.Checked
End Sub
So, now on first page load - I hide/show the panels based on check box code, and now everything starts out "synced" up correctly
Do try a test blank page with above, but hiding and showing the panels should work.
Do note, that in MANY cases it is better to hide/show the panels and NOT use visibile. The reason for this is that any control marked as visibile = false IS NOT SENT to the browser. If you have some client side JavaScript running, or doing things, they will NOT be able to see, nor reference those panels.
So, in many cases, it is FAR better to hide, and NOT use Visible=false. As noted, this is VERY much a requement if you using client side JavaScript to manipulate such controls.
As a result, our code would now become this:
Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Panel1.Style("display") = "normal"
Else
Panel1.Style("display") = "none"
End If
End Sub
Protected Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Panel2.Style("display") = "normal"
Else
Panel2.Style("display") = "none"
End If
End Sub
Protected Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
' same as above 2 - but short hand code
Panel3.Style("display") = IIf(CheckBox3.Checked, "normal", "none")
End Sub
Now, you ONLY need to do the above if you using say JavaScript client side - I just wanted to make clear that using Visible can often be a issue, since as noted visible = false means the control is NOT sent nor rendered browser side - I spent a hard day of bugs learning this when I then started to write a lot of JavaScript.
Since, so far, this looks to be 100% server side code, so you can use .Visible.
but, do cook up a quick tiny test web page - play with above - make sure it works, and then try again on your code.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449364.html
