iterate through NavigateUrls on Page_PreRender and change style - asp.net

I'm not sure if this is at all possible (there may be a different way to acheive it) but is there a way to iterate though all hyperlinks on Page_PreRender and if the NavigateUrl matches the file name then I can add a class to the link to show this as the active page.
Or even better, iterate through all hyperlink NavigateUrls within a certain DIV.
I can do it individually but that would take too long as there are so many links and be too hard to manage:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
If filePath = "/" & hMembership.NavigateUrl Then
hMembership.CssClass = "active"
End If
End Sub

You can do something like this in the Page_PreRender:
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
For Each Control As Control In Me.Form.Controls
If TypeOf (Control) Is HyperLink Then
With TryCast(Control, HyperLink)
If .NavigateUrl = filePath Then
.CssClass = "active"
End If
End With
End If
Next Control

Related

Open picturefiles with Openfiledialog and list them in CheckedListBox without filepath and show fullpath in Textbox for each item when selected VB

I am using Visual Basic code:
Openfiledialog > I want to keep using this.
CheckedListBox
Textbox
Picturebox
My code below is working but I want the CheckedListBox to show only the filename(safefilename) for each image.jpg item from folder and show the fullpath with filename for each CheckedListBox.item selected to show in my textbox, I don't know and can't find the right code for this.
(now I am using the fullpath aka openfiledialog1.filenames) somehow I need to combine the openfiledialog.safefilenames with openfiledialog.filenames..
my code sofar:
Private Sub Button1_Click(sender As Object, e As
EventArgs) Handles Button1_Click
openFiledialog1.Filter = "Pictures(*.jpg*)|*.jpg"
If openFiledialog1.ShowDialog =
Windows.Forms.DialogResult.OK Then
For Each f As String In openFiledialog1.FileNames
SuspendLayout()
CheckedListBox1.Items.Add(f.ToString)
ResumeLayout()
TextBox1.Text = f
For i As Integer = 0 To
CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i,True)
Next
end if
Private Sub
CheckedListBox1_SelectedIndexChanged(sender As
Object, e As EventArgs) Handles
CheckedListBox1.SelectedIndexChanged
TextBox1.Text = CheckedListBox1.SelectedItem
If Not CheckedListBox1.SelectedIndex < 0 Then
Try
Dim img As Image =
Image.FromFile(CheckedListBox1.
SelectedItem.ToString())
PictureBox1.BackgroundImage = img
Catch ex As Exception
End Try
End If

Unable to cast object of type 'System.Web.UI.WebControls.RadioButtonList' to type 'System.Web.UI.WebControls.TableRow'

I have a 4 tier architecture using asp.net 4.0
1- Web application
2- Business object
3- Business Logic
4- Data Logic
i have used factory method to create dynamic forms. on a radio button list selected index changed event , i want to hide/show a table row which is also created dynamically. table row is available in session. i am using the following code
Private Sub parameter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim rdbtn As RadioButtonList = DirectCast(sender, RadioButtonList)
Dim selectedText As String = rdbtn.SelectedItem.Text
Dim worksRequiredRowId = SessionManager.getWorksRequiredRowId()
Dim worksRequiredRow As TableRow = CType(sender.FindControl(worksRequiredRowId), TableRow)
If selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
worksRequiredRow.Style.Add("display", "table-row")
Else
worksRequiredRow.Style.Add("display", "none")
End If
Catch ex As Exception
End Try
End Sub
And i get the following error.
Unable to cast object of type
'System.Web.UI.WebControls.RadioButtonList' to type
'System.Web.UI.WebControls.TableRow'.
Please help me to find out the solution.
Best ragards.
You can actually hide that particular item from radio button list instead of converting it into table row. Something like this.
If rdbtn.SelectedItem.Text <> selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
rdbtn.Items(itemIndex).Attributes.CssStyle.Add("display", "none")
End If
I hope this helps.
I think your mistake is confusing the generated HTML markup with the raw ASP.NET markup. RadioButtonList does create an HTML for each item, but you can't access that rows at the server-side (to make them visible or hidden).
You should either use the server-side method mentioned in Bridewin D.P.'s answer or transfer the code to client-side and use Javascript to manipulate the rows.
Server-side would look something like this:
Private Sub parameter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim rdbtn As RadioButtonList = DirectCast(sender, RadioButtonList)
Dim selectedText As String = rdbtn.SelectedItem.Text
Dim worksRequiredRowId = SessionManager.getWorksRequiredRowId()
Dim style as String
If selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
style= "table-row"
Else
style= "none"
End If
rdbtn.Items(worksRequiredRowId).Attributes.CssStyle.Add("display", style)
Catch ex As Exception
End Try
End Sub

adding events to programatically added controls in web user control

I am trying to put a - what seems - very simple web user control
basically i want it to render as a dropdownlist/checkboxlist or radiolist based on a property
but also want to be able to work out what is selected
i was trying the following - but cant seem to work out how to attach to the selectedindexchanged of the listcontrol so that i can set the selectd value(s)
its not helping that my VB is not up to much but am forced to use it in this instance
its not even giving me the intellisense for the event..
Public Options As List(Of Options)
Public ControlRenderType As ControlRenderType
Public IncludeFreeOption As Boolean
Public SelectedOptions As List(Of Options)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim c As ListControl
Select Case (ControlRenderType)
Case STGLib.ControlRenderType.CheckBoxList
c = New CheckBoxList()
Case STGLib.ControlRenderType.DropdownList
c = New DropDownList()
Case STGLib.ControlRenderType.RadioButtonList
c = New RadioButtonList()
Case Else
Throw New Exception("No Render Type Specified")
End Select
For Each opt In Options
Dim li = New ListItem(opt.Description, opt.ID)
c.Items.Add(li)
Next
c.SelectedIndexChanged += ..?? or something
Page.Controls.Add(c)
End Sub
can anyone explain please - it is of course quite possible that I am going about this in completely the wrong way..
thanks
First create a Sub or a Function to handle the IndexChange of the object that you have created dynamically and make sure that the signature of the Sub is something like this
Sub myOwnSub(ByVal sender As Object, ByVal e As EventArgs)
...
... Handle your event here
...
End Sub
Then after you create your object add the following code
Dim obj as ListBox
AddHandler obj.SelectedIndexChanged, AddressOf myOwnSub

Deleting Dynamically Populated images from a Directory (NOT GridView) ASP.NET (VB)

The code below displays a thumbnail of each image in a specific server directory and when I click on the image it pops up a nice full sized picture. It works perfectly.
I would however, like to be able to delete an image. I first thought I could have a button at the bottom of the page with a checkbox next to each image, giving it a uniqueID as per the filename but as they are dynamically created I couldn’t figure how to handle the Click Event on the button for a randomly named Checkbox ID. Then I tried adding a button next to each item and then tried an OnClick & OnServerClick to call a Sub but this didn’t work either.
Any/All suggestions welcomed :)
Private Sub ImageList()
If Directory.Exists(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text)) Then
Dim MySB As New StringBuilder
Dim dirInfo As New DirectoryInfo(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text))
MySB.Append("<ul class=""clearfix"">")
MySB.AppendLine()
For Each File In dirInfo.GetFiles()
MySB.Append("<li><a rel=""jobpic"" href=""JobImages\" & DBC_JOB_JobID.Text & "\" & File.Name & """><img src=""JobImages\" & DBC_JOB_JobID.Text & "\Thumbs\" & File.Name & """ width=""150"" height=""100"" /> <span class=""size"">" & File.Name & " </span></a></li>")
MySB.AppendLine()
Next
MySB.Append("</ul>")
MySB.AppendLine()
lblMyPictures.Text = MySB.ToString
End If
End Sub
OK what Kendrick is talking about (basically) is using server side controls to keep track of which file to delete. What you are doing right now is dumping markup into a Label control, which on postback won't fire an event on the server side. However you can accomplish this easily with server side controls.
The basic idea is you use a container control such as a Panel and add each child control to it. Then you hook events to each row with data identifying that row (such as filename).
Markup:
<asp:Panel ID="pnlList" runat="server">
</asp:Panel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Directory.Exists(Server.MapPath("Files")) Then
Dim objDirInfo As New DirectoryInfo(Server.MapPath("Files"))
For Each objFile As FileInfo In objDirInfo.GetFiles()
Dim objLabel As New Label
objLabel.Text = objFile.Name
Dim objLinkButton As New LinkButton
objLinkButton.Text = "Delete"
objLinkButton.CommandName = "Delete"
objLinkButton.CommandArgument = objFile.Name
AddHandler objLinkButton.Command, AddressOf DeleteFile
Dim objLiteral As New LiteralControl
objLiteral.Text = "<br/>"
pnlList.Controls.Add(objLabel)
pnlList.Controls.Add(objLinkButton)
pnlList.Controls.Add(objLiteral)
Next
End If
End Sub
Public Sub DeleteFile(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "Delete" Then
Dim strFileName As String = Server.MapPath("Files\" & e.CommandArgument)
If File.Exists(strFileName) Then
Dim objFile As New FileInfo(strFileName)
objFile.Delete()
End If
End If
End Sub
This would be an excellent example of where using a data aware would make your life a lot easier.
That said, if you didn't want to use a server-side control, you could assign an ID to each checkbox (i.e. DeleteImage_1) and then store the ID and associated image name in the viewstate on the page. Go through the checked checkboxes and refer back to the viewstate for the name that goes with each ID when they click the delete button.

How can i scroll to an anchor?

I have bound a datapager control to a listview.
I would like to scroll to the first item of the listview control on the DataPager click. I think this should be done with javascript. It seems that the datapager does not allow that.
What options do I have? How can I scroll to a specific anchor when clicking on the DataPager?
you can use the basic html named anchor to scroll to a specific anchor.
You can use the javascript function scrollIntoView for that on client side or on "server side":
http://www.codeproject.com/KB/aspnet/ViewControl.aspx
Thanks Tim!
And for the lazy guys out there (just like me ;), here is the VB.NET equivalent.
It contains typo corrections and the new RegisterClientScriptBlock Method
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FocusControlOnPageLoad("Label1", Me.Page)
End Sub
Public Sub FocusControlOnPageLoad(ByVal ClientID As String, ByVal page As System.Web.UI.Page)
Dim csName As String = "ScrollViewScript"
Dim csType As Type = Me.GetType
Dim cs As ClientScriptManager = page.ClientScript
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script>function ScrollView(){")
csText.Append("var el = document.getElementById('" & ClientID & "');")
csText.Append("if (el != null){")
csText.Append("el.scrollIntoView();")
csText.Append("el.focus();}}")
csText.Append("window.onload = ScrollView;")
csText.Append("</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub

Resources