How do I pass a value over to a module/function. Whenever I search here or Google I am getting code for C# rather than VB.net
I want to be able to click on a button and pass a value to a module and action it. Each button has a value 1,2,3,4 etc... and they pass back to make a panel visible=true/false.
i.e. I want to say the below but when I click btnShow1 I want to pass this as a value to a function and hide/show the panel I am talking about.
Current code
Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
If panel1.Visible = True Then
panel1.Visible = False
Else
panel1.Visible = True
End If
End Sub
Guess code
'Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
vpanel = btnShowQ1.value
Call fncPanel(vpanel as string) as string
End Sub
Then somewhere - I guess in App_code I create function.vb
Imports Microsoft.VisualBasic
Public Class ciFunctions
Public Function fncPanel(vPanel as string)as string
If (vPanel).visible = False then
(vPanel).visible = true
Else
(vPanel).visible = false
End IF
End Function
End Class
Set the button's CommandArgument property to the panel number to which it shows or hides:
<asp:Button ID="Button1" runat="server" Text="Show/Hide" CommandArgument="1" />
<asp:Button ID="Button2" runat="server" Text="Show/Hide" CommandArgument="2" />
On the button click event:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, ...
' get the button which triggers this event
Dim thisButton As Button = DirectCast(sender, Button)
' construct the panel name to toggle visibility from the button's CommandArgument property
Dim panelName as String = "panel" & thisButton.CommandArgument
' call the function, passing the panel name and the reference to current page
TogglePanel(Me, panelName)
End Sub
You can add all the buttons click event handler to this sub by adding them to the Handles... list as above. Therefore you don't have to create event handler for each buttons on the page. Just one to handle them all.
Create the function to toggle visibility:
Sub TogglePanel(pg As Page, panelName As String)
' get the panel control on the page
Dim panel As Panel = DirectCast(pg.FindControl(panelName), Panel)
' toggle its visibility
panel.Visible = Not panel.Visible
End Sub
The pg parameter is required if you put this function in a class other than the page it runs on or in a module. If the function sit in the same page class, then pg is not required.
You should be able to just add a parameter of type Panel to the sub, and in the btnShow event, pass the item.
Public Sub ChangePanelVis(panel as Panel)
If panel.Visible then
panel.Visible = True
Else
panel.Visible = True
End If
End Sub
Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
ChangePanelVis(panel1)
End Sub
Related
The control that I want to use is already registered in my page.
<%# Register Src="~/MEDCONTROLS/statform.ascx" TagPrefix="uc1" TagName="statform" %>
and i am displaying it in my page using this code. (using different id's && different TempHosp#)
<uc1:statform runat="server" TempHospNum="ER101" ID="statform1" />
<uc1:statform runat="server" TempHospNum="ER102" ID="statform2" />
<uc1:statform runat="server" TempHospNum="ER103" ID="statform3" />
Now what I need to do is, on the click button event, it will create a new user control, actually same user control but only different id and different property eg.
EDIT
Protected Sub btnAddUserControl_Click(sender As Object, e As EventArgs) Handles btnAddUserControl.Click
'Generate user control with a TempHospnum = "ER104" and id="statform4"
End Sub
Now it is adding BUT in page.init only, NOT on Button_click
'NOT WORKING
Protected Sub btnNewBed_Click(sender As Object, e As EventArgs) Handles btnNewBed.Click
Dim myControl As statform = Page.LoadControl("~/MEDCONTROLS/statform.ascx")
myControl.ID = "statform13"
myControl.TempHospNum = "ER113"
myControl.ERBedNumber = "Bed13"
form1.Controls.Add(myControl)
myControl.Visible = True
End Sub
'This is working but this is not what I needed
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim myControl As statform = Page.LoadControl("~/MEDCONTROLS/statform.ascx")
myControl.ID = "statform13"
myControl.TempHospNum = "ER113"
myControl.ERBedNumber = "Bed13"
form1.Controls.Add(myControl)
myControl.Visible = True
End Sub
Use a variable which references the class name of the user control, and use Page.LoadControl.
You can interact with any properties you add to the user control class definition.
dim myControl as YourUserControlClassName = Page.LoadControl("~/yourlayoutfilepath/yourUserControlFile.ascx")
myControl.ID="stratFormX"
Me.Controls.Add(myControl)
I'm trying to write code that will, at the click of a button, push a string variable onto a page and then open that page. The trouble that I am running into is that variable, a button's text, is nested within a gridview, making accessing it difficult. It is also difficult because I am importing it from an SQL database into the Gridview, so I cannot access it directly from there either. It works with my current code if I use the button's onClick event, but then for some reason I have to click the button twice in order for it to work. I have read on here to instead use the onPreRender event, but that interferes with pushing the string variable onto the page, and I can't have that. Is there any other way to get rid of having to click the button twice that doesn't involve the onPreRender event?
Here is the code:
Imports System.Data.SqlClient
Imports System.IO
Partial Class PartsLookup
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Function FindControlRecursive(ByVal RootControl As Control, ByVal ControlID As String) As Control
If RootControl.ID = ControlID Then
Return RootControl
End If
For Each controlToSearch As Control In RootControl.Controls
Dim controlToReturn As Control = FindControlRecursive(controlToSearch, ControlID)
If controlToReturn IsNot Nothing Then
Return controlToReturn
End If
Next
Return Nothing
End Function
Protected Sub Button1Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim clickedButton As Button = sender
MsgBox(clickedButton.Text)
Dim ControlText As Control = FindControlRecursive(GridView1, clickedButton.ID)
Dim ControlText1 As Button = ControlText
Dim MachineNumber As String = ControlText1.Text
If MachineNumber = "" Then
ControlText1.Visible = False
End If
Dim SpecificPart() As String = {String that is the files contents, the middle of which is where the variable will be pushed to}
Dim path As String = "File path where the variable is to be pushed to"
File.WriteAllLines(path, SpecificPart)
clickedButton.PostBackUrl = "~/PartNumberPages/PartTemplate.aspx"
End Sub
End Class
I'm wishing to redirect to two different pages depending on which button is pressed on my form.
Both buttons call the Insert method of the Datasource, but only one of them needs to get hold of the newly inserted GUID and redirect using that GUID.
I'm doing it using this code
Protected Sub DSCustomers_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSCustomers.Inserted
Response.Redirect("~/addTicket.aspx?step=2&customerID=" & e.Command.Parameters("#customerID").Value.ToString)
End Sub
But how can I know if it's the other button that has been pressed and doesn't need to redirect?
I can't get access to the CommandArgument otherwise I would just check which button has been pressed using that and then redirect accordingly.
Thanks
You can't just use a global private boolean?
Dim RedirectButtonPressed As Boolean = false
Protected Sub RedirectButton_Click(sender As Object, e As EventArgs) Handles RedirectButton.Clicked
// code code code
RedirectButtonPressed = True
End Sub
Protected Sub DSCustomers_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSCustomers.Inserted
If RedirectButtonPressed Then
Response.Redirect("~/addTicket.aspx?step=2&customerID=" & e.Command.Parameters("#customerID").Value.ToString)
RedirectButtonPressed = False
End If
End Sub
I am trying to use the code below to store the items from the list into a session. For some reason when I debug the code the count is returning 0 even though there are multiple items in the list box? Any ideas what I am doing wrong here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NameTextBox_AutoCompleteExtender.OnClientItemSelected = "getSelected"
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPListbox.Items(i).ToString()
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=NameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPListbox.ClientID %>').options.add(opt);
}
I am going to guess that you do not have any logic in your Page_Load to populate the listbox, based upon what it had when you finished the autocomplete extender logic. Since, you do not, then when the click event fires after the Page_Load your values are gone.
Put the logic that executes on selection of the autocomplete extender in a method and have your Page_Load call that, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End Sub
Private Sub PopulateListBox()
' Go to whatever resource you are using to get the values for the list box
End Sub
UPDATE:
Since you are depending upon using a client-side function to grab the values from the autocomplete extender and populating the listbox that way, you need to mimic that logic in your Page_Load on the server-side, because it will be too late if you try to use the client-side one, since you need the data server-side and all of the server-side events happen before the client-side logic in a server post back.
You need to do something like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Only do this when page has posted back to the server, not the first load of the page
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Private Sub PopulateListBox()
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPListbox.Items.Add(newItem)
NPListbox.SelectedIndex = NPListbox.Items.Count - 1
End Sub
I have a button within a formview control on my page.
Because the button is contained within the formview, my code-behind can't see it.
So I did this:
Dim btnSave As Button = CType(fvCourse.FindControl("btnSave"), Button)
And then I added an event handler like this:
AddHandler btnSave.Click, AddressOf btnSave_Click
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.write("hey!")
End Sub
The problem is, I don't think it's working because I never see the "hey!" on my page.
Am I missing something?
Thanks
I don't know about missing something, but I reckon you could do it a simpler way since you're using VB. Give your button a command name and command argument first:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
CommandArgument="1" CommandName="yes" />
These can be anything - typically you use the command name to determine which button a user clicked on, and the command argument to show the record id.
In your code-behind, attach a macro to the ItemCommand event of the FormView (which fires when something happens within it):
Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub
And in VB, that's all you need to do!
You should use the ItemCreated event of the FormView for such things. If the Button is in the ItemTemplate you need to check for the FormViewMode.ReadOnly, for EditItemTemplate you need to use Edit:
Private Sub fvCourse_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCourse.ItemCreated
Select Case fvCourse.CurrentMode
Case FormViewMode.Edit
Dim btnSave As Button = DirectCast(fvCourse.FindControl("btnSave"), Button)
AddHandler btnSave.Click, AddressOf btnSave_Click
End Select
End Sub