C# .NET - javascript to call click even of usercontrol button in master page
Asked By hiral jani
04-Feb-12 03:08 AM
Hello,
I have 1 web applicaion & In that I have 1 master page & in the master page I have 1 usercontrol.
there are two content pages.
I want that when I access the first content page & click the button of user control then it should call the function in user control but data should be passed of 1st page as parameter
Kindly give some suggestion along with some code / example
dipa ahuja replied to hiral jani
Ur query is confusing , do you have written any code? then post so we can correct it or tell us with detail..
[)ia6l0 iii replied to hiral jani

You seem to be going thru a bad design. You are tight coupling the master page controls to the content page. You can use "Events" alternatively.
Follow the normal subscription pattern, where controls on master page raise events, and content pages who have subscribed to it , get notified.
So Let's do it step by step.
a) In your master page, create a commandeventhandler.
public event CommandEventHandler MasterPageSomethingChanged;
b) In your master page, you should raise an event at appropriate place.You say you have a usercontrol and a button. Let's use the button control's click event, to raise this custom event as well.
protected void btnRaiseEvent_Click(object sender, EventArgs e)
{
//Check if there are subscriptions
if (MasterPageSomethingChanged != null)
MasterPageSomethingChanged(this, new CommandEventArgs(send the values that you want pass));
}
c) In the Content Page, you need to subscribe to the master page event first. Let's call your content page as contentpage.aspx
protected void Page_Init(object sender, EventArgs e)
{
Master.MasterPageSomethingChanged += new CommandEventHandler(ContentPageEventHandler);
}
d) Add the event handler in contentpage to handle the event.
private void ContentPageEventHandler(object sender, CommandEventArgs e)
{
//use the commandEventArgs to read values that has been sent.
}
Hope this is understable.

Reload calling page in ASP.NET 2.0 .NET Framework Hi I have a list on LinkButton controls on a page. When the link is clicked the LinkButton.Command event does a Server.Transfer to a page which writes binary to the HTTP output stream, e.g. a word document, pdf etc Response.BinaryWrite(buffer); Response.End(); I would like to reload the page with the LinkButtons after this event to reflect changes which are made after the download need to do a postback and then write some client script to open the download page? What's the neatest solution please? Many thanks Andrew ASP.NET Discussions ASP.NET (1) IIS (1) JavaScript (1) RegisterClientScriptBlock (1) CommandEventArgs (1) CommandEventHandler (1) RegisterStartupScript (1) LinkButton (1) Hi Andrew, When the LinkButton is clicked, in its Command Server.Transfer; instead, you use ClientScript.RegisterStartupScript to register a script to open the download page in a new window: void Link1_Command(object sender, CommandEventArgs e) { ClientScript.RegisterStartupScript(GetType(), "download", } http
i<3;i++){ myButton = new LinkButton(); myButton.CommandName = "MyCommand"; myButton.CommandArgument+ = new System.UI.WebControls.CommandEventHandler(MyButton_Click); myButton.Text = "Page " + Convert.ToString(i+1); } } protected void MyButton_Click(object sender, CommandEventArgs e) { ViewState["pg"] = e.CommandArgument.ToString(); } My problem, I suppose it's very simple to I thank you in advance for your help Stan ASP.NET Discussions System.UI.WebControls.CommandEventHandler (1) CommandEventHandler (1) CommandEventArgs (1) Convert.ToInt32 (1) Page.IsPostback (1) LinkButton (1) EventArgs (1) MyButton.CommandArgument (1) Hi Stan Once button is clicked is executed. Change protected void Page_Load(object sender, EventArgs e) { } protected void MyButton_Click(object sender, CommandEventArgs e) { ViewState["pg"] = e.CommandArgument.ToString(); / / I change my code for this post myLabel.Text
Registering an event handler to a dynamically created button I"m designing a web page that creates buttons dymamically and then assigns an event handler to them. I have 2 files which I'm listing here: 1) ButtonCBtest.aspx: <%@ Page src = "buttonCB.cs" Inherits = "buttonCB"%> <html> <form runat = "server"> <asp:panel id = "tstPanel" runat = "server Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; public class buttonCB : Page { protected Panel tstPanel; protected void makebutton (Object s, EventArgs e) { Button tmpButton; tmpButton = new Button(); tstPanel.Controls.Add(tmpButton); tmpButton.Command + = new CommandEventHandler(this.btn_click); tmpButton.Text = "Press here"; tmpButton.CommandName = "write message"; } private void btn_click (Object s CommandEventArgs e) { Response.Write(" You pressed the button."); } } When I press the "btn2" button, the new main problem in that you have created a button the causes a postback. When the page reloads, your button isn't there. The page is reloading, but NOBODY PRESSED THE ADD BUTTON "button". This is such a classic question 1 OF PROGRAMMATICALLY CREATED CONTROLS: If you create a control and add it to the Page Control Tree programmatically, when the page reloads (because of a postback) you MUST HAVE A
creating dynamic textboxes in updatepanel Hi All: I am creating an asp.net page which has a dropdownlist with values 0, 1, 2, 3, 4 inside updatepanel When user tricky situation because you are dealing with dynamic controls you need to populate them on page init in order to persist viewstate, also events for controls added inside an update panel dynamically, and store it in a session variable (because the viewstate is not loaded during page init). Then on the page init load any controls you have previously added. Then handle the click event during the page load with some custom code instead or the normal click event. I have created a sample page to help test this. Here is the code for the aspx page (default.aspx): <%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Default.aspx.cs" Inherits = "_Default" %> <!DOCTYPE html PUBLIC "- / / W3C / / DTD