how to perform Click event in Gridview - asp.net

I have taken a Gridview and in Gridview I have taken an Image Button.
Now I want to perform click event on that Image Button.
How can I do that??
Tell me something about click events using Gridview.

You need to bind RowCommand event of gridView, You can not bind click event to button as you do with button not in GridView.
GridView html
<asp:GridView id="GridView1" Runat="server" OnRowCommand="GridViewCommandEventHandler" />
Event handler
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
}

You can directly create click event on that Image Button by double click on Image Button
Or Manually you can add
<asp:ImageButton id="ImageButton1" Runat="server" OnClick="ImageButton1_Click"/>
and in code behind file you can use
void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
// you code here...
}

Related

Button click not firing in UserControl

I have user control deriving from BaseUserControl class and Now I need to add button in UserControl file(ascx)
I have added following code snippet in ascx:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
Below is the code sample of ascx.cs file
public partial class MyContol : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// code to execute after button is clicked
}
}
But here the button event is not triggering and during debug I am able to see its hitting Page_Load event, please help me to resolve on this.
Regards
Anand
I have seen this type of thing many times. Try the following.
Remove the button from the ASCX side of the page.
Remove the C# btn submit method.
Save the page.
Now go into design view...
Add the button back, assign the name in design view.
In Design View double click on the button so that it created the C# method for you.

asp gridview plus button command

I have a Gridview based on a Acces DB in .aspx
I added +1 column to the grid, which is:
<asp:TemplateField HeaderText="view">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Select" CommandName="Select" CausesValidation="False" id="Button1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I have a button outside of the grid: Button2
Could I add a command to Button1, to simulate to click on Button2 as well?
EDIT: Found a link here: that talks specifically about wiring multiple events to the same handler, from MSDN:
If you already have an event handler, you can bind several control
events to it. These multiple events can be from the same control or
one event from several different controls, as long as the events all
have the same method signature. For example, you might want to bind
the Click events of several Button server controls on an ASP.NET page
to a single event handler. When your handler is called, you can
determine which control caused the event.
Yes! If you are familiar with wiring up a button then all you have to do is point both of them to the same function and clicking either one will fire the same function.
If you want to do this withing visual studio just double click each button in the designer and this will give you a function on the code behind, within that function just add a call to the function you want to call.
You should create a seperate function that is called from both buttons.
Like this:
protected void Button1_Click(object sender, EventArgs e)
{
buttonCallFunc();
}
protected void Button2_Click(object sender, EventArgs e)
{
buttonCallFunc();
}
protected void buttonCallFunc()
{
//Code goes here
}

Click on an 'image button' in a repeater in asp.net

I have a repeater and there is an image button in each row of this.
By clicking on each Image button ,I should get the Id of respective row but I don't Know how to manage the click event of this image button in repeater.
Try this:
ASPX
<asp:LinkButton OnCommand="lbRemove_Command" CommandArgument='<%# Eval("Key")%>' CommandName="Remove" runat="server">
Code Behind
protected void lbRemove_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Remove":
string ca = e.CommandArgument.ToString();
break;
}
}
You need to use ItemCommand event of repeater. You may also look in to ItemDataBound for custom actions on while binding data source.

Event that fires when user clicks on the "New" button of an ASP.Net DetailsView

In an ASP.Net VB.Net code-behind file, can you tell me what event fires that I can use to trap when the user clicks on the "New" button of a DetailsView at the time the DetailsView shows blank items such as DropDownLists, TextBoxes etc?
I need use this event handler to pre-select a value in a DropDownList obtained from a variable.
Try the ItemCommand event. This is how to use it. I only know C# , but I think you get the hang of it.
Code behind:
protected void DetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "New")
{
// Your code here
}
}
Markup:
<asp:DetailsView ID="DetailsView" runat="server" OnItemCommand="DetailsView_ItemCommand" ...

Mulitple linkbuttons in user control needs to call event handler in the aspx page

I have multiple user controls on a page and each of them have multiple linkbuttons that perform the same logic on the server side. Is there a way for all the linkbuttons to have the same event handler that is defined in the code behind of the page?
If needed, I can change the linkbuttons to be any other HTML or ASP.NET control as long as it can support a clickable image.
Inside your usercontrol create an event and wire it up. Then call it from the linkbuttons OnClick event handler:
UserControl.ascx:
<asp:LinkButton
runat="server"
id="linkbutton1"
OnClick="LinkButtonClicked">
Click Me
</asp:LinkButton>
<asp:LinkButton
runat="server"
id="linkbutton2"
OnClick="LinkButtonClicked">
Click Me Again
</asp:LinkButton>
UserControl.ascx.cs
public event EventHandler UserControlLinkClick;
protected void LinkButtonClicked(object sender, EventArgs e)
{
if (this.UserControlLinkClick!= null)
{
this.UserControlLinkClick(this, e);
}
}
Inside your parent page wire up the user controls UserControlLinkClick:
protected override void OnInit(EventArgs e)
{
MyUserControl uc = LoadControl("~/PathToUserControl.ascx");
uc.UserControlLinkClick += new EventHandler(MyUserControl_UserControlLinkClick);
}
protected void MyUserControl_UserControlLinkClick(object sender, EventArgs e)
{
//this code will execute when the usercontrol's LinkButtonClicked event is fired.
}
Hope that helps!!
Create an Event/Delegate in your custom control. Then in your page code behind, you can add event handlers or subscribe methods to the delegate.
Link for Events
Link for Delegates
Hope this helps

Resources