C# 从代码隐藏中的类访问TextBox和DropDownList值
我知道我缺乏关于类和继承之间关系的基本知识 我发现很难理解一件简单的事情: 给定的C# 从代码隐藏中的类访问TextBox和DropDownList值,c#,class,asp.net-4.0,code-behind,C#,Class,Asp.net 4.0,Code Behind,我知道我缺乏关于类和继承之间关系的基本知识 我发现很难理解一件简单的事情: 给定的DDl或TextBox可以从代码隐藏处访问 int selected = DDLID.SelectedIndex ; string userInput = TBXID.Text; 现在,从放置在代码隐藏中的类开始: public static class ControlsValue { public static int UserSel = DDLID.Selected.index; public
DDl
或TextBox
可以从代码隐藏处访问
int selected = DDLID.SelectedIndex ;
string userInput = TBXID.Text;
现在,从放置在代码隐藏中的类开始:
public static class ControlsValue
{
public static int UserSel = DDLID.Selected.index;
public static string UserText = TBXID.Text;
}
我试图“整理”我的代码,这样我就可以在其他项目中重用它
…因此,我已将与该类中的代码相关的所有全局变量移动到该类中
我不能用webControls值分配变量
怎么做
更新
我能想到的一种方法是通过参数
public static class ControlsValue
{
public static void getValues(DropDownList DDLID)
{
public static int UserSel = DDLID.Selected.index;
}
public static string UserText(TextBox TBXID)
{
return TBXID.Text;
}
}
像这样创建一个不同的类
public class ControlValues{
private int_dropDownIndex;
public int DropDownIndex{
get { return _dropDownIndex; }
set { _dropDownIndex= value; }
}
private string _textBoxValue;
public string TextBoxValue{
get { return _textBoxValue; }
set { _textBoxValue= value; }
}
public ControlValues(int dropDownIndex, string textBoxValue){
this._dropDownIndex = dropDownIndex;
this._textBoxValue = textBoxValue;
}
}
您可以从代码隐藏中创建一个实例,如下所示
ControlValues cv= new ControlValues(DDLID.Selected.index, TBXID.Text);
现在,您可以访问下拉索引和文本作为
cv.DropDownIndex;
cv.TextBoxValue;
尽管我对此给出了答案,但请注意:
- 请记住web应用程序的无状态性质以及使用它的方式李>
- 在ASP.NET中,创建一个类实例来保存服务器控件的值是低效的,因为这些控件及其值可以直接从后面的代码访问。使用这种方法将产生额外的开销
- 如果你认真学习可重用性,我强烈建议你学习面向对象编程的基础知识。一旦掌握了OOP,您将清楚地看到何时应用OOP原则李>
创建一个不同的类,如下所示
public class ControlValues{
private int_dropDownIndex;
public int DropDownIndex{
get { return _dropDownIndex; }
set { _dropDownIndex= value; }
}
private string _textBoxValue;
public string TextBoxValue{
get { return _textBoxValue; }
set { _textBoxValue= value; }
}
public ControlValues(int dropDownIndex, string textBoxValue){
this._dropDownIndex = dropDownIndex;
this._textBoxValue = textBoxValue;
}
}
您可以从代码隐藏中创建一个实例,如下所示
ControlValues cv= new ControlValues(DDLID.Selected.index, TBXID.Text);
现在,您可以访问下拉索引和文本作为
cv.DropDownIndex;
cv.TextBoxValue;
尽管我对此给出了答案,但请注意:
- 请记住web应用程序的无状态性质以及使用它的方式李>
- 在ASP.NET中,创建一个类实例来保存服务器控件的值是低效的,因为这些控件及其值可以直接从后面的代码访问。使用这种方法将产生额外的开销
- 如果你认真学习可重用性,我强烈建议你学习面向对象编程的基础知识。一旦掌握了OOP,您将清楚地看到何时应用OOP原则李>
可重用类应该没有任何依赖关系。在您的情况下,您可以创建一个类,并从类构造函数中的dropdownlist和textbox中注入值,然后像访问属性一样访问它。@Tariqulazam您能否提供一个示例,说明如何以im my code的形式从主类中的类访问它?可重用类应该没有任何依赖关系。就你而言,您可以创建一个类,并从类构造函数中的dropdownlist和textbox中注入值,然后像访问属性一样访问它。@Tariqulazam您可以提供一个示例,说明如何以im代码的形式从主类中的类中访问它吗?非常感谢您的解释,我已经从我难以同意的事情中走出来(C#)在asp.net上使用c#这是一个开始,你知道,当我在asp.net上使用c#时,我学到了我所能学到的一切,就像c#只是一个工人,离开了它的王国Winforms。我希望你知道我的意思,全局变量,它的值在每次加载时都会消失,很难理解这是为什么,理解这一切…非常感谢你的解释,我已经从我很难使用C(C)的东西转移到asp.net,这是一个开始,你知道,当我在asp.net上使用c#时,我学到了我所能学到的一切,就像c#只是离开了Winforms王国的工人之一一样。我希望你们知道我的意思,全局变量,它的值在每个负载上消失,很难理解为什么会这样,理解它的全部。。。