Reach control from another page. ASP.Net
You need to get an instance of the form. See my two form project below
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog.";
}
}
}
ASP.net(C#): Is it possible to access controls on one webform from another?
I feel slightly stupid for how easy this ended up being, but here it is for anyone having the same issue.
Here's the design for a simple user control I used to test this. Its just a button. xD
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl3.ascx.cs" Inherits="AcmeCompany_DATA_CENTER_v2.WebUserControl3" %>
<asp:Button ID="Button1" runat="server" OnClientClick="Button1_Click" Text="Button" OnClick="ButtonCreate_Click" PostBackUrl="~/WebUserControl3.ascx" />
heres the code behind for the user control
protected void ButtonCreate_Click(object sender, EventArgs e)
{
WebPartManager wpm = (WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);
TextBox testBox = new TextBox
{
ForeColor = System.Drawing.Color.Blue,
ID = "testID",
Width = 500,
Height = 200
};
GenericWebPart testGWP = wpm.CreateWebPart(testBox);
wpm.AddWebPart(testGWP, wpm.Zones["WebPartZone4"], 1);
}
I was unaware of the Zones class inside of webpartmanager, and I came across it just scrolling through looking for something that may work. Tada.....It's always the little things.
P.S. @Maciej S. Thank you, While you didn't give me the answer, our conversations helped my brain think to look for certain things.
access control of another page asp.net
In general, this doesn't make sense.
When your second page is executing, the first page is gone. It simply no longer exists. There is no label for you to assign to.
Even if you could assign to the label, the previous request is over. The HTML (without the change) has already been sent to the user's browser.
How can i get all control in another page of site in code behind
I use HttpWebRequest
to solve this problem.
In Default.aspx
page when Clicked
on button run this code :
byte[] dataArray = Encoding.UTF8.GetBytes("");
//url = "http://localhost:50036/UI/Dashboard.aspx?Action=FindControl"
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = dataArray.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)httpRequest.GetResponse();
if (httpRequest.HaveResponse == true)
{
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();
/*
In responseString string i have all control and types seperated by `semicolon`(`;`)
*/
}
else
Console.Write("no response");
In this code url
Variable contain url of Dashboard.aspx
Note:url must contain http://
otherwise doesn't work
In Dashboard.aspx
page in Page_Load
write this code:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Action"] != null && Request.QueryString["Action"].ToString() == "FindControl")
{
HttpContext.Current.Response.Write(ControlsList(this));
HttpContext.Current.Response.End();
}
}
public void ControlsList(Control parent)
{
string ans = "";
foreach (Control c in parent.Controls)
{
if (c is TextBox || c is Button || c is DropDownList || c is CheckBox || c is RadioButton || c is CheckBoxList || c is RadioButtonList || c is ImageButton || c is LinkButton)
{
if(c.ID != null && c.ID != "")
ans +=c.ID + "," + ((System.Reflection.MemberInfo)(c.GetType().UnderlyingSystemType)).Name + ";";
}
ans += ControlsList(c);
}
return ans;
}
In Page_Load
check Action=FindControl
then find all control with specified Type with recursive function ControlsList
and write it to response to use in Default.aspx
Its completely work for me!
How to get event control from another aspx Page. button click or Page load | ASP.NET
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();
Related Topics
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument
Cs0120: an Object Reference Is Required For the Nonstatic Field, Method, or Property 'Foo'
C# Difference Between == and Equals()
Pass List of Checkboxes into View and Pull Out Ienumerable
Convert Generic List/Enumerable to Datatable
Why Covariance and Contravariance Do Not Support Value Type
Json.Net Error Self Referencing Loop Detected For Type
Find and Extract a Number from a String
Convert a Unicode String to an Escaped Ascii String
What Is the Overhead of Creating a New Httpclient Per Call in a Webapi Client
How to Strip HTML Tags from a String in Asp.Net
How and When to Use 'Async' and 'Await'
The Calling Thread Cannot Access This Object Because a Different Thread Owns It
How to Get the Path of the Assembly the Code Is In
Set Object Property Using Reflection
A Field Initializer Cannot Reference the Nonstatic Field, Method, or Property
In C#, Should I Use String.Empty or String.Empty or "" to Intitialize a String
Nullable Types and the Ternary Operator: Why Is '? 10: Null' Forbidden