vb.net button click event not firing first time - asp.net

I have a very simple Textbox and Button, but Button click event not firing at first time ( only firing in second click)
<asp:TextBox ID="TxtSurvey" runat="server"></asp:TextBox> <br />
<asp:Button ID="BtnGoSurvey" runat="server" Text="LetsGo"/>
The code is very simple too
Protected Sub BtnGoSurvey_Click(sender As Object, e As EventArgs) Handles BtnGoSurvey.Click
Session("IDSurvey") = TxtSurvey.Text
BtnGoSurvey.PostBackUrl = "~/Survey.aspx"
End Sub
What i'm doing wrong ?
Thanks for help

If you can use Response.Redirect, then following is supposed to work:
Protected Sub BtnGoSurvey_Click(sender As Object, e As EventArgs) Handles BtnGoSurvey.Click
Session("IDSurvey") = TxtSurvey.Text
Response.Redirect("~/Survey.aspx",True)
End Sub

Related

Get Button Id Sender In Grid Batch update Dev Express vb.net

I have a method "Grid Batch Update", in this case, i want get an id from button clicked in this method
Private Sub grid_BatchUpdate(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataBatchUpdateEventArgs) Handles grid.BatchUpdate
'My Code
End Sub
I have tried to search but not found the answer
Ok, then as a general rule, you can use "sender".
Protected Sub Button1_Click1(sender As Object, e As EventArgs) Handles Button1.Click
Dim btn As Button = sender
Debug.Print("id of button is " & btn.ID)
End Sub
However, do note if you going to have say several buttions use the SAME event?
Then you should remove the handles from above. And then you "must" then set the on-click event.
So, above becomes:
Protected Sub Button1_Click1(sender As Object, e As EventArgs)
Dim btn As Button = sender
Debug.Print("id of button is " & btn.ID)
End Sub
And I might then say have two buttons use the same click event. eg this:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<br />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button1_Click1" />
So, if you double click on a button from the web forms designer, it will NOT put in a OnClick tag, but uses the "handles button one in code behind.
Either way is fine - but you don't need (nor want) the "handles" tied to one button on the code behind - so use markup OnClick="Button1_Click1"
In above, then clicking on either button would result in this:

Find a control from an updatepanel and manipulate it

I have a .aspx page that contains a hidden panel that I need to display from a .ascx control.
On my .Click event from my button on my .ascx I have the following:
Dim myControl2 As Control = FindControl("Content1")
Dim myControl3 As Control = FindControl("keywordSearchModal")
On my .aspx I have the following:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="server">
<asp:Panel ID="keywordSearchModal" runat="server" Width="800px" Visible="true">
This is your modal
<asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>
</asp:Content>
Both myControl2 and myControl3 contain nothing when the button is clicked.
How can I find this control from the .ascx that also happens to be an updatepanel.
UPDATE
I was able to find the control by doing the following:
Dim myPanel As Panel = Page.Master.FindControl("ContentHolder").FindControl("keywordSearchModal")
myPanel.Visible = False
Dim myControl As Control = Page.Master.FindControl("ContentHolder").FindControl("keywordSearchModal")
myControl.Visible = False
I was able to use the ContentPlaceHolderID - However now I can't change the visibility...
UPDATE II
thanks to the suggestion below I was able to capture the button click on my .aspx page. However, now just like before I can't seem to manipulate the control. In this example adding text to the panel.
Protected Sub SearchStart(ByVal sender As Object, ByVal e As EventArgs) Handles EquipmentDetails1.SearchStart
Dim newLabel As New Label
newLabel.Text = "This is a label!"
keywordSearchModal.Controls.Add(newLabel)
End Sub
Thoughts?
Here is what I've used in the past...
My user control has an imagebutton and the following in the codebehind
Public Event SearchStart As EventHandler
'This will be the event triggered when the imagebutton is clicked
Protected Sub ib_Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ib_Search.Click
'Bubble up event to parent
RaiseEvent SearchStart(Me, e)
End Sub
Any page that uses the User Control will have the following in codebehind...
Protected Sub mc_SearchFilter_SearchStart(ByVal sender As Object, ByVal e As EventArgs) Handles mc_SearchFilter.SearchStart
'CODE that needs to be executed when search image button is clicked in my_control_SearchFilter
End Sub
Hope that helps....

Is it possible to disable an ImageButton when processing?

Is it possible to disable an ImageButton when running a process?
I found a way to disable a Button when processing, doing this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString))
End If
End Sub
But it didn't work for ImageButton.
Also, I tried this code for disabling an Imagebutton:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString) + ";return false;")
End If
End Sub
Neither of both ones makes ImageButton to be disabled when processing, but it works for Button.
this suits better for full postback solution, if you are using ajax, I'd recommend to consider a jquery plugin or any other javascript plugin you can search for, saying that well hereĀ“s a small work around:
you need to use the onClientClick event that's provided OOB for the image button, it's easier to manage due to it's directly tied to the onlick event over the client and there's no nee to registar a custom client event when clicking the button.
all you need to do is passing your code to the onClientClick event
<asp:ImageButton runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Processing...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />
take a look to this solution

Handling events of usercontrols within listview

I have a simple usercontrol which raises an event on button click
Public Class UcPaymentCheque
Inherits System.Web.UI.UserControl
Public Event OnCancelClick()
Private Sub btnCancelPayment_Click(sender As Object, e As System.EventArgs) Handles btnCancelPayment.Click
RaiseEvent OnCancelClick()
End Sub
End Class
This usercontrol is used within a listview
<asp:ListView ID="lvwNonTpProducts" runat="server" ItemPlaceholderID="ItemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="ItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<TPCustomControl:UcPaymentCheque ID="UcTPPaymentCheque" runat="server" Visible="false" />
</ItemTemplate>
</asp:ListView>
which is databound on page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
BuildPage()
End If
End Sub
At what point should add the handler? I have fiddled with the ondatabound event like so;
Private Sub lvwNonTpProducts_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvwNonTpProducts.ItemDataBound
Dim UcTPPaymentCheque = DirectCast(e.Item.FindControl("UcTPPaymentCheque"), UcPaymentCheque)
AddHandler UcTPPaymentCheque.OnCancelClick, AddressOf OnCancelClick
End Sub
but this does not work and am guess at a databound issue???
You can check out my response to a similar question here: creating and listening for events
Essentially, you want a user control to raise its own event, like this:
Partial Class myControl
Inherits System.Web.UI.UserControl
Public Event MyEvent As EventHandler
'your button click event
Protected Sub bnt_click(ByVal sender As Object, ByVal e As EventArgs)
'do stuff
'now raise the event
RaiseEvent MyEvent (Me, New EventArgs)
end sub
end class
In this example, I raise the event when the user clicks a button within the user control. You can easily raise the event anywhere, such as when the control loads, using a timer, whatever.
Then, in the main page, you want to and an event handler to the user control, like this:
<mc:myControlrunat="server" ID="myControl1" OnMyEvent="myControl_MyEvent" />
Now, in the code behind, you can add the event, like this:
Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs)
'do stuff
end sub
You can add the handler in the declaration of the user control in the listview, using OnCancelClick, as follows:
<asp:ListView ID="lvwNonTpProducts" runat="server" ItemPlaceholderID="ItemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="ItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<TPCustomControl:UcPaymentCheque ID="UcTPPaymentCheque" runat="server" Visible="false" OnCancelClick="UcTPPaymentCheque_OnCancelClick" />
</ItemTemplate>
</asp:ListView>
Where UcTPPaymentCheque_OnCancelClick is the function you should use to handle the event, in the control that contains the listview.

Button Click and LinkButton Click

I am having a strange day! I am trying to access values inside the Datagrid control. There are textboxes in each row of the Datagrid control. If I use a asp.net button control outside the datagrid control I can access all the values. But if I use LinkButton outside the datagrid control then I cannot access any value.
I have to use LinkButton.
UPDATE 1: Code
//This works
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
//This does not work and I get 0 items in the SelectedIndexNumbers
Protected Sub LinkButtonUpdateReceipts_Click(sender As Object, e As EventArgs) Handles LinkButtonUpdateReceipts.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
<asp:Button ID="Button1" runat="server" Text="Button" />
<c1:LinkButton ID="LinkButtonUpdateReceipts" runat="server" Text="Update Totals" Icon="images/go-blue-right.gif"></c1:LinkButton>
I'm suprised that either of those work. You do not appear to have an OnClick event for either of those buttons. At a minimum I would add an OnClick event to your LinkButton that fires LinkButtonUpdateReceipts_Click.
Also, why aren't you just using <asp:LinkButton? what is <c1:LinkButton?

Resources