web 控件用法

asp:CheckBox:
<asp:CheckBox id="MyControl" name="MyControl" Text="MyControl" runat="server"/>

asp:RadioButton:
<asp:RadioButton id="MyControl" Text="My RadioButton" GroupName="aaa" runat="server"/>
注:属性GroupName与htmlinputRadioButton控件的name属性等同!!

asp:HyperLink:
<asp:HyperLink id="MyControl" Text="My HyperLink" NavigateUrl="asp_hyperlink.aspx"

runat="server"/>
注:1 此控件的NavigateUrl和Target属性的区别???
2 另可以添加name属性例:
<asp:HyperLink id="MyControl" Text="Paragraph One" name="para1" runat="server"/>
接着在该页面其它部位利用下面html元素到达Paragraph:
<a href="thepage.aspx#para1">Go to Paragraph</a>
实现创建一个"锚".
3 使用Attributes属性:

进行设置和修改:
MyAnchor.Attributes("name") = strNewName ;'
访问:
strNewName = MyAnchor.Attributes("name");
因此,即使没有Name属性也可以用此法添上一个name属性.当想要为一个元素添加非标准的属性时,这种方

法很有用.
4 还可以应用ImageUrl属性实现使用图像作为超连接.

asp:LinkButton:
<asp:LinkButton id="MyControl" Text="My LinkButton" runat="server"/>
注:默认下将使用AutoPostBack功能,点击后表单将会被返回到生成Click事件的服务器中.(估计这就是命

名为LinkButton的原因吧!类似于button)
此外,不能选择使用图像而只能使用文本.

asp:Image:(用于需要在页面中显示图像,并且在服务端代码中对该控件进行访问)
<asp:Image id="MyControl" ImageUrl="bokk.gif" runat="server"/>

asp:Panel:(创建格式化<div>元素的一种途径)
<asp:Panel id="mycontrol" Text="aaa" runat="server">
some text inside the Panel control!
</asp:Panel>
注:对于任何服务器控件,可以使用Style集合添加自己额外的样式属性:
MyControl.Style("slector-name")="value";

asp:Label:
<asp:Label id="MyControl" Text="My label" runat="server"/>
注:此控件能客服<asp:Panel>控件自动换行的问题,能将其中的内容与其它元素和文本排列在一行中!

asp:Button:
<asp:Button id="aa" Text="value" runat="server"/>

asp:ImageButton:
<asp:ImageButton id="s" ImageUrl="bool.gif" runat="server"/>

asp:TextBox:
<asp:TextBox id="aa" Text="ss" runat="Server"/>
注:使用TextMode属性可以设置单行,多行和密码模式

asp:Table:
<asp:Table id="aa" runat="server"/>
注:(附服务端处理程序)
int intRows = Convert.ToInt32(selRows.Value);
int intCols = Convert.ToInt32(selCols.Value);
int intRowCount;
int intColCount;
TableRow objRow;
TableCell objCell;

for (intRowCount = 0; intRowCount <= intRows - 1; intRowCount++)
{
objRow = new TableRow();

for (intColCount = 0; intColCount <= intCols - 1; ++intColCount)
{
objCell = new TableCell();
objCell.Controls.Add(new LiteralControl("R" + intRowCount.ToString() + "C" +

intColCount.ToString()));
objRow.Cells.Add(objCell);
}
MyControl.Rows.Add(objRow);
}
//此处asp:Table与htmlTable的区别是:
asp:Table:
objCell.Controls.Add(new LiteralControl("R" + intRowCount.ToString() + "C" +

intColCount.ToString()));
而htmlTable:
objCell.InnerHtml = "R"+intRowCount+"c"+intColCount;
原因在于htmlTable控件的objCell是HtmlTableCell对象,而asp:Table中objCell则是TableCell对象

asp:Literal:(构着函数:new LiteralControl("String");)
<asp:Literal Text="Some" runat="server"/>
注:此控件只有一个Text属性,它不会产生其他的输出(例如不会生成html元素)因此当所需的工作是将一

些文本放到页面中的时候很有用.
例:
<asp:Literal Text="<gloop>A custom element</gloop>" runat="server"/>
产生输出:
<gloop>A custom element</gloop>

asp:PlaceHolder:
<asp:PlaceHolder id="mycontrol" runat="server"/>
<script runat="server">
private void Page_Load()
{
int intLoop;
TextBox obj;
for(intLoop=0;intLoop<3;intLoop++)
{
obj = new TextBox();
mycontrol.Controls.Add(obj);
}

}
</script>
注:如果希望在页面中动态的创建控件,而不是在页面源代码中对控件进行显示声明时可以用此控件.

下面讨论事件相应:(click事件和change事件)
<%@Page Language="C#" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- #include file="style.inc" -->
</head>
<body>
<!--------------------------------------------------------------------------->
<span class="heading">The ASP Web Forms Control Events</span><p />

<form runat="server">
<table border="0" cellpadding="10"><tr><td nowrap="nowrap" colspan="2">

<asp:TextBox id="MyText" Text="OriginalValue" runat="server"
OnTextChanged="MyChangeCode" AutoPostBack="True" />
//事件:OnTextChanged="MyChangeCode"
<span class="cite">Press 'Tab' to submit Textbox value automatically</span>

</td></tr><tr><td nowrap="nowrap">

<asp:CheckBox id="MyCheckbox" Text="My CheckBox" runat="server"
OnCheckedChanged="MyChangeCode" AutoPostBack="True" />
//事件:OnCheckedChanged="MyChangeCode"
</td><td nowrap="nowrap">

<asp:DropDownList id="MyDropDownList" runat="server"
OnSelectedIndexChanged="MyListChangeCode" AutoPostBack="true">
//事件:OnSelectedIndexChanged="MyListChangeCode"
<asp:ListItem Text="Option 1" Value="Value 1" />
<asp:ListItem Text="Option 2" Value="Value 2" />
<asp:ListItem Text="Option 3" Value="Value 3" />
</asp:DropDownList>

</td></tr><tr><td nowrap="nowrap">

<asp:RadioButtonList id="MyRadioList" runat="server"
OnSelectedIndexChanged="MyListChangeCode" AutoPostBack="true">
//事件:OnSelectedIndexChanged="MyListChangeCode"
<asp:ListItem Text="Option 1" Value="Value 1" />
<asp:ListItem Text="Option 2" Value="Value 2" />
<asp:ListItem Text="Option 3" Value="Value 3" />
</asp:RadioButtonList>

</td><td nowrap="nowrap">

<asp:ImageButton id="MyImageButton" ImageUrl="ClickmeButton.gif" runat="server"
ImageAlign="absbottom" OnClick="MyImageCode" /><p />
//事件:OnClick="MyImageCode"
<asp:Button id="MyButton" Text="Submit" runat="server"
OnClick="MyClickCode" />
//事件:OnClick="MyClickCode"
</table>
</form>

<div id="divResult" runat="server" EnableViewState="False" />

<script runat="server">
//事件处理函数:
void MyChangeCode(Object objSender, EventArgs objArgs)
{
Control c = (Control)objSender;
divResult.InnerHtml += "Change event detected for control '"
+ c.ID + "'<br />";
}

void MyListChangeCode(Object objSender, EventArgs objArgs)
{
ListControl c = (ListControl)objSender;
divResult.InnerHtml += "Change event detected for control '"
+ c.ID + "'<br />SelectedIndex is '"
+ c.SelectedIndex
+ "', selected item text is '"
+ c.SelectedItem.Text
+ "', selected item value is '"
+ c.SelectedItem.Value + "'<br />";
}

void MyClickCode(Object objSender, EventArgs objArgs)
{
Button c = (Button)objSender;
divResult.InnerHtml += "Click event detected for control '"
+ c.ID + "'<br />";
}

void MyImageCode(Object objSender, ImageClickEventArgs objArgs)
{
ImageButton c = (ImageButton)objSender;
divResult.InnerHtml += "Click event detected in control '"
+ c.ID + "' at X=" + objArgs.X
+ " Y=" + objArgs.Y + "<br />";
}

</script>
接着是命令控件:
Button,ImageButton,LinkButton有属性CommandName和CommandArgument,当按钮被点击后,在服务端生成

一个可被响应的Command事件,例:
<asp:Button id="MyButton" OnCommand="MyCommandCode"
Text="Run Command" runat="server"
ToolTip="This is a Button control" />

<asp:ImageButton id="MyImageButton" OnCommand="MyCommandCode"
ImageUrl="ClickmeButton.gif" runat="server" ImageAlign="absbottom"
ToolTip="This is an ImageButton control" />

<asp:LinkButton id="MyLinkButton" OnCommand="MyCommandCode"
Text="Run Command" runat="server"
ToolTip="This is a LinkButton control" />
//事件处理函数:
void MyCommandCode(Object objSender, CommandEventArgs objArgs)
{
Control c = (Control)objSender;
divResult.InnerHtml += "Command event detected for control '"
+ c.ID + "'<br />"
+ "CommandName is '"
+ objArgs.CommandName
+ "', CommandArgument is '"
+ objArgs.CommandArgument + "'";
}
优点:
1 当一个表单上有多个按钮控件时可以利用它来检测是单击哪个按钮来提交表单.
以前版本中是通过对Request.Form集合进行查询(若表单的method设置为get那么就需要对

Request.QueryString检查)以确定单击了哪个按钮.
2 对复杂的控件如datagrid进行处理时通过对CommandName属性设置预先定义好的值,那么当在datagrid

控件中对数据进行编辑的时候就可以利用这些按钮来完成专有控件的任务.

你可能感兴趣的:(C++,c,Web,C#,asp)