LinkButton Click event ignored - asp.net

I have the following Hyperlink as a button:-
<asp:LinkButton ID="loginButton" runat="server" CssClass="loginButton" Text="LOGIN" OnClientClick="return validateLogin(memNoID,pwID)" AddressOf="loginButton.Click"></asp:LinkButton>
It causes a postback but only executes the onload and prerender sections of code. It totally ignores the following function signature:-
Protected Sub loginButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Some code
End Sub
Any pointers appreciated.

Add OnClick in your code:
<asp:LinkButton ID="loginButton" runat="server" CssClass="loginButton" Text="LOGIN" OnClientClick="return validateLogin(memNoID,pwID)" OnClick="loginButton_Click" AddressOf="loginButton.Click"></asp:LinkButton>

Did you try adding your _Click method to the OnClick event?

Related

Repeater FooterTemplate Button Click Event Not being handled

When debugging, clicking on btnSignup does not hit the breakpoint on the first line of my btnSignup_Click event.
Just to see if it'd help I removed the datasourceID from the repater and created a Sub routine to databind the repeater on page_load if-not-isPostback. That didn't help.
What do I need to do to get my btnSignup to fire my btnSignup_Click event?
I have this on my ASCX
<asp:Repeater ID="rptParticipants" runat="server" DataSourceID="sdsParticipants">
<ItemTemplate>
<p class="participant"><span class="participant-number"><%# Container.ItemIndex + 1 %>.</span> <span class="participant-name"><%# Container.DataItem("name")%></span></p>
</ItemTemplate>
<FooterTemplate>
<br />
<asp:Literal ID="litBlanks" runat="server"></asp:Literal>
<div class="text-center">
<asp:Button ID="btnSignup" runat="server" Text="Sign Me Up" CssClass="btnSignup" OnClick="btnSignup_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove Me" CssClass="btnRemove" OnClick="btnRemove_Click" />
</div>
</FooterTemplate>
</asp:Repeater>
I have this on my ASCX.vb
Public Sub btnSignup_Click(sender As Object, e As System.EventArgs)
CheckAvailability() 'Breakpoint on this line
'more code here
End Sub
Try using the ItemCommand Event on the Repeater to handle your button clicks. Replace the OnClick Attribute with the CommandName as shown below:
<asp:Button ID="btnSignup" runat="server" Text="Sign Me Up" CssClass="btnSignup" CommandName="SignUp" />
Next, create an event handler for your Repeater's ItemCommand Event:
Protected Sub rptParticipants_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptParticipants.ItemCommand
If e.CommandName = "SignUp" Then
btnSignup_Click(Me, New System.EventArgs)
End If
End Sub

Button click event not fired after moving it to the asp:content

I just moved the button from html to the asp:content because I use master page :
<asp:content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"></asp:CustomValidator>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Upload" />
</div>
</asp:content>
The following code was working before I moved it, now the click event is not getting fired :
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
And it gave me an error (This error didn't occur before):
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
So I added the below line of code :
Private WithEvents Button1 As Button
But still, the button1 is never fired.
Please kindly help me.
The ContentPlaceHolder is a different NamingContainer than the Page(it implements INamingContainer), so it's not initialized automatically from ASP.NET if you declare the variable Private WithEvents Button1 As Button.
You have to attach the event handler declaratively (or programmatically from codebehind):
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Upload" />
and remove the Handles clause, the method must now be at least Protected:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub

In ASP.NET, how do I change the visibility of a button in the same column of a gridview itemtemplate when the other button is clicked?

I have the following gridview:
<asp:GridView ID="gridOpenMECs" runat="server">
<Columns>
<ItemTemplate>
<asp:ImageButton ID="btnShow" runat="server" ImageUrl="xxx.png"
OnClick="btnShow_OnClick" />
<asp:ImageButton ID="btnHidden" runat="server"
ImageUrl="yyy.png" Visible="false" />
</ItemTemplate>
</Columns>
</asp:GridView>
When button1's onclick serverside event is fired I want to obtain a handle on button2 so that I may change its Visible attribute to True.
Protected Sub btnShow_OnClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim btn as ImageButton = CTYPE(sender, ImageButton) 'get the sending button handle
'' what next to make btnHidden visible?
End Sub
How can I accomplish this? Thank you.
Sorry, C# speak ...
GridViewRow whichrow = (GridViewRow)btn.NamingContainer;
ImageButton btnHidden = (ImageButton)whichrow.FindControl("btnHidden")

textChanged event doesn´t fire and i have autopostback=true

I have a textbox event declarated but it doesn´t fire. I have seen in SO other answers but all of them say that autopostback property has been true and i have it
my aspx
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
<Juice:Datepicker ID="Datepicker2" runat="server" TargetControlID="txtDia"
DateFormat="dd/mm/yy"
MonthNames="Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre"
MonthNamesShort="Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,"
AutoPostBack="True" /></td>
and my aspx.vb
Protected Sub txtDia_TextChanged(sender As Object, e As System.EventArgs) Handles txtDia.TextChanged
CargarDatos()
End Sub
You should define your textbox like this (see OnTextChanged="txtDia_TextChanged" being added):
<asp:TextBox OnTextChanged="txtDia_TextChanged"
runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox"/>
And remember that this event will rise onblur (focus removed from that textbox) only.
In your aspx you should write
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
The event is not triggered if you overwrite the text in codebehind. So for example if you databind the control where the TextBox sits in.
You should so that only If Not IsPostBack:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
DataBindAllControls(); // including your textbox
}
}
Edit: Sorry, here VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
DataBindAllControls() ' including your textbox
Else
End Sub
What FAngel said is correct.
I've had the same issue when validating dates and using jquery datepicker. I used the below to fire the validation routines after the box had been populated. So technically they fired twice, once after the textbox had lost focus to the datepicker, then again when my code fired after the datepicker had populated the textbox. It would have been better to disable the onblur event and call directly from the below, but wasn't possible in my project.
$('.datePicker').each(function () {
$(this).datepicker({
onSelect: function () {
$(this).trigger('blur');
}
});
});
You can use a variation on this by disabling the auto-postback and manually triggering it via the onSelect event.
You need to specify the method to be called when the event is fired like so:
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px" OnTextChanged="txtDia_TextChanged" AutoPostBack="True" CssClass="textbox"/>
Note:
OnTextChanged="txtDia_TextChanged"

Not able to click asp link button

I have a linkbutton displaying the path of an uploaded document..The tag structure is like so
<tr>
<td>
<asp:Label ID="lblDoc" runat="server" Text="Document:"></asp:Label>
</td>
<td colspan="3">
<asp:LinkButton ID="lnkDoc" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton>
</td>
</tr>
I am handling the onclick event on the server side:
Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.open('OpenDocument.aspx?DocumentPath=" & System.Web.HttpUtility.UrlEncode(lnkDoc.Text) & "', 'OpenUploadedDoc','left=0px,top=0px,width='+screen.availWidth+',height='+screen.availHeight+',menubar=yes,resizable=yes,scrollbars=1')</script>")
End Sub
But I am not able to click the document path. When I hover over the linkbutton, nothing happens. What could be the issue?
EDIT:
When I try to add onclick on the client side, I get the following error.
Transfer.Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs)' is not accessible in this context because it is 'Private'.
I think there are some validation on your page so click event is not fire so please set causesvalidation="false" of LinkButton as below code and check again:
<asp:LinkButton ID="lnkDoc" causesvalidation="false" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton
Thanks,
Hitesh
I have fixed the issue. The problem was with rendering the linkbutton in HTML(which was inside a panel). So I placed it in a outside the Panel.
</asp:Panel>
<div>
<asp:Label ID="lblDoc" style="margin-left: 3px" Text="Document: " runat="server"></asp:Label>
<asp:LinkButton ID="lnkDoc" style="margin-left:100px" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton>
</div>
Thanks all for your suggestions..!
ASPX
<tr>
<td>
<asp:Label ID="lblDoc" runat="server" Text="Document:"></asp:Label>
</td>
<td colspan="3">
<asp:LinkButton ID="lnkDoc" CausesValidation="false" runat="server" Text="Test"></asp:LinkButton>
</td>
</tr>
Code behind
Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>alert('hi')</script>")
End Sub
try to give onclick event on .aspx page, and check it..
i hope that way it will work
Protected Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.open('OpenDocument.aspx?DocumentPath=" & System.Web.HttpUtility.UrlEncode(lnkDoc.Text) & "', 'OpenUploadedDoc','left=0px,top=0px,width='+screen.availWidth+',height='+screen.availHeight+',menubar=yes,resizable=yes,scrollbars=1')</script>")
End Sub
Note: make your event Protected not Private
And also You need to add the click event to link button.
<asp:LinkButton ID="lnkDoc" runat="server" PostBackUrl="~/Transfer.aspx" onclick="lnkDoc_Click"></asp:linkbutton>
Add text to the button and make the handler public.
You need to add the click event to link button.
<asp:LinkButton ID="lnkDoc" runat="server" onclick="lnkDoc_Click"></asp:linkbutton>
Page behind file you have to write
Protected Sub lnkDoc_Click((ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDoc.Click
// code you want to run
End Sub
Maybe you want to set LinkButton.OnClientClick property? It let's you specify client-side handler for rendered link button control.
Take a look:
<asp:linkbutton id="LinkButton1" text="Open Web site" onclientclick="Navigate()" onclick="LinkButton1_Click" runat=Server />
Here Navigate() is a JavaScript function called upon click, and LinkButton1_Click is a server side event handler.
You want to open a new browser window to display a document when user clicks a link, right?
So a better approach will be to create JavaScript function with documentPath parameter (set on the server-side). No need to for messy RegisterStartupScript. You seem to run in circles now. Take a step back and rethink what you really want your code to do.

Resources