fire onClick event on ascx from aspx page c# - c#

I have save button on a dynamic usercontrol that I load onto aspx page, but I want to move the button onto .aspx page instead. How can I fire the onclick event from aspx to ascx.
Any help would be great.
Cheers.
Code Example:
ascx:
protected void BT_Save_Click(object sender, EventArgs e)
{//Save details currently on ascx page }
aspx:
protected void BT_aspx_Click(object sender, EventArgs e)
{
//when this button is clicked I need it to fire BT_Save_Click on ascx page to save the data
}

In user control
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
In User control .cs page
public event EventHandler ButtonClickDemo;
protected void Button1_Click(object sender, EventArgs e)
{
ButtonClickDemo(sender, e);
}
In Page aspx page
<uc1:WebUserControl runat="server" id="WebUserControl" />
In Page.cs
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);
}
protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
{
Response.Write("It's working");
}

Write a public / internal sub on the ASCX, and then call it from the onClick on the ASPX?

You'll need to create a custom event on your ascx user control and subscribe it within your aspx page.
have a look at this question
define Custom Event for WebControl in asp.net

Related

How to get event control from another aspx Page. button click or Page load | ASP.NET

1st Page.
<asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="Button1_Click" OnClientClick="return" />
Actually, I need to call this page from another 2nd.aspx.cs page.
public void Page_Load(object sender, EventArgs e){
//Do some stuff in the page load event handler.
}
public void Button1_Click(object sender, EventArgs e){
//Do some stuff in the button click event handler.
}
I change the button Access Modifiers property to public from the designer page.
public global::System.Web.UI.WebControls.Button Button1;
2nd Page
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1_Click();
This doesn't work!
1st Page.
FirstPageName.aspx page.
<asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="Button1_Click" OnClientClick="return" />
FirstPageName.aspx.cs page.
public void Button1_Click(object sender, EventArgs e){
//Do some stuff in the button click event handler.
}
Change the button Access Modifiers property to public from the designer page.
public global::System.Web.UI.WebControls.Button Button1;
2nd Page
Here, we can use 2 methods.
1st Method
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1_Click(firstPage.Button1, EventArgs.Empty);
2nd Method
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1.Click += new EventHandler(firstPage.Button1_Click);
I'm also attaching another two methods. Try this, This works for me.
YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Button.PerformClick();

How To Pass Textbox Value Present In UserControl To Aspx Page Label By Clicking Button In UserControl

TestUC.ascx Design Code
<asp:TextBox ID="txtbox1" runat="server" ClientIDMode="Static" placeholder="Enter Some Text" ></asp:TextBox><br />
<asp:Button ID="btn1" runat="server" Text="Click" OnClick="btn1_Click" ClientIDMode="Static" />
Test.aspx Page Code
<%# Register Src="~/WebUserControls/TestUC.ascx" TagName="WebUserControlTest"
TagPrefix="uctest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="server">
<asp:Label ID="lbl1" runat="server" >Label</asp:Label>
<uctest:WebUserControlTest ID="ucTest" runat="server"></uctest:WebUserControlTest>
</asp:Content>
OutPut:
I Need ..
Step1: Enter Some text In Text Box
Step2:Then I Click Click Button
[Note: This Two Controls Are Bind From UserControl]
Step3:What Text Entered in TextBox Is Show In label [Note Label Present In Aspx Page]
You will need to have a custom event & you will also need to expose the Text property of the TextBox in your UserControl, like this.
public partial class YourUserControl : UserControl
{
public String Text
{
get
{
return this.txtBox1.Text;
}
//write the setter property if you would like to set the text
//of the TextBox from your aspx page
//set
//{
// this.txtBox1.Text = value;
//}
}
public delegate void TextAppliedEventHandler(Object sender, EventArgs e);
public event TextAppliedEventHandler TextApplied;
protected virtual void OnTextApplied(EventArgs e)
{
//Taking a local copy of the event,
//as events can be subscribed/unsubscribed asynchronously.
//If that happens after the below null check then
//NullReferenceException will be thrown
TextAppliedEventHandler handler = TextApplied;
//Checking if the event has been subscribed or not...
if (handler != null)
handler(this, e);
}
protected void yourUserControlButton_Click(Object sender, EventArgs e)
{
OnTextApplied(EventArgs.Empty);
}
}
Then in your aspx page, where you have placed YourUserControl (OR you are dynamically adding it from the code behind), you can subscribe to this event like this.
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
yourUserControl.TextApplied += new YourUserControl.TextAppliedEventHandler(yourUserControl_TextApplied)
}
}
You can use the custom event of the user control in your page like this.
protected void yourUserControl_TextApplied(Object sender, EventArgs e)
{
yourLabelInYourPage.Text = yourUserControl.Text;
}
And you are done...
EDIT : You can rename the Controls & Events as you like. I have used the names only for the example purpose.
EDIT : In website projects, if you want to add your user control dynamically then,
you might need to include the namespace ASP in your page, like this.
using ASP;
And add this Directive in your page in the aspx markup.
<%# Reference Control="~/PathToYourUserControl/YourUserControl.ascx" %>
other solution : create an event in the usercontrol, which is called in the button click.
Subscribe to this event in the codebehind of the aspx page. that way you can update your interface only if a value is provided.
a little more complicated, but you could re-use this logic to more complex control / parent control feature in the future.
i can add code snippet if asked
This Answer Is Prepared By Help Of #Devraj Gadhavi i Edited Some Code .
UserControl Page Design Code
<asp:TextBox ID="txtbox1" runat="server" ClientIDMode="Static" placeholder="Enter Some Text" ></asp:TextBox><br />
<asp:Button ID="btn1" runat="server" Text="Click" OnClick="btn1_Click" ClientIDMode="Static" />
UserControl Page Code
public partial class TestUC : System.Web.UI.UserControl
{
public String Text
{
get
{
return this.txtbox1.Text;
}
}
public delegate void TextAppliedEventHandler(Object sender, EventArgs e);
public event EventHandler TextApplied;
protected virtual void OnTextApplied(EventArgs e)
{
if (TextApplied != null)
TextApplied(this, e);
}
protected void btn1_Click(object sender, EventArgs e)
{
OnTextApplied(EventArgs.Empty);
}
}
Aspx Page Design Code
<%# Register Src="~/WebUserControls/TestUC.ascx" TagName="WebUserControlTest"
TagPrefix="uctest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="server">
<asp:Label ID="lbl1" runat="server" >Label</asp:Label>
<uctest:WebUserControlTest ID="ucTest" runat="server"></uctest:WebUserControlTest>
</asp:Content>
Aspx Cs File Code
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ucTest.TextApplied += new EventHandler(ucTest_TextApplied);
}
protected void ucTest_TextApplied(Object sender, EventArgs e)
{
lbl1.Text = ucTest.Text;
}
}
Another method ,If you don't want to expose the Text property of the TextBox in your UserControl Just use method "UserControl.FindControl("Id Of your Textbox which is present in user control")" in your Case WebUserControlTest.FindControl("txtbox1").
And below is the simpler way to register an event handler on the parent web form's code behind.
Code goes as below for parent form asxp.cs
protected override void OnInit(EventArgs e)
{
//find the button control within the user control
Button button = (Button)WebUserControlTest.FindControl("btn1");
//wire up event handler
button.Click += new EventHandler(button_Click);
base.OnInit(e);
}
void button_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox) WebUserControlTest.FindControl("txtbox1");
//id of lable which is present in the parent webform
lblParentForm.Text=txt.text;
}
Also, you can achieve this using JavaScript like below (given your code above):
<script type="text/javascript">
function getUCTextboxValue() {
let txtName = document.getElementById('<%=ucTest.FindControl("txtbox1").ClientID %>');
let lbl = document.getElementById('<%=lbl1.ClientID %>');
lbl.innerText = txtName.value
}
Also, from the parent page, you can add a HiddenField and save the textbox value on it (using the JavaScript code above) then catch its value from code behind.

OnClick event not working for linkbutton in a master page

This is the html ,
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
This is the code behind,
protected void hlnkLogoffF_Click(Object sender, EventArgs e)
{
//do something here
}
When i run this, i get the following error,
"The resource cannot be found. "
You have to use
if (!Page.IsPostBack)
{
// Your code here
}
in code behind of master page.
Also
What happens if you turn off validating with CausesValidation setting to false?
i tried to run your code
.aspx
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
.Cs
protected void hlnkLogoffF_Click(object sender, EventArgs e)
{
}
i Haven't get any error.Could you please post the code inside the click event.?

ASP.NET Button event is not raised in Web User Control

I have a ASP.NET user control, which contains a Telerik Report Viewer plus a button (server control).
I need to handle some stuff inside the click event of the button, but the event does not seem to fire.
Does anyone know why this is the case?
Here is the HTML directives inside the UserControl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ReportControl.ascx.cs" Inherits="TelerikReportCustomRetrive.UserControl.ReportControl" %>
Here is the markup inside the UserControl:
<form runat="server" id="form1">
<telerik:ReportViewer ID="ReportViewer1" runat="server" Height="461px" ShowDocumentMapButton="False" ShowHistoryButtons="False" ShowNavigationGroup="False" ShowParametersButton="False" ShowPrintPreviewButton="False"></telerik:ReportViewer>
<asp:Button runat="server" ID="btnNav" OnClick="btnNav_Click" />
And the code behind code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var instanceReportSource = new InstanceReportSource
{
ReportDocument =
new TheReport()
};
ReportViewer1.ReportSource = instanceReportSource;
}
}
protected void btnNav_Click(object sender, EventArgs e)
{
Response.Write("Button Fired!");
}
Delete the if(!PostBack) statement. You should always initialize the control, not only if page is not post back.

OnClick event not firing on ModalDialog

I have a Modal Dialog:
function ShowPopup()
{
window.showModalDialog('dialog.aspx', null, 'status:no;dialogWidth:950px;dialogHeight:150 px');
}
Then its called in the code behind
Page.ClientScript.RegisterStartupScript(this.GetType(), "popUpScript", "ShowPopup();", true);
The dialog.aspx has two buttons:
<asp:Button id="btn1" runat="server" Text="Button 1" OnClick="btn1_Click"></asp:Button>
<asp:Button id="btn2" runat="server" Text="Button 2" OnClick="btn2_Click"></asp:Button>
However, the Click events in the code behind are never getting fired.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, System.EventArgs e)
{
Response.Redirect(url)
}
protected void btn2_Click(object sender, System.EventArgs e)
{
Response.Redirect(url);
}
}
I recall seeing this problem before and I think it is related to caching. Try adding this to your dialog.aspx page_load method when !IsPostBack
Response.AddHeader("Pragma", "no-cache")
To stop the browser caching the page and reusing it
Are you receiving any hidden JavaScript errors preventing the POST?

Categories