I am creating a sub administration web page. I get the Role_ID that the sub_Admin is assigned to, then i get all the User_ID's associated with that Role_ID, from there I get all the user names associated with the User_ID's associated with the Role_ID.
I populate the UserList dropdownlist box with the UserNames. For some reason this Dropdownlist box only gets populated half the time, the rest of the time it is empty upon page_load.
How do I get it to where it populates each an every time?
Any assistance would be greatly welcomed.
Place your code inside : if(! ispostback) {...} block when populating user list
It's not populating the dropdownlist half the time when the Page originally loads. I don't understand how by placing it in the IF notispostback block will make a difference.
We can analyze what the problem if you show the code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
' 'For when the page originally loads.
If (User.IsInRole("BayC")) Then Role_ID = "BayC"
If (User.IsInRole("Char")) Then Role_ID = "Char"
If (User.IsInRole("Chel")) Then Role_ID = "Chel"
BindUserstoMemberRole(Role_ID)
Else
'For every time after the original page load.
If (User.IsInRole("BayC")) Then Role_ID = "BayC"
If (User.IsInRole("Char")) Then Role_ID = "Char"
If (User.IsInRole("Chel")) Then Role_ID = "Chel"
BindUserstoMemberRole(Role_ID)
End If
End Sub
Here is the code that populates the dropdownlist that is only populating half of the time.
Private Sub BindUserstoMemberRole(ByRef Role_ID As String)
Dim ID, User_ID As Guid
Dim hrow = UsersTable.NewRow
Dim UserName As String
SQLSelectStr = "Select * From aspnet_Roles where RoleName = '" + Role_ID + "'"
myAdapter = New SqlDataAdapter(SQLSelectStr, GADSDB_conn)
GADSDB_conn.Close()
GADSDB_conn.Open()
'Popultate Gridview with rows returned from SQL Select Statement
myAdapter.Fill(tl)
GADSDB_conn.Close()
For Each row In tl.Rows
ID = row.item("RoleID")
Next
GetRoleIDTable.Reset()
SQLSelectStr = "Select * From aspnet_usersinRoles where RoleID = '" + ID.ToString + "'"
myAdapter = New SqlDataAdapter(SQLSelectStr, GADSDB_conn)
GADSDB_conn.Open()
'Popultate Gridview with rows returned from SQL Select Statement
myAdapter.Fill(GetRoleIDTable)
UsersTable.Reset()
For Each u In GetRoleIDTable.Rows
User_ID = u.item("UserID")
If UsersTable.Columns.Contains("UserName") = False Then
UsersTable.Columns.Add("UserName", GetType(String))
End If
SQLSelectStr = "Select * From aspnet_users where UserID = '" + User_ID.ToString + "'"
myAdapter = New SqlDataAdapter(SQLSelectStr, GADSDB_conn)
GADSDB_conn.Close()
GADSDB_conn.Open()
GetUserNameTable.Clear()
'Popultate Gridview with rows returned from SQL Select Statement
myAdapter.Fill(GetUserNameTable)
For Each n In GetUserNameTable.Rows
UserName = n.item("UserName")
hrow("UserName") = UserName
UsersTable.ImportRow(n)
Next
Next
UserList.DataSource = UsersTable
UserList.DataTextField = "UserName"
UserList.DataValueField = "UserName"
UserList.DataBind()
'UserList.Items.Insert(0, New ListItem("---Select a User---", String.Empty))
End Sub
I have one question, if your role ID is not changing with post back which seems to the case from your code, why do have to bind data to dropdown twice. I would bind it only if not Postback unless you have a valid reason to do it.
Copy/Paste error on my part from Visual Studio to the forum.
So does anyone know why it is not loading the first time correctly?
Anyone have any more ideas?
hi,
when load this web page you use User.IsInRole() method and then get the Role_ID .but your ispostback and not postback
has no difference .the postback method to be meaningless .
if your web need to update this page in run time you can use javascript method: window.setInterval() refer the below link:
http://www.w3schools.com/js/js_timing.asp
Hope this helps
If IsPostBack Then
' 'For when the page originally loads.
If (User.IsInRole("BayC")) Then Role_ID = "BayC"
If (User.IsInRole("Char")) Then Role_ID = "Char"
If (User.IsInRole("Chel")) Then Role_ID = "Chel"
BindUserstoMemberRole(Role_ID)
End If
I figured it out.
I was doing two many sql queryies, half the time it would time out. the code I ended up going with is this, works every time:
Private Sub BindUserstoMemberRole(ByRef Role_ID As String)
' Get the selected role
Dim usersBelongingToRole() As String
Dim selectedRoleName As String = RoleList.SelectedValue
usersBelongingToRole = Roles.GetUsersInRole(Role_ID)
' Bind the list of users to the GridView
UserList.DataSource = usersBelongingToRole
UserList.DataBind()
GetRoleIDTable.Reset()
SQLSelectStr = "Select * From aspnet_usersinRoles where RoleID = '" + Guid + "'"
myAdapter = New SqlDataAdapter(SQLSelectStr, GADSDB_conn)
GADSDB_conn.Close()
GADSDB_conn.Open()
'Popultate Gridview with rows returned from SQL Select Statement
myAdapter.Fill(GetRoleIDTable)
UserInList(GetRoleIDTable)
End If
End Sub
Private Sub UserInList(ByRef GetRoleIDTable As System.Data.DataTable)
Dim User_ID As Guid
Dim UserName As String
Dim hrow = UsersTable.NewRow()
UsersTable.Reset()
For Each u In GetRoleIDTable.Rows
User_ID = u.item("UserID")
If UsersTable.Columns.Contains("UserName") = False Then
UsersTable.Columns.Add("UserName", GetType(String))
End If
SQLSelectStr = "Select * From aspnet_users where UserID = '" + User_ID.ToString + "'"
myAdapter = New SqlDataAdapter(SQLSelectStr, GADSDB_conn)
GADSDB_conn.Close()
GADSDB_conn.Open()
GetUserNameTable.Clear()
'Popultate Gridview with rows returned from SQL Select Statement
myAdapter.Fill(GetUserNameTable)
For Each n In GetUserNameTable.Rows
UserName = n.item("UserName")
hrow("UserName") = UserName
UsersTable.ImportRow(n)
Next
Next
End Sub
沒有留言:
張貼留言