pass parameters with OnServerClick vb.net - asp.net

I am really new in VB and It is hard to get my head around it especially working with non-MVC projects, so i wish i can find my answer here.
I want to pass arguments with the OnServerClick the code below is the html code:
<a id="Anchor1" OnServerClick="LogIn" runat=server> Click Me </a>
and the method in the server is below:
Protected Sub LogIn(sender As Object, e As EventArgs)
e.value
End Sub
I want to pass parameters(args) to the LogIn method??
I don't want to use any VB or razor buttons or anchor tags i want to use the html tags with the onserverclick attribute. is that possible??
thank you in advance.

You can add custom attribute to aspx <a> like this.
<a id="Anchor1" OnServerClick="LogIn" runat="server" customdata="hello"> Click Me </a>
And get that value in LogIn.
Protected Sub LogIn(sender As Object, e As EventArgs)
Dim myButton = CType(sender, HtmlAnchor)
Dim valuepassedfromUI = myButton.Attributes("customdata")
Dim buttonID = myButton.ClientID
End Sub
, See the screenshot.

Related

Programatically adding MenuItems triggers Page_load in target URL

I am adding menu items programmatically so I can control which ones appear based on the role of the user. My problem is that every menu item I add behaves as if it has been clicked on during the load. Stripped to bare bones, here is the simplified code:
Public Class test2
Inherits System.Web.UI.Page
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
If Not IsPostBack Then
CreateMenu()
End If
End Sub
Private Sub CreateMenu()
MainMenu.Items.Add(New MenuItem("Home", "", "/Peak.aspx"))
End Sub
End Class
The test2.aspx contains:
<form id="form1" runat="server">
<asp:Menu ID="MainMenu" runat="server" >
<Items>
</Items>
</asp:Menu>
</form>
This is all the code in Peak.aspx.vb:
Public Class Peak
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
MsgBox("This should not appear but does!!")
End Sub
End Class
The Peak.aspx Page_Load is being loaded and the message box displayed, when test2 is set as the start up page. My real code has several menu items added and CreateMenu causes ALL the page_Loads to be being triggered with a large overhead!
Is this a bug or something I can switch off, and is there a workaround?
You're using this constructor https://msdn.microsoft.com/en-us/library/b00a9c35.aspx
public MenuItem(
string text,
string value,
string imageUrl
)
If you look at it carefully, your third parameter sets imageurl, thus triggering Page_Load event when it tries to load the url. Maybe you wanted to use this overload with 4 parameters? https://msdn.microsoft.com/en-us/library/70k2h50z.aspx
public MenuItem(
string text,
string value,
string imageUrl,
string navigateUrl
)

Trigger Anchor Tag with LinkButton onclick function vb.net

as you know, Fancybox comes with the anchor tag
I am currently working on a project of a datalist using vb.net
When I click the Linkbutton, I am suppose to get the command argument and set it to session and then open up the fancybox. As fancybox has its own restriction to anchor tags, it is only workable on the first item of the datalist
So I want to do it in such a way that when I click the linkbutton, I will pass the session and also trigger the anchor tag from the code behind. Here is the codebehind for the button_click for the linkbutton
Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
'Dim addJobRequest As Button = sender
' Dim jobID As String = addJobRequest.CommandArgument
' addJobtoRequest(jobID)
Dim button As LinkButton = sender
Dim itegerr As Integer = button.CommandArgument
Session("jobID") = itegerr
Return
'From here, I need a code that can trigger anchor tag
End Sub
Here is the code for the anchor tag
<a id="fancybox-manual-b" href="javascript:;">
P.s. I have tried the linkbutton href function and but it doesn't work.

Updating button text when FormView item is Updated

I'm still somewhat new to ASP, and can't seem to figure out what the issue is.
When I update an item in FormView, I need the text of a button to change.
I'm using:
Protected Sub fvClientInfo_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fvClientInfo.ItemUpdated
btnEditClient.Text = "Edit"
End Sub
The line of code is being called (can tell from debug mode), but the button text isn't changing and I have no idea why.
I should make sure to mention that the button is NOT inside the FormView.
Any ideas?
Try this.
Protected Sub btnchange() Handles FormView1.DataBound
Button1.Text = "Newtxt"
End Sub
and add this to your formview
OnDataBound="btnchange"

Add onclick event to the radio button that added dynamically

I'm creating an online exam page with 30 radiobuttons that are created dynamically at runtime.
How will I get the click event of each radiobutton and tag it in my method that I will check if the next question is need to be jump or escape.
Example:
If I'm in question 10 and answer = "Yes", redirect me to Question 15, else go to the next question
HTML code-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="RadioButtonsPanel" runat="server" />
</form>
</body>
</html>
VB Code-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Add each radio button
AddNewRaduiButton("MyRadio1")
AddNewRaduiButton("MyRadio2")
AddNewRaduiButton("MyRadio3")
AddNewRaduiButton("MyRadio4")
End Sub
Private Sub AddNewRaduiButton(ByVal name As String)
' Create a new radio button
Dim MyRadioButton As New RadioButton
With MyRadioButton
.ID = name
.AutoPostBack = True
.Text = String.Format("Radio Button - '{0}'", name)
End With
' Add the click event to go to the sub "MyRadioButton_CheckedChanged"
AddHandler MyRadioButton.CheckedChanged, AddressOf MyRadioButton_CheckedChanged
Page.FindControl("RadioButtonsPanel").Controls.Add(MyRadioButton)
End Sub
Protected Sub MyRadioButton_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Convert the Sender object into a radio button
Dim ClickedRadioButton As RadioButton = DirectCast(sender, RadioButton)
' Display the radio button name
MsgBox(String.Format("Radio Button {0} has been Updated!", ClickedRadioButton.ID))
End Sub
Use the following statement:
AddHandler radioButton.Click, AddressOf instance.MethodName
Refer to How to: Dynamically Bind Event Handlers at Run Time in ASP.NET Web Pages
Also consider using an anonymous sub (VB2010 only) to write the event handler inline
AddHandler radioButton.Click,
Sub(s As Object, e As EventArgs)
MessageBox.Show("Awesome!")
End Sub
Adapted from here
You can also use closures...

asp.net masterpage - make hyperlink visible at runtime

Is it possible to alter the visible property of hyperlinks on a masterpage at runtime?
thanks
If you expose the hyperlink as a property on the master page, you can get a reference to a pages master and cast that to your specific master page then set visibility of the hyperlinks using that property.
In your master page have something like this:
Public ReadOnly Property RemitViewerLink() As HyperLink
Get
Return hlRemitViewer
End Get
End Property
Then in your child page you can do this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim MyMaster As MasterPage = DirectCast(Page.Master, MasterPage)
MyMaster.RemitViewerLink.CssClass = "selectedMenuItem" 'or set visibility
End Sub
No, if you set visible=False then it won't even display in the HTML output of the page. You would need to use javascript to hide/show the hyperlinks instead.

Resources