一,在后置代码中动态生成JavaScript代码的函数:
以前在后缀代码中生成javascript代码一直是使用Response.Write来生成,今天看到两个函数,可以让客户端生成Javascript代码,一个是:RegisterClientScriptBlock,另一个是:RegisterStartupScript,两者不同之处在于生成的位置不同,他们都在<Form>与</Form>块之间,但第一个在<Form>之后,第二个是在</Form>之前。两个函数可以配合IsClientScriptBlockRegistered与IsStartupScriptRegistered函数来查看代码块有没有被注册使用。例:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Page.RegisterStartupScript("Setfirstfocus",js);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
二,关于ViewState的一些知识点:
可以详见 http://www.microsoft.com/china/msdn/archives/library/dnaspnet/html/Asp11222001.asp
三,在客户端获得按键的javascript代码:
<
script language
=
"
javascript
"
>
function keyDown()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var keycode=event.keyCode;
var keychar=String.fromCharCode(keycode);
alert('ASCII='+keycode+'\nKeyChar='+keychar);
}
document.onkeydown
=
keyDown;
</ script >
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ script >
四,用Enter替代Table健的Javascript代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
五,动态创建控件:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LiteralControl可以根据字符串来生成页面的html代码
六,动态更改Web控件的属性:
可以像给控件添加script事件一样为其添加样式属性,如:
Button1.Attributes.Add("style","BACKGROUND-COLOR: #008888");
七、在页面的Javscript代码中清加.net代码
如,aspx页面有以下控件
<asp:Literal ID="Literal1" Text="abc" runat="server"></asp:Literal>
我想在下面的JS代码中获得Literal1.Text文字,通过以下方式获得:
<script language="javascript">
alert("<%=Literal1.Text %>");
</script>