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
Related
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
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
I have datalist containing images.
I have bounded these images to datalist by taking image names from database.
I just want to get src [Path of image] which user clicks on datalist.
My datalist is as following:
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" runat="server" />
</ItemTemplate>
</asp:DataList>
and i have bounded it as follows:
Protected Sub dlImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlImages.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.EditItem Then
CType(e.Item.FindControl("ImageButton1"), ImageButton).ImageUrl = "~/ScreenMasterImages/" & e.Item.DataItem("PageName") & ".jpg"
End If
End Sub
Please help me to get src of particular image which user clicks on datasource.
I'd go for this:
Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
Dim btn As ImageButton = sender
Dim selImage As String = btn.ImageUrl.ToString
MsgBox(btn.ImageUrl.ToString)
'Shorter:
Dim selImage As String = sender.ImageUrl.ToString
End Sub
Complete Code for testing:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim ditems As New List(Of di)
For i As Integer = 0 To 10
Dim dii As New di
dii.d_Name = "x" & i
ditems.Add(dii)
Next
dlImages.DataSource = ditems
dlImages.DataBind()
End If
End Sub
Protected Sub dlImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlImages.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.EditItem Then
CType(e.Item.FindControl("ImageButton1"), ImageButton).ImageUrl = "~/ScreenMasterImages/" & DirectCast(e.Item.DataItem, di).d_Name & ".jpg"
End If
End Sub
Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
Dim btn As ImageButton = sender
Dim selImage As String = btn.ImageUrl.ToString
MsgBox(btn.ImageUrl.ToString)
End Sub
End Class
Public Class di
Public Property d_Name() As String
Get
Return m_d_Name
End Get
Set(value As String)
m_d_Name = value
End Set
End Property
Private m_d_Name As String
End Class
And the asp:
<asp:DataList ID="dlImages" runat="server" DataKeyField="d_Name" DataMember="d_Name" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" OnClick="ImageButton1_Click" Height="200px" Width="200px" runat="server" />
</ItemTemplate>
</asp:DataList>
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
How do I add a TextBox column to a GridView from code behind?
Use a TemplateField and add a TextBox to the ItemTemplate/EditItemTemplate.
<ItemTemplate>
<asp:TextBox ID="TxtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</ItemTemplate>
You can bind it on the aspx as in the example above or in GridView.RowDataBound:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim dr As DataRow = DirectCast(DirectCast(e.Row.DataItem, DataRowView).Row)
Dim TxtFirstName As TextBox = DirectCast(e.Row.FindControl("TxtFirstName"), TextBox )
TxtFirstName.Text = dr("FirstName").ToString
End Select
End Sub
Edit: Here is a simple example on how to add a TemplateField dynamically:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim txtColumn As New TemplateField
txtColumn.ItemTemplate = New TextColumn
Me.GridView1.Columns.Add(txtColumn)
End If
End Sub
Class TextColumn
Implements ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim txt As New TextBox
txt.ID = "MyTextBox"
container.Controls.Add(txt)
End Sub
End Class
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx