我需要在 gridview 的所有元素上運行一個 rowcommand 以節省職員一些時間,但我被困在一個動作中。這是觸發操作的按鈕:
Protected Sub lbMasivo_Click(sender As Object, e As EventArgs) Handles lbMasivo.Click
Dim args = New CommandEventArgs("GestionarCampoAdicional", "")
For Each item As GridViewRow In gv1.Rows
Dim arg = New GridViewCommandEventArgs(item, args)
gv1_RowCommand(item, arg)
Next
ShowMessage(TipoMensaje.MsgError, "Done")
End Sub
然后在原始命令中有一個拋出例外的函式(在 gv1_rowcommand 上):
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
但是,item應該是 GridViewRow 不是嗎?正如您可能猜到的那樣,我對 vb 或 asp 并不了解,但我正在努力學習。
uj5u.com熱心網友回復:
那么您需要將代碼移出該行命令的單獨例程。換句話說,我們假設您在網格外有一個按鈕,說“檢查”全部或其他。
那么,行命令中的代碼應該做什么?沒有這些資訊,我們都非常迷茫。
所以,假設我有 gv,我想要一個“全選”框?
像這樣說:
<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" cssclass="table">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server"
Checked='<%# Eval("Active") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:DropDownList ID="cboRating" runat="server"
DataValueField="ID"
DataTextField="Rating" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div style="float:right">
<asp:Button ID="cmdCheckAll" runat="server" Text="Make All Active" CssClass="btn" />
</div>
加載 gv 的代碼是:
Dim rstCboChoice As DataTable
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()
rstCboChoice = MyRst("SELECT ID, Rating FROM tblRating ORDER BY ID")
' Now load our grid
GHotels.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
GHotels.DataBind()
End Sub
Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cboRating As DropDownList = e.Row.FindControl("cboRating")
cboRating.DataSource = rstCboChoice
cboRating.DataBind()
cboRating.Items.Insert(0, "Not Selected")
End If
End Sub
現在我們有了這個:

現在,我們可以勾選每一行。或者我們可以選擇網格外的按鈕,并處理每一行,這樣說:
Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click
For Each gRow As GridViewRow In GHotels.Rows
Dim ckBox As CheckBox = gRow.FindControl("chkActive")
ckBox.Checked = True
Next
End Sub
So, whatever code you have or WANT to run and occur for each row, then you put it inside of the above loop. So, you not shared with us what kind of processing or code you need or want to run for each row, but whatever the heck that code is? You put it in the above loop.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447548.html
