trigger asp:button's click event by clicking another asp:button - asp.net

i have two asp:buttons.. say button1 and button2, what i would like to do is fire button2's click event when I click button1.. is there a way to trigger click event on an asp:button through code behind? please help, newbie here.

Well, you can put the same event for both buttons, something like this:
<asp:Button ID="btn1" runat="server" Text="Button 1" CommandName="Save" />
<asp:Button ID="btn2" runat="server" Text="Button 2" CommandName="Cancel" />
and in code behind (vb.net):
Protected Sub btn_event(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click
Dim btn As Button = CType(sender, Button)
'now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!
End Sub
C# code:
protected void btn_event(object sender, EventArgs e) {
Button btn = (Button)sender;
//now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!
}
If you're using C#, remember to set the event click on asp:button tag.

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:

vb.net button click event not firing first time

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

Checkbox OnCheckedChanged event not firing

I have a datagrid that is populated through user input, and in that grid I have a checkbox that should remove the row from the datagrid if checked. I have set the EnableViewState to true and false with no results. It appears that if the checkbox is clicked, the event does not fire. Debugging never gets to the event. Can anyone point me? Here's the markup:
<asp:TemplateColumn HeaderText="Remove">
<ItemTemplate>
<asp:checkbox ID="chkSelection" runat="server" AutoPostBack="True" Checked="false" OnCheckChanged="EPACheck_Clicked"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
And here's the code behind for the event:
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim ckbx As CheckBox = CType(sender, CheckBox)
Dim dgItem As DataGridItem = CType(ckbx.NamingContainer, DataGridItem)
Dim rcDelete As String = dgItem.ItemIndex
CType(Session("mytable"), DataTable).Rows.RemoveAt(Convert.ToInt32(rcDelete))
BindgrdEPA()
End Sub
Your method doesn't handle the event. Try the below snippet.
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Handles chkSelection.CheckedChanged
The reason OnCheckedChanged wasn't firing for me was because I disabled IE8's Protected Mode.
Once I enabled Protected Mode OnCheckedChanged worked.
Previously, I had disabled Protected Mode to test a cookie problem with one of our web applications. I didn't re-enable Protected Mode when I finished that task.
To re-enable Protected Mode I did this:
Clicked on Tools\Internet Options.
Clicked on Security tab.
Clicked on Reset all zones to default level.
I suppose I could have clicked "Enable Protected Mode" checkbox.

How to fire a button event from inside a repeater?

I have done my research but can't find an efficient way to do the following in VB:
Each button should fire the same event.
The button event saves every repeater item and so each event is not unique.
I am aware I can use the ItemCommand option but have not been able to get it working as desired.
ASP.NET
Inside Repeater Item
<asp:Button ID="btnSave" RunAt="Server"/>
VB.NET
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sqlConn.Open()
For Each Item As RepeaterItem In rpt.Items
...
Next
sqlConn.Close()
End Sub
Edit:
After some research here on SO, I found that others events than ItemCommand are not caught by Asp:Repeater, as FlySwat said on his answer. So you'll need to write your VB.NET code like this:
First, declare the ItemCommand event on your page with something like this:
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
If e.CommandName = "Save" Then
'Save
End If
End Sub
Then, on Asp:Button markup inside the Asp:Repeater, you must set its CommandName property like this:
<Asp:Button ID="btnSave" runat="server" CommandName="Save" UseSubmitBehavior="false"/>
Take a look here to learn more about the UseSubmitBehavior.
Try it.
When the button is inside a Repeater template, you need to add OnClick event, you can add event on ItemDataBound event of the Repeater control.
Your .aspx code will look something like this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="SomeText" />
</ItemTemplate>
</asp:Repeater>
code-behind
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
{
var btn = e.Item.FindControl("btnSave") as Button;
if (btn != null)
{ // adding button event
btn.Click += new EventHandler(btn_Click);
}
}
}
void btn_Click(object sender, EventArgs e)
{
//write your code
}
in vb.net
Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = Repeater1.AlternatingItem OrElse e.Item.ItemType = Repeater1.Item Then
Dim btn = TryCast(e.Item.FindControl("btnSave"), Button)
If btn IsNot Nothing Then
' adding button event
btn.Click += New EventHandler(btn_Click)
End If
End If
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
'write your code
End Sub

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