User being added to 1 role only (asp.net membership) - asp.net

I am trying to allow a user to select a role on createuserwizard from a dropdownlist containing all roles. I dont get errors but user always added to the "Offering Rooms" role no matter what dropdownlist item is selected.
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
roleDropDownList = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")
roleDropDownList.DataSource = Roles.GetAllRoles()
roleDropDownList.DataBind()
End Sub
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
Roles.AddUserToRole(RegisterUser.UserName, roleDropDownList.SelectedValue)
End Sub
Markup:
<asp:DropDownList ID="RoleDropDownList" runat="server">
</asp:DropDownList>
Html:
<option value="Offering Rooms">Offering Rooms</option>
<option value="Seeking Rooms">Seeking Rooms</option>

You need to add check if this is post back and not bind again:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
roleDropDownList = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")
roleDropDownList.DataSource = Roles.GetAllRoles()
roleDropDownList.DataBind()
End If
End Sub

Related

How to get a Id from code behind (aspx.vb) to a Dim variable on .aspx

I have a code behind aspx.vb that have a variable.
And I want to use it on the .aspx web page.
eg.
Public Class proventos_rendimento_distribuicoes_amortizacoes
Inherits System.Web.UI.Page
Dim id_to_use As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblCustomerId.Text = Me.Page.RouteData.Values("nome_artigo").ToString()
id_to_use = Me.Page.RouteData.Values("nome_artigo").ToString()
End If
End Sub
End Class
And I want to use it on .aspx with something like <%= id_to_use %>
e.g.
<b>Customer Id:</b>
<asp:Label ID="lblCustomerId" runat="server" />
<br />
<b><%= id_to_use %></b>
But this doesn't work.
Is it possible to do it?
Just need to declare Dim id_to_use As String = "" to public:
public Dim id_to_use As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblCustomerId.Text = Me.Page.RouteData.Values("nome_artigo").ToString()
id_to_use = Me.Page.RouteData.Values("nome_artigo").ToString()
End If
End Sub

Get Text from listbox in VB.net

in my aspx page I have
<asp:listbox class="myClass" id="lbFamilies" OnSelectedIndexChanged="lbFamilies_SelectedIndexChanged" runat="server" SelectionMode="Multiple"
Height="137px" AutoPostBack="True" EnableViewState="True"></asp:listbox>
And the following is in my codebehind
Protected Sub lbFamilies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
What I am trying to do is to get the text from the selected element, but I can't figure out how to do this
You just have to use the listbox.SelectedItem.Text property:
Protected Sub lbFamilies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim text As String = Nothing
If lbFamilies.SelectedItem IsNot Nothing Then
text = lbFamilies.SelectedItem.Text
End If
End Sub
Thanks, if I have multiselect how would I go through separate elements
Then you have to use a loop:
Protected Sub lbFamilies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim allSelectedTexts As New List(Of String)
For Each item As ListItem In lbFamilies.Items
If item.Selected Then
allSelectedTexts.Add(item.Text)
End If
Next
' following is just a bonus if you want to concatenate them with comma '
Dim result = String.Join(",", allSelectedTexts)
End Sub
or with a LINQ one-liner:
Protected Sub lbFamilies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim result = String.Join(",", From item In lbFamilies.Items.Cast(Of ListItem)()
Where item.Selected
Select item.Text)
End Sub

Why is the wrong value being passed to my server validation function?

On my web form I have a TextBox which I want to perform custom validation on:
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:CustomValidator runat="server" ID="valDate" ControlToValidate="tbDate" onServerValidate="valDate_ServerValidate" ValidateEmptyText="true" ></asp:CustomValidator>
In my code behind, I set the date TextBox's Text in Page_Load, and I have a validation function:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tbDate.Text = DateTime.Today.ToShortDateString
End Sub
Protected Sub valDate_ServerValidate(source As Object, args As ServerValidateEventArgs)
Dim newDate As DateTime
args.IsValid = DateTime.TryParse(args.Value, newDate)
End Sub
Here's the problem: If I enter a value in the textbox and click the submit button (or hit Enter), the validation function doesn't receive the value that I typed. Instead, inside valDate_ServerValidate, args.Value is set to the textbox's initial value that was set in Page_Load. What's going on here?
You are always overwriting the value, so change
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tbDate.Text = DateTime.Today.ToShortDateString
End Sub
to
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then tbDate.Text = DateTime.Today.ToShortDateString
End Sub

How to bind the page title to the datasource

I need to bind title to the datasource so the web page title will show depends on the data in datasource.
When i'm using the Formview and the hiddenfield to get the data from datasource the code cannot compile
How to create a script that the pagetitle can be display based on the data in datasource?
<Script runat = "server">
Protected sub Page_load(Byval sender as Object, Byval e As System.eventargs)
Title = Hiddenfield1.value
End sub
</Script>
<asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value= '<%#eval ("PageTitleConstruct") %>' />
</ItemTemplate>
</asp:FormView>
Try this
Sub FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub
In the Page_Load function. You'll have to get the value earlier than you are though.
Page.Title = value here.
You should be able to listen for the ItemCreated event, and set the title in that callback.
I have tried the script, but when using the
FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub
The page.title value only nothing
But correct if i change the Event to
Private Sub FormView2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView2.Load
Dim hiddenfield1 As HiddenField = CType(FormView2.FindControl("hiddenfield1"), HiddenField)
Page.Title = hiddenfield1.Value
End Sub

Radio button checked changed event in asp.net

I have 2 radio buttons on a simple web page and If I click one of them the other should be false(i.e checked=false) but it's not working Can anyone point me the mistake I'm doing.I knew it's a silly one but I need to know what's going on?
Here are the radio buttons:
<asp:RadioButton ID="Rb1" runat="server" Text=""/>
<asp:RadioButton ID="Rb2" runat="server" Text=""/>
Onchecked changed event:
Protected Sub Rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb1.CheckedChanged
Rb2.Checked = False
End Sub
Protected Sub Rb2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb2.CheckedChanged
Rb1.Checked = False
End Sub
You have to set GroupName property and AutoPostBack=True.

Resources