1、Label控件:標籤——呈顯出來的時候會變成span標籤
Text - 標籤上文字
<asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
BackColor-背景色
ForeColor-前景色
Font
Bold-加粗
Italic-傾斜
UnderLine-下劃線 OverLine StrikeOut
Name - 字體名
Size - 字體的大小
BorderColor-邊框顏色
BorderWidth-邊框粗細
BorderStyle - 邊框樣式
Height - 高
Width - 寬
注: .ss
{
display:inline-block;
color:Red;
background-color:Black;
height:50px;
width:500px;
}
默認狀況下<span ></span>裏面的width和height是不起做用的,但有
display:inline-block;這width和height兩個屬性就起做用了。
Enabled-是否可用
Visible-是否可見
ToolTip-鼠標指上去的提示信息
CssClass - 樣式表的class選擇器
2、Literal:也是一個標籤,這個標籤不會在兩端加上span
3、TextBox:文本框: HiddenFiled:隱藏域。Value
擁有Label全部的屬性
TextMode——文本框的呈現模式;
SingleLine--單行文本框;MultiLine-多行文本框;Password-密碼框
ReadOnly - 只讀
MaxLength - 最大輸入的字符數。只有TextMode是SingleLine和Password的時候起做用,在MultiLine的時候不起做用。
Columns:寬度,以字母個數爲單位
Rows:高度,以行數爲單位。只有TextMode是MultiLine的時候才起做用。在單行文本或多行文本下是不起做用的。
4、Button 按鈕: LinkButton 超連接按鈕 ImageButton 圖片按鈕(ImageUrl屬性)
擁有Label標籤的全部屬性
OnClientClick:當按鈕被點擊的時候,要執行的客戶端的JS代碼。它的觸發要在按鈕的C#事件代碼以前。javascript
★★★★★★★★★★★★★★★JS的調用技巧★★★★★★★★★★★★★★★★★★★
如何給文本框加JS
法一:在HTML視圖找到相關元素,直接嵌入相關的事件和JS代碼。
例如:java
<script language="javascript"> function dofocus(txt) { txt.value = ""; } </script>
設計時候:
用戶名:<asp:TextBox ID="TextBox1" onfocus="doFocus(this)" runat="server" ForeColor="#999999">(必填)</asp:TextBox>服務器
系統不會提示出來,直接寫。
運行起來:
用戶名:<input name="TextBox1" type="text" value="(必填)" id="TextBox1" onfocus="doFocus(this)" style="color:#999999;" />
法二:在aspx.cs文件的Page_Load方法中,使用Attributes屬性加入JS
例如:
C#代碼:
protected void Page_Load(object sender, EventArgs e)
{
TextBox2.Attributes.Add("onfocus","doFocus(this)");
}
運行起來:
密碼:<input name="TextBox2" type="text" value="(必填)" id="TextBox2" onfocus="doFocus(this)" style="color:#999999;" />
*******************************************************************************************佈局
5、HyperLink:超連接
擁有Label的全部屬性:
Text -
NavigateUrl - 超連接的導航地址。至關於href
Target - 打開位置
ImageUrl - 圖片超連接的圖片地址。
6、Image:圖像
擁有Label的全部屬性:
ImageUrl - 圖片超連接的圖片地址。字體
複合控件
1、下拉列表:DropDownList
擁有Label的全部的屬性:
會作三件事情:
(一)把內容填進去
法一:逐項添加
private void FillNation1()
{
//取出數據來
List<NationData> list = new NationDA().Select();
//想法扔進去
foreach (NationData data in list)
{
ListItem li = new ListItem(data.Name, data.Code);
DropDownList1.Items.Add(li);
}
}this
//取數據 List<NationData> list = new NationDA().Select(); //填上去 foreach (NationData data in list) { ListItem li = new ListItem(); li.Text = data.Name; li.Value = data.Code; DropDownList1.Items.Add(li); }
法二:數據綁定
private void FillNation2()
{
//取出數據來
List<NationData> list = new NationDA().Select();
//想法扔進去
DropDownList1.DataSource = list;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind(); //最終執行綁定填充,不要漏掉
}
技巧:如何給下拉列表加上「請選擇」的功能
1.事先在數據源上加下「請選擇」的數據項,在綁定的時候天然會綁定上。spa
寫代碼: ...
NationData da=new NationData();
da.Code="-1";
da.Name="==請選擇==";
list.Insert(0,da);設計
...
2.事先在下拉列表中設置靜態的"請選擇"列表項。而後再綁定或添加數據的時候在後面添加上。
若是採用數據綁定模式,默認狀況下會把原有的項沖掉。須要設置AppendDataBoundItems屬性。code
在屬性上手動填寫 「請選擇」 項。server
3.全部的項都綁定或填加到下拉列表後,再寫代碼加上」請選擇「的功能。
protected void Page_Load(object sender, EventArgs e) { FIllnation(); ListItem li=new ListItem("==請選擇==","-1"); DropDownList1.Items.Insert(0,li); }
(二)把選中的值取出來
每次點擊按鈕時候,都是先執行PageLoad代碼,再執行Button的Click代碼。 緣由?????
if(!IsPostBack)
{
防止每點提交頁面,都會執行這裏面的代碼。
這裏面的代碼,只有頁面初次加載的時候才被執行。點擊按鈕提交的時候,不會被執行到。
之後記着:在Page_Load事件中99%的狀況下須要寫這段判斷
}
SelectedItem
SelectedValue
SelectedIndex
//Label1.Text = DropDownList1.SelectedItem.Text + DropDownList1.SelectedItem.Value;
//Label1.Text = DropDownList1.SelectedValue;
int index = DropDownList1.SelectedIndex;
Label1.Text = DropDownList1.Items[index].Text + DropDownList1.Items[index].Value;
(三)設定某項爲選中項
給DropDownList的兩個屬性賦值:
SelectedIndex = 要選中的索引號
SelectedValue = 要選中項的值
屬性:
Items - ListItem的集合
Add()
Clear()
Insert()
Count
Remove()
RemoveAt()
DataSource
DataTextField
DataValueField
AppendDataBoundItem
SelectedIndex
SelectedItem
SelectedValue
2、RadioButtonList
擁有DropDownList全部的屬性和功能。
它呈現出來的是單選按鈕列表。
屬性:
RepeatDirection:佈局的方向
RepeatLayout:用表格佈局仍是流式佈局(table/Flow)
RepeatColumns:一行顯示幾個
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FIllnation(); ListItem li = new ListItem("==請選擇==", "-1"); RadioButtonList1.Items.Insert(0, li); RadioButtonList1.SelectedIndex = 0; } } private void FIllnation() { List<NationData> list = new NationDA().Select(); RadioButtonList1.DataSource = list; RadioButtonList1.DataTextField = "Name"; RadioButtonList1.DataValueField = "Code"; RadioButtonList1.DataBind(); } protected void Button2_Click(object sender, EventArgs e) { //取值 string lbl = RadioButtonList1.SelectedItem.Text + RadioButtonList1.SelectedValue; Label3.Text = lbl; } protected void Button3_Click(object sender, EventArgs e) { RadioButtonList1.SelectedIndex = -1; }
3、CheckBoxList
擁有RadioButton全部的屬性和功能。
呈現出來的是複選框。
技巧:
1.如何獲取選中的多個項?
//獲取複選框的選中值。
//思路:遍歷複選框列表中的每一個項,判斷每一個項的選中狀況。
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
Label2.Text += li.Value + ",";
}
}
2.如何設置某幾個項同時選中?
//設置文本框中指定的項(用 | 隔開的每一項)被選中
//思路:從文本框中解析出要選中的項的value值,而後再遍歷每一項,判斷是不是文本框中指定的,是的話就設爲選中,不是就設爲不選中。
CheckBoxList1.SelectedIndex = -1;
string s = TextBox1.Text;
string[] ss = s.Split('|'); //解析出要選中的value值
foreach (ListItem li in CheckBoxList1.Items)
{
if (ss.Contains(li.Value))
{
li.Selected = true;
continue;
}
}
private void Fillnation() { List<NationData> list = new NationDA().Select(); CheckBoxList1.DataSource = list; CheckBoxList1.DataTextField = "Name"; CheckBoxList1.DataValueField = "Code"; CheckBoxList1.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Fillnation(); ListItem li = new ListItem("--請選擇--", "-1"); CheckBoxList1.Items.Insert(0, li); CheckBoxList1.SelectedIndex = 0; } } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; //獲取複選框中的值 //思路:遍歷複選框列表中的每一項,判斷每一項的選中狀況 foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected==true) { Label1.Text += li.Value + ","; } } } protected void Button2_Click(object sender, EventArgs e) { //設置文本框中指定的項(用|隔開的每一項)被選中 //思路:從文本框中解析出要選中的項的VALUE值,而後再遍歷每一項,設置判斷是不是文本框中的指定的,是的話就設爲選中,不是的話設爲不選中 CheckBoxList1.SelectedIndex = -1;//上來先清空一把 string s = TextBox1.Text; string[] ss=s.Split('|');//解析出要選中的value值 foreach (ListItem li in CheckBoxList1.Items) { foreach (string t in ss) { if (li.Value==t) { li.Selected = true; break; } } } }
4、ListBox:列表框
擁有DropDownList控件的全部屬性。
SelectionMode - Single,Multiple
若是是單選的話,照着下拉列表來作。
若是是多選的話,照着CheckBoxList來作。
例子:下面例子雖然簡單,但必定注意:AutoPostBack 屬性要設爲true,當選定內容後自動提交服務器,這樣才能實現三級聯動。
private void Fillchina() { List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode("0001"); DropDownList1.DataSource = list; DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "AreaCode"; DropDownList1.DataBind(); } private void Fillcity() { string code = DropDownList1.SelectedValue; List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode(code); DropDownList2.DataSource = list; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); } private void Fillcounty() { string code = DropDownList2.SelectedValue; List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode(code); DropDownList3.DataSource = list; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Fillchina(); Fillcity(); Fillcounty(); } } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { Fillcounty(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Fillcity(); Fillcounty(); }