[英]Dropdownlist populated from datasource - values are inaccessible from code behind
[英]access TextBox And DropDownList Values from a class inside code behind
我知道我對類與繼承之間的關系缺乏基本的了解
我發現很難理解一個簡單的東西:
可以從后面的代碼訪問給定的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 Values
分配變量
怎么做呢?
我能想到的一種方法是通過參數
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);
現在您可以訪問DropDown索引和文本為
cv.DropDownIndex;
cv.TextBoxValue;
盡管我為此提供了答案,但請注意:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.