2017-02-08 12 views
0

こんにちはすべていくつかの属性を持つドロップダウンリストに項目を追加する方法について質問があります。私の最初のアイデアは:ドロップダウンリストを記入するにはどうすればいいですか

<asp:GridView ID="GV_Area" runat="server" AutoGenerateColumns="false" CssClass="table table-striped bolsa-table-gv text-center" 
                  DataKeyNames="Area,Oferta"> 
     <Columns> 
      <asp:BoundField DataField="Nombre" HeaderText="Nombre" ReadOnly="true" /> 
        <asp:TemplateField> 
          <ItemTemplate> 
            <asp:DropDownList ID="ddl_Especializaciones" runat="server" CssClass="selectpicker" multiple DataSource='<%#GetDropDownData(Container)%>'></asp:DropDownList> 
          </ItemTemplate> 
        </asp:TemplateField> 
     </Columns> 
</asp:GridView> 

とコードで:

Protected Function GetDropDownData(e As System.Web.UI.WebControls.GridViewRow) As ListItem() 

      Dim item As ListItem = New ListItem 
      Dim arr As ArrayList = New ArrayList 

      item.Selected = True 
      item.Text = "Hello" 
      item.Value = "Hello2" 
      arr.Add(item) 

      Return arr 
     End Function 

要素が正しくドロップダウンリストではなく、テキストのみの属性に含まれています。選択した値が機能していません。あなたは私が間違っていると思いますか、他の方法で簡単にやりますか?ヘルプ

+0

の背後にあるコードたぶん、DropDownListコントロールの(ddl_Especializaciones)データバインドイベントのハンドラを配線してみてください。 – N0Alias

答えて

1

ため

おかげ百万あなたがGridViewコントロールにDropDownListコントロールを使用する場合は、データとそれらを埋めるために最も簡単な方法は、OnRowDataBoundイベントを使用することです。

<asp:GridView ID="GV_Area" runat="server" OnRowDataBound="GV_Area_RowDataBound"> 

Protected Sub GV_Area_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 

    'check if the row is a datarow 
    If (e.Row.RowType = DataControlRowType.DataRow) Then 

     'cast the row back to a datarowview 
     Dim row As DataRowView = CType(e.Row.DataItem,DataRowView) 

     'use findcontrol to locate the dropdownlist 
     Dim ddl As DropDownList = CType(e.Row.FindControl("ddl_Especializaciones"),DropDownList) 

     'if you need to use a datasource value for filling the dropdownlist 
     Dim Nombre As Integer = Convert.ToInt32(row("Nombre")) 

     'now fill the dropdownlist with values 

     'from a datasource, where 'textField' and `valueField` are colums/properties of the datasource 
     ddl.DataSource = Common.LoadFromDB 
     ddl.DataTextField = "textField" 
     ddl.DataValueField = "valueField" 
     ddl.DataBind 

     'or add them manually 
     ddl.Items.Insert(0, New ListItem("Item A", "1", true)) 
     ddl.Items.Insert(1, New ListItem("item B", "1", true)) 

    End If 

End Sub 
+0

ありがとうございます! – Ary

関連する問題