从Gridview获取DropDownList项

时间:2014-04-19 11:22:56

标签: asp.net vb.net visual-studio-2010 gridview drop-down-menu

我在ASP.NET中使用Visual Basic。我有一个带有DroDownList列的GridView表,我需要一种从中获取所选项的方法。现在我使用ImageButton来从DropDownList中获取所选项目以弹出消息框。我已经知道如何从boundfield获取整数。但是,使用相同的代码来获取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 个答案:

答案 0 :(得分: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