如果物件被簽出/簽入,試圖使整個行變為紅色。這是我當前的代碼,我知道這可能很簡單,但我是個菜鳥。
<ItemTemplate>
<tr id="trId" runat="server">
<td style="text-align: left;">
<%#Eval("Name")%>
</td>
<td style="text-align: left;">
<asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
</td>
<td style="text-align: left; display: none;">
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%#Eval("CheckedOut")%>' />
</td>
<td style="text-align: left;">
<asp:Label ID="lblStatus" runat="server" Text='<%#Eval("Status")%>'></asp:Label>
</td>
<td style="text-align: left;">
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'></asp:Label>
</td>
<td style="text-align: left;">
<asp:Button ID="btnChangeStatus" Postback="false" runat="server" Width="150"
CommandArgument='<%#Eval("CatId") & "^" & Eval("CheckedOut")%>' OnClick="ChangeStatus" CssClass="btn btn-default" Font-Bold="True" />
</td>
<td style="text-align: left;">
<asp:Button ID="btnEdit" PostBack="false" runat="server" Text="Edit" CssClass="btn btn-default" Width="150"
CommandArgument='<%#Eval("CatId")%>' OnClick="Edit" />
</td>
</tr>
</ItemTemplate>
Protected Sub CheckPermissions(sender As Object, e As RepeaterItemEventArgs) Handles rep_Data.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim chkStatus As CheckBox = CType(e.Item.FindControl("chkStatus"), CheckBox)
Dim btnChangeStatus As Button = CType(e.Item.FindControl("btnChangeStatus"), Button)
Dim lblStatus As Label = CType(e.Item.FindControl("lblStatus"), Label)
If chkStatus.Checked = False Then
btnChangeStatus.Text = "Check Out"
Else
btnChangeStatus.Text = "Check In"
lblStatus.ForeColor = Drawing.Color.Red
End If
uj5u.com熱心網友回復:
好的,如果您注意到這是 GridView 還是 ListView,那就太好了。
然而,查看標記 - 看起來像一個 ListView - 它們是一個很好用的控制元件 - 我最喜歡的。雖然 GridView 通常標記較少,但當您真的想要布局更復雜的網格時,ListView 開始獲勝(總的來說,整體標記甚至通常更少 - 并且存在更多的格式/樣式選項。
作為一般規則,給定列的行格式(甚至單個控制元件)的最合適選擇是什么?使用行資料系結事件。
您也不分享您擁有的示例代碼在哪里/何時/如何運行?
但是,就目前而言,我們假設您要向該串列視圖提供一些資料。
你也有兩個按鈕 - 但是你為按鈕設定了 postback = false ?(為什么??)。事實上,無論如何按鈕都沒有 PostBack="False" 。所以,洗掉它。
我們可以稍微連接按鈕 - 但第一步是獲取我們的條件格式,“簽入”和“簽出”的“文本”首先適用于現有資料。然后,我們可以處理兩次按鈕點擊。
所以,我們使用行資料系結事件。這有幾個原因,但在該“事件”期間,您不僅可以獲得整個串列視圖行,而且還可以在行系結事件期間獲得不在串列視圖中的列!!!。所以我可能想突出顯示一些值,或者甚至不在 ListView 顯示中的標志 - 但它是資料源的一部分。這意味著您通常不必包含一些隱藏的列值 - 因此請記住這一重要提示。
所以,假設我們有這個串列視圖:
<div style="width:50%">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="Desciption" runat="server" Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Eval("Active") %>' />
</td>
<td>
<asp:label ID="ActiveText" runat="server" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" class="table">
<tr runat="server" style="">
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">City</th>
<th runat="server">Descripiton</th>
<th runat="server">Active</th>
<th runat="server">Active Text</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
沒有太多的標記。然后我們可以用以下代碼填充 LV:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String = "SELECT TOP 10 * from tblHotels WHERE Description is not null ORDER BY HotelName"
Using cmdSQL = New SqlCommand(strSQL, conn)
conn.Open()
Dim rstData = New DataTable()
rstData.Load(cmdSQL.ExecuteReader)
ListView1.DataSource = rstData
ListView1.DataBind()
End Using
End Using
End Sub
我們的輸出現在是這樣的:

Now, lets format the above that say non active rows are say red background.
And we ALSO will set the active text
(if active = true), then You can book!
(if active = false), the text = "don't use"
So, as noted, we use the row data bound event.
But we need to add a "id" to the row markup like this:
<ItemTemplate>
<tr id="onerow" runat="server">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
so, we called it one row.
Now, in our data item bound event, we have this code:
Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListView1.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim ckBox As CheckBox = e.Item.FindControl("ActiveCheckBox")
Dim txtActive As Label = e.Item.FindControl("ActiveText")
Dim onerow As HtmlTableRow = e.Item.FindControl("onerow")
If ckBox.Checked = False Then
txtActive.Text = "Dont use"
' set whole row to red (light red
onerow.BgColor = "LightCoral"
Else
txtActive.Text = "Use Me"
' set whole row to red (light red
onerow.BgColor = "Lightskyblue"
End If
End If
End Sub
And now we have this:

Now the next part is those two button click events. We do and can and want some code for those two buttons.
So this one first:
<td style="text-align: left;">
<asp:Button ID="btnChangeStatus" runat="server" Width="150"
CommandArgument='<%#Eval("CatId") & "^" & Eval("CheckedOut")%>' OnClick="ChangeStatus" CssClass="btn btn-default" Font-Bold="True" />
Ok, I removed your PostBack=False.
And above - Change Status event? that does not look right.
We can't easy double click on the button "inside" of the listview to wire up a click event - but you CAN AND SHOULD do this from markup like this:
In the markup Type in OnClick=
when you hit =, you get this:

choose create new event. Looks like nothing occurred.
the markup will change to:
<asp:Button ID="btnChangeStatus" runat="server" Text="Button"
OnClick="btnChangeStatus_Click"
/>
所以,現在我們可以去后面的代碼,我們有我們的點擊事件。
您可以通過以下方式獲得行點擊并以任何方式處理該行:
Protected Sub btnChangeStatus_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim lvRow As ListViewItem = btn.NamingContainer
' now our code is anything we want, say simular to item data bound event.
Dim ckBox As CheckBox = lvRow.FindControl("ActiveCheckBox")
Dim txtActive As Label = lvRow.FindControl("ActiveText")
Dim onerow As HtmlTableRow = lvRow.FindControl("onerow")
If ckBox.Checked = False Then
txtActive.Text = "Dont use"
' set whole row to red (light red
onerow.BgColor = "LightCoral"
Else
txtActive.Text = "Use Me"
' set whole row to red (light red
onerow.BgColor = "Lightskyblue"
End If
' -------- or get command arugments????
Dim str = btn.CommandArgument
End Sub
或者,您的代碼在上述事件中會這樣說:
' -------- or get command arugments????
Dim btnChangeStatus As Button = sender
Dim lvRow As ListViewItem = btn.NamingContainer
Dim chkStatus As CheckBox = lvRow.FindControl("chkStatus")
Dim lblStatus As Label = lvRow.FindControl("lblStatus")
If chkStatus.Checked = False Then
btnChangeStatus.Text = "Check Out"
Else
btnChangeStatus.Text = "Check In"
lblStatus.ForeColor = Drawing.Color.Red
End If
所以,我們得到按鈕,得到容器(串列視圖行),然后按照上面的方式進行編碼。所以上面是“smaple”,但是你可以有任何你想要的代碼來點擊上面的單行按鈕。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/376587.html
