gridview中的ddl需要获取行值

时间:2014-08-11 21:53:49

标签: asp.net vb.net gridview

我在gridview中有一个DDL,并且根据用户对DDL的选择,我需要从gridview获取值,例如row id,row column1和row column 2的值。我在gridview中的代码是:

<asp:GridView ID="gvPO" runat="server" DataKeyNames="DC" AutoGenerateColumns="False" RowDataBound="gvPO_RowDataBound" >
   <Columns>
      <asp:TemplateField HeaderText="Id"><ItemTemplate><asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label></ItemTemplate></asp:TemplateField>

      <asp:TemplateField HeaderText="DC"><ItemTemplate><asp:Label ID="lblDC" runat="server" Text='<%# Bind("DC") %>'></asp:Label></ItemTemplate></asp:TemplateField>

      <asp:TemplateField HeaderText="Ped"><ItemTemplate><asp:DropDownList ID="ddlPed" runat="server"SelectedValue='<%# Bind("Ped") %>' AutoPostBack="True" onselectedindexchanged="ddlPed_SelectedIndexChanged"  ><asp:ListItem Value="0">No</asp:ListItem><asp:ListItem Value="1">Yes</asp:ListItem></asp:DropDownList></ItemTemplate></asp:TemplateField>

   </Columns>
</asp:GridView> 

我还有其他专栏,但前两名应该足够了。我在后面的代码中的代码如下:

Protected Sub ddlPed_SelectedIndexChanged(sender As Object, e As EventArgs)
   Dim pedVal As String = TryCast(sender, DropDownList).SelectedItem.Value
   Dim dc As String = gvPO.FindControl("lblDC").ToString() 'returns nothing
   If (pedVal = "1") Then
      pnlPed.Visible = True 'this works as the panel is made visible
   End If
End Sub

我知道我需要获取Id的行ID和/或我可以使用DC的数据键作为行吗?不知道如何获得这些数据。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您的caode无效,因为您没有从gridview的相应行执行FindControl。尝试

Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim row As GridViewRow = DirectCast(ddl.NamingContainer, GridViewRow) 'your selected row
Dim lblDCA As Label = DirectCast(row.FindControl("lblDC"), Label) 'find any controls using the row object

....