开发者

dropdownlist binding from function in code behind aspx vb

开发者 https://www.devze.com 2022-12-29 16:42 出处:网络
so I have this working in C# , why it is not working in VB public string[] CustomizedRoles() { int length = Roles.GetAllRoles().Length;

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
0

精彩评论

暂无评论...
验证码 换一张
取 消