简体   繁体   English

如何在asp.net类中禁用文本框

[英]how can disable a textbox in class asp.net

 private void txtenable (Boolean txtenable)
 {
     if(txtenable== false)
     {
         txtname.Enabled = false;
         txtTel.Enabled = false;
         txtmobile.Enabled = false;
         txtAdress.Enabled = false;
      }
      else
      {
           txtname.Enabled = true;
          txtTel.Enabled = true;
          txtmobile.Enabled = true;
          txtAdress.Enabled = true;
      }

 }

I want use this class but i can not call textboxes. 我想使用此类,但我无法调用文本框。 How can call textbox in class? 如何在课堂上调用文本框?

First you need to be able to accept the TextBox objects within the class, then you can manipulate them how you see fit. 首先,您需要能够接受类中的TextBox对象,然后可以按照自己认为合适的方式对其进行操作。 I haven't actually tried this, but this is how I would go about setting it up. 我实际上没有尝试过,但这就是我要进行设置的方式。

public class YourClass
{
TextBox txtName; 
TextBox txtTel;
TextBox txtMobile;
TextBox txtAddress;
private void txtenable (Boolean txtenable, TextBox txtName, TextBox txtTel, TextBox txtMobile, TextBox txtAddress)
 {
 if(txtenable== false)
 {
     txtName.Enabled = false;
     txtTel.Enabled = false;
     txtMobile.Enabled = false;
     txtAddress.Enabled = false;
  }
  else
  {
      txtName.Enabled = true;
      txtTel.Enabled = true;
      txtMobile.Enabled = true;
      txtAddress.Enabled = true;
  }

}

In order for you to access the textboxes from within your class you will need to pass them such as: 为了使您能够从班级内访问文本框,您需要传递它们,例如:

public class OtherClassContainingTextBoxes
{
private void SomeEvent(object sender, EventArgs e){
txtenable(true, txtName, txtTel, txtMobile, txtAddress);
}

However, based on the example provided, I am unsure why you wouldn't do this in a method within the class you have your textboxes. 但是,根据提供的示例,我不确定为什么您不会在拥有文本框的类中的方法中执行此操作。

You could do something on pageload: 您可以在pageload上执行以下操作:

 protected void Page_Load(object sender, EventArgs e)
 {
    if (Session["enable"] == false){
       txtenable(false);
    }else{
      txtenable(true);
    }
 } 
private void txtenable (Boolean txtenable)
 {
     if(txtenable== false)
 {
         txtName.Enabled = false;
         txtTel.Enabled = false;
         txtMobile.Enabled = false;
         txtAddress.Enabled = false;
}
     else
     {
         txtName.Enabled = true;
         txtTel.Enabled = true;
         txtMobile.Enabled = true;
         txtAddress.Enabled = true;
      }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM