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
Related
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
I have a popup panel which contains an UpdatePanel, which contains a DataList. Table rows are populated using ItemTemplate and there is a LinkButton generated on each row for deleting this row. I would like to delete this record in the DataList's ItemCommand event handler and rebind the DataList.
However, after I click a "delete" button in the DataList, ItemCommand is not fired. I've already checked if IsPostBack in my Page_Load and only do Datalist.Databind() if it's not a postback. Normally I would expect first Page_Load and then list_ItemCommand being called after I click a delete button in the DataList, but list_ItemCommand is not called as expected. And nothing is then displayed in DataList which is inside the UpdatePanel.
And stranger, if I remove the IsPostBack check in Page_Load, that being said, rebind the DataList in every Page_Load, ItemCommand will be caught and list_ItemCommand is called. This is against the answers in many other posts "ItemCommand event will be canceled if DataList is rebinded during PostBack".
Code behind:
Protected records As New List(Of Record)
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Set some page properties...
If Not Page.IsPostBack Then
GetListOfRecordFromDatabase()
datalist.DataSource = records
datalist.DataBind()
End If
End Sub
Protected Sub datalist_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles datalist.ItemCommand
Select Case e.CommandName.ToLower
Case "delete"
For Each c As Record In records
If c.Id = e.CommandArgument Then
records.Remove(c)
Exit For
End If
Next
DeleteRecordFromDatabase(e.CommandArgument)
datalist.DataSource = records
datalist.DataBind()
End Select
End Sub
Controls:
<asp:Content ID="content1" runat="server" ContentPlaceHolderID="Content1PlaceHolder">
<asp:LinkButton ID="btnpopup" runat="server" OnClientClick="javascript:return popup()"></asp:LinkButton>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" EnableViewState="false" >
</asp:ScriptManagerProxy>
<asp:Panel ID="PanelPopup" runat="server" style="display:none;">
<asp:UpdatePanel ID="UPPopup" runat="server" UpdateMode="conditional" EnableViewState="false">
<ContentTemplate>
<div id="divPopup1" runat="server">
<table id="table1" cellpadding="2" cellspacing="1" width="500" border="0" >
<asp:DataList ID="datalist" runat="server" OnItemCommand="datalist_ItemCommand">
<ItemTemplate>
<tr align="center">
<td><%#Container.ItemIndex +1 %></td>
<td><asp:Label ID="Label1" runat="server" Text='<%# eval("Name") %>'></asp:Label></td>
<td><asp:Label ID="Label2" runat="server" Text='<%# eval("Color") %>'></asp:Label></td>
<td><asp:LinkButton ID="Delete" CommandName="Delete" runat="server" Text="Delete" CommandArgument='<%# eval("Id") %>' ></asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:DataList>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div style="text-align:center;"><br />
<asp:Button ID="BtnSavePopup" runat="server" Text="Save and Close"/>
</div>
</asp:Panel>
<script type="text/javascript">
function popup() {
return mypagehelper.openAsModalPopup("<% =PanelPopup.ClientID%>");
}
</script>
</asp:Content>
Further more, I tried to grab the ControlID and the Control who raised the event during Postback using this code:
If IsPostBack Then
Dim CtrlID As String = String.Empty
If Request.Form("__EVENTTARGET") IsNot Nothing And
Request.Form("__EVENTTARGET") <> String.Empty Then
CtrlID = Request.Form("__EVENTTARGET")
Dim postbackControl As System.Web.UI.Control = Page.FindControl(CtrlID)
Else
End If
And I found that I can get my CtrlID as "ctl00$datalist$ctl08$Delete" but the postbackControl is Nothing. While on my other normal pages I can get both the controlID and actual control(which is a LinkButton) who raised the event.
Remove EnableViewState="false" from update panel.
it's late answer but i faced in soon, also may be it help some one else ...
the CommandName="Delete" is reserved for DataList "OnItemDeleting" Event
so just changing the CommandName to anything else (for example "Remove") will help or handle the OnItemDeleting Event.
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")
i have a User Control.
In this user control I have a Repeater.
In this repeater I have again a User Control.
If I try to catch the click event of an ImageButton in this user control, I'm gettin nothing because the event has not been fired.
Is there a way to catch this event?
some code:
first user control:
<asp:Repeater ID="Rpt" runat="server">
<ItemTemplate>
<uc1:myUserControl id="myUserControl1" runat="server" />
</ItemTemplate>
</asp:Repeater>
in myUserControl
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClientClick="thisEventIsFiring();" OnClick="btn_Click" Text="btn" />
This event isn't firing:
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
' I am not firing
End Sub
Also the following does not work because it returns nothing:
If Request(btn.UniqueID) IsNot Nothing
'check if Button was clicked
End If
It should be
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClick="btn_Click" Text="btn" />
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?