积极答复者
在asp.net下如何在aspx.cs后台代码里获取用客户端脚本语言改写aspx的页面控件元素?

问题
-
在asp.net下如何在aspx.cs后台代码里获取用客户端脚本语言改写aspx的页面控件元素?例如:添加一<select>控件,并标记为服务端运行,初始时有一选项,当在页面使用JavaScript增加、删除新项时,点击一Button触发后台事件,在aspx.cs下怎样才能获取到在页面的修改的元素?和页面回发再次展现时,怎样才能使刚刚新修改的元素保持在页面上啊?谢谢!期待解答!
aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebTestJSSelectDemo.aspx.cs" Inherits="CT.WebSearchFiles.WebTestJSSelectDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>select options 增加、删除操作Demo</title>
<script language="javascript" type="text/javascript">
//添加的一项
function AddOption()
{
var option = new Option("新项目");//new Option("新项目","1");
var item = document.getElementById("select1").options.length;
document.getElementById("select1").options[item] = option;
document.getElementById("select1").selectedIndex = item;
alert("添加成功!");
}
//在选中的一项后添加一项
function AddSelectedOption()
{
var option = new Option("新项目");//new Option("新项目","1");
var item = document.getElementById("select1").selectedIndex+1;
if(item != "undefined" && item != -1)
{
document.getElementById("select1").options[item] = option;
document.getElementById("select1").selectedIndex = item;
alert("添加成功!");
}
else
{
alert("请选择一项!");
}
}//删除选中的一项
function DelOption()
{
var item = document.getElementById("select1").selectedIndex;
//document.form1.select1.selectedIndex;
if(item != "undefined" && item != -1)
{
document.getElementById("select1").options[item] = null;
document.getElementById("select1").selectedIndex = item - 1;
alert("删除一项成功!");
}
else
{
alert("请选择一项!");
}
}
//删除除第一项之后的所有项
function DelAllOption()
{
var num = document.getElementById("select1").options.length;
for(var i=num ; i>= 1;i--)
{
document.getElementById("select1").options[i] = null;
}
alert("成功删除成功!");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<select id="select1" name="select1" size="10" runat="server">
<option value="0">选择项目</option>
</select>
<input type="button" onclick="AddOption()" value="添加项目" >
<input type="button" onclick="AddSelectedOption()" value="在选中后添加项目" >
<input type="button" onclick="DelOption()" value="删除项目" >
<input type="button" onclick="DelAllOption()" value="删除全部" >
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
aspx.cs后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;namespace CT.WebSearchFiles
{
public partial class WebTestJSSelectDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}protected void Button1_Click(object sender, EventArgs e)
{
//客户端添加删除的项这里看不到 须研究解决
string strCount = this.select1.Items.Count.ToString();
string[] strKeys = Request.Params.AllKeys;
//这样也只能获取到最后一个新项目,其余的获取不到
string[] strValues = Request.Params.GetValues("select1");
}
}
}- 已移动 彬慈Moderator 2009年6月18日 8:26 ([Loc]From:微软 .NET 俱乐部(INETA))
答案
-
先FindControl,比如panel或者div,然后再添加Control,比如添加一个button到这个Control里面。然后你可以使用Control的属性设置Control,也可以通过Attribute.Add方法来添加。你说的js触发后台事件不是很容易做。然后就是获取页面修改的元素,我觉得你可以将你修改的元素放到一个变量里,再返回的时候读取这个变量再复原。或者页面的控件的状态更改的时候,响应一个事件,然后执行事件去记录更改的过程。
我的博客,最近新写了一个Windows Mobile 6.5 Widget开发的文章,欢迎捧场
尽力回答每一个问题,但不代表一定正确,希望初学者能够多自己尝试。。共勉。。:)- 已标记为答案 肖小勇Moderator 2009年6月20日 5:25
-
1 首选是通过 AJAX 来做2 将 js 改变的东西存到 <input type="hidden" runat="server"/> 中, 然后通过后台访问和解析这个控件的值做相应的操作
知识改变命运,奋斗成就人生!- 已标记为答案 肖小勇Moderator 2009年6月20日 5:25
全部回复
-
先FindControl,比如panel或者div,然后再添加Control,比如添加一个button到这个Control里面。然后你可以使用Control的属性设置Control,也可以通过Attribute.Add方法来添加。你说的js触发后台事件不是很容易做。然后就是获取页面修改的元素,我觉得你可以将你修改的元素放到一个变量里,再返回的时候读取这个变量再复原。或者页面的控件的状态更改的时候,响应一个事件,然后执行事件去记录更改的过程。
我的博客,最近新写了一个Windows Mobile 6.5 Widget开发的文章,欢迎捧场
尽力回答每一个问题,但不代表一定正确,希望初学者能够多自己尝试。。共勉。。:)- 已标记为答案 肖小勇Moderator 2009年6月20日 5:25
-
1 首选是通过 AJAX 来做2 将 js 改变的东西存到 <input type="hidden" runat="server"/> 中, 然后通过后台访问和解析这个控件的值做相应的操作
知识改变命运,奋斗成就人生!- 已标记为答案 肖小勇Moderator 2009年6月20日 5:25