so I have this working in C# , why it is not working in VB
public string[] CustomizedRoles()
{
int length = Roles.GetAllRoles().Length;
string[] arrRoles=Roles.GetAllRoles();
string[] customizedRoles= new string[length+1];
customizedRoles[0] = "";
for (int开发者_如何学C i = 1; i < length+1; i++)
{
customizedRoles[i] = arrRoles[i-1];
}
return customizedRoles;
}
asp:GridView....
asp:DropDownList ID="ddlOwnerRolesUpdate" runat="server" DataSource="<%# CustomizedRoles() %>" Height="25px" Width="177px" SelectedValue='<%# Bind("Owner") %>'>
where is "Owner" is a data value come from a datasource for the grid
it is working fine in C# .
in Vb with the same aspx and this code behind it is not working.
Public Function CustomizedRoles() As String()
Dim length As Integer = Roles.GetAllRoles().Length
Dim arrRoles As String() = Roles.GetAllRoles()
Dim _customizedRoles As String() = New String(length + 1) {}
_customizedRoles(0) = " "
For index As Integer = 1 To length
_customizedRoles(index) = arrRoles(index - 1)
Next
Return _customizedRoles
End Function
can anyone show me how to bind array of string to a dropdownlist from aspx to code behind in VB
I found that VB.NET can't bind null values to dropdownlist my code had a mistake which generate a null value due to Array Initialization : Dim _customizedRoles As String() = New String(length + 1) {} // it should be length only
after fixing this , it work fine , just a hint , C# do more automatic things than VB, so don't count that VB will do things that C# do it by default.
Public Function CustomizedRoles() As String()
Dim length As Integer = Roles.GetAllRoles().Length
Dim arrRoles As String() = Roles.GetAllRoles()
Dim _customizedRoles As String() = New String(length) {}
_customizedRoles(0) = " "
For index As Integer = 1 To length
_customizedRoles(index) = arrRoles(index - 1)
Next
Return _customizedRoles
End Function
精彩评论