I am trying to get all roles a user is assigned to in NOPCOMMERCE. I know how to find out if a user is in a specific role, but I can't get all roles in a list or in a listbox.
If I use the following code:
Imports Nop.Core.Infrastructure
Imports Nop.Services.Helpers
Imports Nop.Core.Domain.Customers
Imports Nop.Services.Customers
Imports Nop.Core
ListBox1.DataSource = EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles()
ListBox1.DataBind()
'this is the listbox on aspx page
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
I get this in the listbox:
System.Data.Entity.DynamicProxies.CustomerRole_6E0DFC682B2D3247B3BEEED3063345A95D5742CE33B2761C4CB20A8C0A0AD639
System.Data.Entity.DynamicProxies.CustomerRole_6E0DFC682B2D3247B3BEEED3063345A95D5742CE33B2761C4CB20A8C0A0AD639
System.Data.Entity.DynamicProxies.CustomerRole_6E0DFC682B2D3247B3BEEED3063345A95D5742CE33B2761C4CB20A8C0A0AD639
System.Data.Entity.DynamicProxies.CustomerRole_6E0DFC682B2D3247B3BEEED3063345A95D5742CE33B2761C4CB20A8C0A0AD639
Which shows 4 roles, but they don't look like roles to me. I need the actual role name. Does anyone have an idea how I can get all roles a user has? Thanks.
You need to specify in the listbox the DataTextField and DataValueField to tell listbox what fields of the CustomerRole is going to show.
ListBox1.DataSource = EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles()
//add this
ListBox1.DataTextField = "SystemName"; //you can use "Name" instead of "SystemName" or whatever property name of CustomerRole class
ListBox1.DataValueField = "Id";
ListBox1.DataBind()
Related
I have a web page (aspx)- Purchasing page, with a ascx toolbar - Export Toolbar, that is used to export the data (either .xls or .csv).
I need to grab the Name of the Supplier from the Purchasing page and insert that value into the name of the export file on the ascx toolbar.
On the Purchasing page there is a ddl where the user can select the supplier and a grid that will display all the data. Above the grid there is the tool bar with an export button. I need to be able to grab the text of the dropdown list and utilize that on the ExportToolbar.ascx.vb page so I can take that text and insert it into the name.
I was trying to use a public property get and set method but it was not working. How would I go about grabbing that selected text from the Supplier ddl?
Conventional thinking goes like this: an ascx can be hosted on any aspx page. So usually it is bad form for an ascx to access properties of its host page. It is much more proper for the ascx to have a public property and the aspx will push the value into the ascx (as needed).
However, if you really want to go this route, the .Page property (of the ascx) referrs to the host page. If you cast it to the (stronger) type(name) of the host, you can get to the hosts properties. Like this:
'if your host page is called HostPage (and the class name is the same)
Dim host as HostPage = CType(me.Page, HostPage)
'now refer to the controls on the host (aspx) page
dim example as string
example = host.txtExample.Text
Keep in mind, this will cause errors if your ascx is hosted on several pages.
You can use an event form this purpose. Define the event on the UserControl like this:
Public BeforeExportEventArgs
Inherits EventArgs
Public Property FileName As String
End Class
Public Class ToolbarControl
Inherits UserControl
Public Event BeforeExport As EventHandler(Of BeforeExportEventArgs)
Public Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
' Retrieve File Name
Dim beforeExpEventArgs As New BeforeExportEventArgs()
RaiseEvent BeforeExport(Me, beforeExpEventArgs)
' Set default filename if not provided by an event handler
If String.IsNullOrEmpty(beforeExpEventArgs.FileName) Then
beforeExpEventArgs.FileName = "DefaultFileName.csv"
End If
' Export data
End Class
Add an event handler to the form that hosts the UserControl:
Public Class WebForm1
Inherits Page
' ...
Public Sub expToolbar_BeforeExport(sender As Object, e As BeforeExportEventArgs) Handles expToolbar.BeforeExport
e.FileName = ddlSupplier.Text + ".csv"
End Sub
' ...
End Class
This way, you avoid tight coupling between the UserControl and the Page. The pages that host the UserControl can set a specific filename, but don't have to.
What I ended up doing was this-
On the ascx page I created a public property-
Public Property SupplierSelection As String
Get
Return Convert.ToString(ViewState.Item("SupplierSelection"))
End Get
Set(ByVal value As String)
ViewState.Add("SupplierSelection", value)
End Set
End Property
And then on the aspx page I added this on the load grid event-
SupergridToolbar1.SupplierSelection = ddlStrategy.SelectedItem.Text.ToString()
I was then able to use the Supplier Selection on the ascx page. Thanks for the help!
My overall goal is to add fake/unbound items to a listview control (for final HTML Table output reasons). This is a code behind solution. Users will not be adding items as it will be outputted in a rigid table.
I have looked at several examples and while this is easy for a dropdown it is not for listview.
The code below works without error, but my item is not shown on runtime. I think the class is not setting the item fieldname correctly, but I can't figure out the right syntax to fix it.
ColumnNameAList.DataSource = PeriodDataView
ColumnNameAList.DataBind()
Dim test As New Example1("ColumnNameA")
Dim newItem As New ListViewDataItem(ColumnNameAList.Items.Count, ColumnNameAList.Items.Count)
newItem.DataItem = test
ColumnNameAList.Items.Insert(ColumnNameAList.Items.Count, newItem)
ColumnNameAList.Items.Add(newItem)
Here is the Example1 class that is supposed to set the DataValueField:
Public Class Example1
Public Sub New(ColumnNameA__1 As String)
ColumnNameA = ColumnNameA__1
End Sub
Private m_ColumnNameA As String
Public Property ColumnNameA() As String
Get
Return m_ColumnNameA
End Get
Set(value As String)
m_ColumnNameA = value
End Set
End Property
End Class
This outputs my original datasource list, but not the added item.
<ItemTemplate>
<td>
<%# Eval("ColumnNameA")%>
</td>
</ItemTemplate>
In the end I could only reliably solve this with a codebehind solution.
I made a copy of the original datasource, modified my copy and then databound to it.
Dim MyOriginalTableSource As Data.DataView = DataManager.example()
Dim ModifiedTable As DataTable = MyOriginalTableSource.ToTable
'do stuff here
Mylistbox.DataSource = ModifiedTable
Mylistbox.DataBind()
Won't work for everyone, but in this case it works fine for me.
There could be a couple of issues with the way you are approaching this, including that the ListView is already databound and that you are both adding and inserting the newItem.
When we have a scenario like this, we take one of two approaches:
1) Add the new item to the data source before the source is data bound.
2) Remove databinding and manually create each of the list view items, then add your new item at the beginning or end of the loop.
Another way to do it would be to inject it into the sql.
select col1, col2, col3 from table1 union select '1','2','3'
this would ensure that the item is always added, and asp.net doesn't need to know or care.
You can add this into the sql query or add it from the behind code before binding query. if you are not binding with sql, you can also do this to any list item with LINQ
I'm working with a website written in aspx.net over vb.
I have a placeHolder, and I create a table of names inside this PlaceHolder, each name has an HtmlInputCheckBox next to it.
Im doing this in the aspx.vb file, when the page is uploading.
Then, when the user wants to send mail, he presses a button and than I need to access the checkboxes, and I'm having problems with this, the Sub doesn't know the checkBox object.
I would love for some help,
Thank you!
I understand that you're creating those checkboxes dynamically?
In such case, store them as global member of the class, most simple way is to have List of them:
List<HtmlInputCheckBox> arrCheckboxes = new List<HtmlInputCheckBox>();
...
...
HtmlInputCheckBox myCheckbox = new HtmlInputCheckBox();
arrCheckboxes.Add(myCheckbox);
...
This is C# but should be easy to translate to VB - anyhow having this, you can access the List and it should work.
Worst case as "last resort" you can simply iterate the whole Request.Form collection and look for Keys with name matching to the checkbox name.
Put this in the procedure...
Dim chkValue1 As New CheckBox
Dim chkValue2 As New CheckBox
'Find the Checkbox Controls in the PlaceHolder and cast them to the checkboxes we just made.
chkValue1 = CType(YourPlaceHolder.FindControl("Checkbox1ControlId"), CheckBox)
chkValue2 = CType(YourPlaceHolder.FindControl("Checkbox2ControlId"), CheckBox)
'Now you can do this...
Dim bolIsValue1Checked As Boolean = chkValue1.Checked
I have a very simple mockup I'm making for a client that uses the Northwind database. I have an EDMX file with three entities: Products, Categories, and Suppliers.
I'm trying to create a page that has a GridView that shows the products, including the category name and supplier name. With LINQ to SQL I can have the LinqDataSource control return the Products entity and then can have a TemplateField in the GridView like so:
<ItemTemplate>
<%# Eval("Category.CategoryName") %>
</ItemTemplate>
However, it seems the EntityDataSource is not playing so nicely. It's as if it won't lazy load the category data. I have a very simple EDS:
<asp:EntityDataSource ID="dsProducts" runat="server"
ConnectionString="name=NorthwindEntities"
DefaultContainerName="NorthwindEntities" EnableFlattening="False"
EntitySetName="Products">
</asp:EntityDataSource>
But the GridView does not show the category name. If I create a RowDataBound event handler for the GridView and get the Product entity being bound to the row, I see that the product's Category property returns Nothing. For example, if I do:
Protected Sub gvProducts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProducts.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim p As NorthwindModel.Product = e.Row.DataItem
Dim catName = p.Category.CategoryName
End If
End Sub
I get a NullReferenceException when trying to do p.Category.CategoryName.
However, I know lazy loading works for the EDMX b/c if I write code in the Page_Load event handler like:
Dim context As New NorthwindModel.NorthwindEntities
Dim p = context.Products.Take(1).Single()
I can get the Category name via p.Category.CategoryName without error.
Is there some voodoo I need to do to get the EntityDataSource to include support for retrieving related entities?
Thanks
SOLUTION:
I specified the Include property of the EntityDataSource, noting the entity objects to include. Specifically, I updated my EntityDataSource control's declarative markup to:
<asp:EntityDataSource ID="dsProducts" runat="server"
ConnectionString="name=NorthwindEntities"
DefaultContainerName="NorthwindEntities" EnableFlattening="False"
EntitySetName="Products" Include="Category,Supplier">
</asp:EntityDataSource>
You need to use the Include property of the entity data source to get related entities. Then, change the markup to query into these entities.
Take a look a a couple of pages from the book "Programming Entity Framework": http://books.google.com/books?id=wp8V0vBebnoC&pg=PA284&lpg=PA284&dq=entitydatasource+include&source=bl&ots=cKtfB1J8vC&sig=C--WGKuU-9CNOQgDxdN0MpSMLt4&hl=en&ei=OidPTMnKB5P-ngeM1rinBw&sa=X&oi=book_result&ct=result&resnum=7&ved=0CDAQ6AEwBg#v=onepage&q=entitydatasource%20include&f=false
i want to create a modalpopup dynamically but i come across a problem.I pasted my sub here and i dont know what to do for that problem.When i want to show modalpopup,it says
"Control 'mdldelete2' of type 'ModalPopupExtender' must be placed inside a form tag with runat=server." How can i solve this?
Public Sub Raise_Alarm(ByRef p_Page As Page,
ByRef p_AssignedButton As System.Web.UI.WebControls.Button,
ByVal p_Message As String)
Dim mdldelete2 As Global.AjaxControlToolkit.ModalPopupExtender =
p_Page.Page.FindControl("mdldelete2")
If mdldelete2 Is Nothing Then
mdldelete2 = New Global.AjaxControlToolkit.ModalPopupExtender
End If
With mdldelete2
.TargetControlID = p_AssignedButton.ID
.PopupControlID = "pnlDelete"
.ID = "mdldelete2"
.BackgroundCssClass = "modalBackground"
.OkControlID = "btnDeleteOk"
.CancelControlID = "btnDeleteCancel"
End With
p_Page.Controls.Add(mdldelete2)
Dim mylabel As Label
mylabel = p_Page.FindControl("lblStatus")
mylabel.Text = p_Message
mdldelete2.Show()
End Sub
Really, you should be adding the mdldelete2 control to the Controls collection of the Form control, rather than the Page directly - that might help.
I often find it's easier to add a PlaceHolder control to the page for this sort of thing - it doesn't add anything directly to the page, but gives you a named container to find and add controls to.
Also, just a point - if you did find an instance of the control with the Page.FindControl method, then you don't need to add it to a form collection again, as it's already in there.
Looks like you need to add a ScriptManager control to the aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>