如何从类访问表单控件 [英] How to access form controls from class

查看:99
本文介绍了如何从类访问表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是面向对象编程的新手



我有一个登录页面----> WebLogin.aspx

我有一个班级----> Login.cs





例如我想在课堂上声明以下内容:

I am basically new at Object Oriented programming

and I have a login page ----> WebLogin.aspx
and I have a class ----> Login.cs


for example I want to declare the following in my class:

string username= txtUsername.Text.ToString();





如何将它声明到我的Login.cs类。



how will I declare it to my Login.cs class.

推荐答案

嘿,



我的一个朋友已经创建了一篇文章,希望这会对你有所帮助:



http://www.c-sharpcorner.com/UploadFile/2b481f/how-to -pass-data-from-one-form-to-another-form-using-class / [ ^ ] \



接受答案,如果它可以帮助你


嗯,这是一个不好的做法,你正在尝试g实现。

相反,将控制值作为参数传递给你的类的函数,这将返回一些东西。

例如,



Login.cs

Well, it's a bad practice that what you're trying to achieve.
Instead, pass the control values as a parameters to the function of your class, which would return something.
For example,

Login.cs
public string LogIn(string user, string password)
{
    string STATUS = string.empty;
    
    string query = "select * from User where UserName = '" + user + "', AND Password = '" + password + "'";
    SqlCommand cmd = new SqlCommand(query, con);
    
    int n = cmd.ExecuteNonQuery();
    
    STATUS = (n > 0 ? "Success !" : "Error");
    
    return STATUS;
}





WebLogin.aspx.cs



WebLogin.aspx.cs

{
    Login l = new Login();
    string status = l.LogIn(txtUserName.Text, txtPassword.Text);
    Response.Write(stauts);
}



这只是基本的想法,你怎么能这样做。希望这对你有所帮助。



-KR


It is just the basic idea, how you can go about this. Hope this would help you.

-KR


在登录类中创建读/写属性



Create Read/Write property in the login class as

public class Login
{
    public string UserName { get; set; }

    public void SomeMethod()
    {
        // read the username
        string username = this.UserName;
    }
}



您可以通过为Login类创建实例来访问ASPX.cs中的相同(属性)。




and you can access the same(property) in the ASPX.cs by creating an instance to the Login class.

public void button_click( object sender, EventArgs e)
   {
       Login login = new Login();
       login.UserName = "karthik";
       login.SomeMethod();
   }


这篇关于如何从类访问表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆