2014-04-19 185 views
1

我在ASP.NET中使用Visual Basic。我有一個帶有DroDownList列的GridView表格,我需要一種方法從中獲取選定的項目。現在我使用ImageButton來從DropDownList中將選定的項目作爲消息框彈出。我已經知道如何從boundfield獲得一個整數。但是,使用相同的代碼來獲取DropDownList項目將不起作用。 這是我的代碼片段:從Gridview獲取DropDownList項目

ASP代碼:

<asp:BoundField DataField="Case#" HeaderText="Case#" ReadOnly="True" /> 

<asp:TemplateField HeaderText="Surgery Time"> 
     <ItemTemplate> 
     <asp:DropDownList ID="Time_Slot" runat ="server"> 
     <asp:ListItem Selected="True" Value="0">Select...</asp:ListItem> 
     <asp:ListItem Value="1">8:00</asp:ListItem> 
     <asp:ListItem Value="2">9:00</asp:ListItem> 
     <asp:ListItem Value="3">10:00</asp:ListItem> 
     <asp:ListItem Value="4">11:00</asp:ListItem> 
     <asp:ListItem Value="5">12:00</asp:ListItem> 
     <asp:ListItem Value="6">1:00</asp:ListItem> 
     <asp:ListItem Value="7">2:00</asp:ListItem> 
     <asp:ListItem Value="8">3:00</asp:ListItem> 
     <asp:ListItem Value="9">4:00</asp:ListItem> 
     </asp:DropDownList> 
     </ItemTemplate> 
     </asp:TemplateField> 

<asp:ButtonField buttontype="Image" ImageUrl="~/Images/check.jpg" commandname="Accept" HeaderText="Accept" SortExpression="Accept" /> 

Visual Basic代碼:

Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 
    If e.CommandName = "Accept" Then 
     Dim index As Integer = Convert.ToInt32(e.CommandArgument) 
     Dim selectedRow As GridViewRow = GridView1.Rows(index) 
     'This value is retrieved from the databound 
     Dim contactCell As TableCell = selectedRow.Cells(0) 
     Dim contact As String = contactCell.Text 
     'however here it is not retrieved from the dropdownlist 
     Dim contactCell2 As TableCell = selectedRow.Cells(1) 
     Dim contact2 As String = contactCell2.Text 
     MsgBox("case number is" + contact + "time is" + contact2) 
    End If 
End Sub 

回答

1

試試這個。

If e.CommandName = "Accept" Then 
    Dim index = e.CommandArgument 
    Dim timeSlot = CType(gridview1.Rows(index).FindControl("Time_Slot"), DropDownList) 
    Dim selectedTimeSlot = timeSlot.SelectedValue 
End If