How can i access user control textfields in another class - c#

i have a user control class named EmployeeInformation.aspx.cs with a Textfield txtRegistrationNo
public partial class EmployeeInformation : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string TextRegistrationNo
{
get
{
return txtRegistrationNo.Text;
}
}
}
Now in another class named SeparationInfo.aspx.cs how can i access txtRegistrationNo field?

You have to access it through object of your custom control, suppose the instance of EmployeeInformation is EmployeeInformation1
string TextRegistrationNo = EmployeeInformation1.TextRegistrationNo;

EmployeeInformation objEmployeeInfo = new EmployeeInformation();
string temp = objEmployeeInfo.TextRegistrationNo;

Related

Accessing a variable from a different code-behind class

EDIT: made variables not-static.
I have a variable in code-behind class and want to access it from another.
Here's the C# code for the "Signup" page:
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
Response.Redirect("Login.aspx");
}
}
}
And here's the code for another code-behind class in an .aspx class "Login":
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
if (loguser == Webform_signup.User)
{
Response.Redirect("Start");
}
}
}
}
The problem is that when I try to access Webform_signup.user, it displays an error saying:
The name Webform_Signup does not exist in the current context.
Anyone has an idea of how to fix it?
The dirt simplest way (and probably not the best way) is to just stick it in a session variable and read it from there instead.
public partial class Webform_Signup : System.Web.UI.Page
{
private string user;
public string User
{
get { return user; }
set { user = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["submit"] != null)
{
user = HttpContext.Current.Request["user"];
HttpContext.Current.Session["user"] = user; //Save to session
Response.Redirect("Login.aspx");
}
}
}
public partial class Webform_Login : System.Web.UI.Page
{
public string loguser = "";
protected void Page_Load(object sender, EventArgs e)
{
if(HttpContext.Current.Request["logsubmit"] != null)
{
loguser = HttpContext.Current.Request["loguser"];
var user = HttpContext.Current.Session["user"]; //Read from session
if (loguser == user)
{
Response.Redirect("Start");
}
}
}
}
This is assuming of course that you have session state enabled.
You need to keep in mind lifecycles. You're ignoring them, and the results would be disastrous in this case.
Any time you declare a variable as static, there will only be one instance of that variable in your application. That means no matter what user is using your app, the Webform_Signup.User field will be shared. That could result in users accessing other users' information! That's really, really bad.
Also, page classes should not be referring to other page classes. A page only exists when a user is accessing that page.
If you want to login a user, then perhaps you should research Forms Authentication. Or better yet, learn a modern framework like ASP.NET Core MVC instead of Web Forms, which is a deadend platform.

Modified UserControl

I have following control class withsome extra methods in it. I have multiple form usercontrols, FormChoosePage has dropdownlist with form list in it, and I get selected forms user control, load its data and after he click save button I call forms save method.
at btnSave_Click ucForm is null, I have 2 question;
1 ) how can I keep dynamicly Modified UserControl (View State or something?)
2 ) am I on right way to do this with generic user controls and everything. If not, what is you sugestion?
FormControler
public class Controler : UserControl
{
public virtual void PageLoad() { }
public virtual void SaveForm() { }
}
Form UC
public partial class ApplicationForm : Controls.Controler
{
public override void PageLoad()
{
ddlFormType.DataSource = [Data];
ddlFormType.DataBind();
}
public override void SaveForm()
{
XForm form = new XForm();
form.something = txtSomething.Text;
form.Save();
}
}
Form Choose Page
public partial class FormChoosePage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlFormLoad();
}
}
Controler ucForm;
protected void ddlForm_SelectedIndexChanged(object sender, EventArgs e)
{
XForm form = XForm.Get<XForm>(ddlForm.SelectedValue);
ucForm = this.LoadControl(form.URL) as Controler;
ucForm.ID = "ucForm";
ucForm.PageLoad();
}
protected void btnSave_Click(object sender, EventArgs e)
{
ucForm.SaveForm();
}
}

Refresh or add ascx control from another ascx control

I am working on an asp .net project and i have a main ascx control which is divided by two RadSpliters. On load of the main.ascx the app is loading two other controls, the control1.ascx and control2.ascx. In the control1 i have a treeview and on selected node of the treeview i want to reload the control2.ascx. Is there a way to do this. Below i am pasting the code that i am using to do that but is not working. Any help or suggestions please?
public partial class Control1: System.Web.UI.UserControl
{
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
}
public interface IReload{
public void reload();
}
public partial class Control2: System.Web.UI.UserControl, IReload
{
}
public partial class Control1: System.Web.UI.UserControl
{
IReload _r;
public IReload setReload
{
set { _r = value; }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
if(_r != null){
_r.reload();
}
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
c.setReload(codeEditor);
}

C# : modify form control from a class

I have Form and Class like that :
namespace ALTER_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.ALTER();
}
}
public class ALTER
{
public Form1 F;
public void ALTER()
{
F.TextBox1.Text="I Altered That";
}
}
}
So i try to call ALTER() to change the textbox1 value in Form1 but i get that error :
object reference not set to an instance of an object
That happens only if i am accessing or modifying the Form1 Controls.
And by the way i set textbox1 modifier to public
So , finally i`d like to change the control value without getting that error.
You need to assign the reference to the form. Like this:
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.F = this;
A.ALTER();
}
Why does your ALTER class (which isn't a great class name either) have to know about your form?
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
this.TextBox1.Text = A.ALTER();
}
}
[...]
public class ALTER
{
public String ALTER()
{
// Do your thing
return "I Altered That";
}
}
Use these lines of code:
ALTER A = new ALTER();
A.F = this ;
A.ALTER();

Passing a variable into User Control

I want to pass one of the textbox's(on the master page) value into the user control(.ascx) page. Here is my code shows how to open user control..
Control usrCnt= LoadControl("userControl.ascx");
usrCnt.ID = "usrCnt";
ASPxPanel1.Visible = true;
ASPxPanel1.Controls.Clear();
ASPxPanel1.Controls.Add(userCnt);
How can post the textbox's value to the user control? I can't do like this..
Control usrCnt= LoadControl("userControl.ascx?param=" + textbox.Text);
Create a method for your usercontrol like SetText
and then
usrCnt.SetText("textValue");
if it is your webusercontrol code behind
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string theText)
{
this.Label1.Text = theText;
}
}
and if you've been added the control to the page
in page call it as
this.WebUserControl11.SetText(TextBox1.Text);
put these on the upper part of your usercontrol
private string _TextBoxValue = string.Empty;
public string TextBoxValue {
get { return _TextBoxValue; }
set { _TextBoxValue = value; }
}
then on your masterpage
usrCnt.TextBoxValue = TextBox1.Text;
For the quickest and dirty way is on your MasterPage
ViewState["TextBoxValue"] = TextBox1.Text();
and on UserControl, access ViewState["TextBoxValue"] to get the value.

Categories

Resources