asp gridview plus button command - asp.net

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
}

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.NET call a function in code behind with LinkButton

I have this LinkButton here
<asp:LinkButton runat="server" ID="EditBtn" CssClass="LinkButton" Text="Edit" Width="45px" OnClientClick="Profiles_Edit" CommandName="edit" />
and I am trying to call this function in my code behind
protected void Profiles_Edit(Object sender, ListViewCommandEventArgs e)
{
//do something
}
but when I click on the button....nothing happens. My LinkButton is inside an ItemTemplate, which is inside a ListView, which is inside a ContentTemplate, which is inside a UpdatePanel....
What is wrong with the way I am calling it?
Thanks,
J
OnClientClick is for specifying the name of the JavaScript function on the client browser.
To call the server-side event, use OnClick.
Also, you may not need the CommandName attribute in this situation. It isn't clear where this LinkButton resides. If it's in a container like a ListView, you would handle it differently.

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

ASP.Net repeater Item Command not getting fired

OK, I've used repeaters literally hundreds of times without problems but something has gone awry today. I have a repeater and I'm subscribing to the itemCommand event, but when my command runs, the page posts back but the event isn't fired.
To get around this I'm having to do my databinding on each postback.
My repeater looks like this:
<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
<li>
<asp:Label id="Label" runat="server" />
<asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
and my codebehind like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetupPage();
}
}
private void SetupPage()
{
// Do other stuff
MyRepeater.DataSource = Repository.GetStuff()
MyRepeater.DataBind();
}
protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
// Do all my stuff here
}
MyRepeater_ItemCommand is not getting called unless I comment out the if (!IsPostBack) line. Once that is commented out and the repeater is getting databound on each postback it works OK. I've done this in so many other pages but on this on it just doesn't seem to work.
Anyone else come across this behaviour or have a solution?
Most likely, you have disabled ViewState for the page.
The reason is that when you execute the postback, all the controls in the repeater are rebuild from the data in the viewstate normally. Then the object that should receive the event is identified based on the ID of the control, and the event is routed.
If you disable the viewstate, the control tree is not rebuild during postback, and therefore the control that should receive the event does not exist in memory. So the event dies.
If you really want to disable the viewstate, but still want to receive the event, I have a workaround (and it's not dirty at all). I've long been thinking about writing a blog entry about it, so if you want, I can take a bit time off my normal chores, and describe it.
Edit: The workaround is described here: http://petesdotnet.blogspot.dk/2009/08/asp.html
Remove if (!IsPostBack) as this is preventing the repeater from rebinding,
and the item command event could not find the row after postback.
I have the same problem and aside from using update panel, I have a required field validator in my modal. I found out that the LinkButtons in my repeater triggers the requiredFieldValidor event and then I added CausesValidation="false" in the LinkButtons of my repeater. Works as expected.
I have this problem in a repeater when I use ImageButton ...
I have search the net for this solution when LinkButton work, but not ImageButton ...
Then I think, LinkButton work? so i will use it :)
<asp:LinkButton CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>
</asp:LinkButton>
So, the image is inside the <A> tag
have fun :)
I removed PostBackUrl property in linkbutton and ItemCommand fired. I think postback runs first.
That may be you have set Validations on your page. So set an new attribute, causevaliation = "false" to Link button. M sure it will solve the problem
I had a similar issue - turned out some discreet validation controls were firing elsewhere on the page. It only took me a day to figure it out ...
I am not positive about this, but you might have to set the CommandName and optionally CommandArgument properties for the button causing the ItemCommand event. Otherwise ASP.NET would assume that there is no button on the page, which you'd like to fire the event. You can try that.
Plus, if you're not differentiating between command names, why not use each button's Click event instead? Just subscribe to those in the repeater's ItemCreated or ItemDataBound.
Try using Page_init instead of Page_load and that should fix the problem.
Try this:
protected void Page_Load(object sender, EventArgs e)
{
SetupPage();
}
If you use nested-repeater, you should rebind your inner repe
Here Is the code you have to use in code behind..
after PageLoad event,
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
// Bind Your Repeater here
rptUser();
}
now you can fire your Itemcommand..if you get Output please mark answer as right thanks
One other thing that it could be (as it just happened to me): if your databind takes place when your page is prerendered, it won't process the item command. Switch it to load or init and you'll be fine.

Resources