Mulitple linkbuttons in user control needs to call event handler in the aspx page - asp.net

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

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
}

How i can call JavaScript function from asp.net server side if script in user control

I have a ASP.NET page with 1 user controls registered.
i have one server control
<asp:LinkButton ID="vidoUpdatebtn" OnClick="vidoUpdatebtn_Click" runat="server">LinkButton</asp:LinkButton>
in .cs i handle
protected void vidoUpdatebtn_Click(object sender, EventArgs e)
{
//do something
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "doSomeThing()", true);
}
in user control i have function doSomeThing()
function doSomeThing() {
alert("TestReady");
}
when i click on LinckButon registred function dosen't work
Write this in PageLoad (the only modifications from what you were writing are the moment in time of the registration -- PageLoad not Click, Click is too late and the face that you must write the actual implementation of the javascript function here):
protected void Page_Load(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript",
#"function doSomeThing() {
alert(""TestReady"");
}"
, true);
}
and this in the asp:LinkButton tag (you must specify the OnClientClick attribute):
<asp:LinkButton ID="vidoUpdatebtn" runat="server" OnClick="vidoUpdatebtn_Click" OnClientClick="doSomeThing()" >LinkButton</asp:LinkButton>
And it should work.
Have you tried this way:
Add Script manager on your aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Codebehind:
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript", "doSomeThing()",true);
If you want client side code to run on the button click, you could use the OnClickClick property instead of the OnClick event. That'll also avoid the need for a postback and will give immediate feedback to the user.
e.g.
<asp:LinkButton ID="vidoUpdatebtn" OnClientClick="alert('TestReady'); return false;" runat="server">LinkButton</asp:LinkButton>

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" ...

asp:UpdatePanel with an ASP.NET checkbox trigger

What is the EventName property when I set up a ASP.NET checkbox control as an async postback trigger for an asp.net update panel?
I believe it is CheckedChanged.
All you have to do is set AutoPostback to true and if your CheckBox is within the UpdatePanel you shouldnt have any problems
<asp:CheckBox runat="server" ID="chk_Name" AutoPostBack="true" OnCheckedChanged="chk_Name_OnCheckedChanged"></asp:CheckBox>
Then in the OnCheckedChanged function you can do whatever you need to do
protected void chk_Name_OnCheckedChanged(object sender, EventArgs e)
{
// Do stuff here
}
OnCheckedChanged is the event name. You can autogenerate the method by double clicking the checkbox in the UI, and based on the checkbox name it will generate the method which will most likely be:
protected void CheckBox1_OnCheckedChanged(object sender, EventArgs e) {}

Resources