怎么用JS回显数据?

robin18 2010-11-03 01:45:53
页面的大部分控件都是用的HTMl控件,提交按钮是服务器的!点击提交按钮后,HTML控件的值都会清空,我用VIEWSTATE保存了数据,写了一段JS,发现无效。也不知道放到哪里,提交后页面再调用这段JS。现在我是方在提交按钮的客服端点击事件里,是不行的!所以有两个问题了
1,回显的JS, 我对JS不是太熟,觉得写的JS可能有问题,望能提供一些回显的例子
2。这段JS该在哪里调用?
是不是需要用到AJAX。。。需要用到的话也请给些例子,谢谢了!先给100分先!
...全文
920 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
claymore1114 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hch126163 的回复:]

如果非要用服务器事件,可以在后台用Request.Params 获取请求的参数值,把这些值拼接成一个json 格式字符串:

protected string _viewVaue= "";
_viewVaue ="{name:'asdf',age:34,sex:'男'}"; // 你自己拼接


aspx 页面:

var viewVaue = <%=_viewVaue %>;
……
[/Quote]
对啊 放到 window.onload 赋值
claymore1114 2010-11-03
  • 打赏
  • 举报
回复
前台变一下

<input name="txtInput" id="txtInput" />
<script>
document.getElementById("txtInput").value = '<%=dicClient.ContainsKey("txtInput")?dicClient["txtInput"]:""%>'
</script>

hch126163 2010-11-03
  • 打赏
  • 举报
回复
如果非要用服务器事件,可以在后台用Request.Params 获取请求的参数值,把这些值拼接成一个json 格式字符串:

protected string _viewVaue= "";
_viewVaue ="{name:'asdf',age:34,sex:'男'}"; // 你自己拼接


aspx 页面:

var viewVaue = <%=_viewVaue %>;
window.onload = function() // 回发,会重新加载页面,加载事件处理一下就可以了
{

// 设置对应控件的值
document.getElementById("txtName").value = viewVaue.name;
....
};
robin18 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hch126163 的回复:]
最好的方法是用ajax ,不让页面回发!!!!
[/Quote]
有例子吗?对AJAX也不熟悉
robin18 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 claymore1114 的回复:]
动态生成的? 贴一下代码
[/Quote]
function createTable(){

// alert(index);
var div = document.getElementById("dd");
var table = document.createElement("table");//创建table
table.setAttribute("className","tb1");
table.setAttribute("cellSpacing","1");
table.setAttribute("cellPadding","3");
table.width="100%";
var row = table.insertRow();//创建一行
row.setAttribute("className","tblrow");
var cell = row.insertCell();//创建一个单元

cell.innerHTML="<table id='TabchkBuy" + index + "' border='0'><tr><td><input id='chkBuy" + index + "_0' type='checkbox' name='chkBuy" + index + "$0' />"
+ "<label for='lblBuy" + index + "_0'>购买</label></td><td>"
+ "<input id='chkBuy" + index + "_1' type='checkbox' name='chkBuy" + index + "$1'/>"
+ "<label for='lblBuy" + index + "_1'>收藏</label></td><td>"
+ "<input id='chkBuy" + index + "_2' type='checkbox' name='chkBuy" + index + "$2'/>"
+ "<label for='lblBuy" + index + "_2'>评论或讨论</label></td></tr></table>"
+"商品编号 <textarea name='txtBuy" + index + "' rows='2' cols='202 id='txtBuy" + index + "' style='height:23px;width:150px;' ></textarea>"
+" <br/>购买时间 <input name='txtBuyTimeStart" + index + "' type='text' id='txtBuyTimeStart" + index + "' style='height:15px;width:80px;' onclick='javascript:calendar()'/> 至 "
+" <input name='txtBuyTimeEnd" + index + "' type='text' id='txtBuyTimeEnd" + index + "' style='height:15px;width:80px;' onclick='javascript:calendar()'/>";
cell = row.insertCell();//创建一个单元
cell.innerHTML="<table id='TabBuyCategory" + index + "' border='0'><tr><td><input id='chkBuyCategory" + index + "_0' type='checkbox' name='chkBuyCategory" + index + "$0' />"
+ "<label for='lblBuyCategory" + index + "_0'>购买</label></td><td>"
+ "<input id='chkBuyCategory" + index + "_1' type='checkbox' name='chkBuyCategory" + index + "$1' />"
+ "<label for='lblBuyCategory" + index + "_1'>收藏</label></td><td>"
+ "<input id='chkOutBuyCategory" + index + "_2' type='checkbox' name='chkBuyCategory" + index + "$2' />"
+ "<label for='lblBuyCategory" + index + "_2'>评论或讨论</label></td></tr></table>"
+"商品分类 <textarea name='txtBuyCategory" + index + "' rows='2' cols='202 id='txtBuyCategory" + index + "' style='height:23px;width:150px;' ></textarea>"
+" <br/>购买时间 <input name='txtBuyCategoryTimeStart" + index + "' type='text' id='txtBuyCategoryTimeStart" + index + "' style='height:15px;width:80px;' onclick='javascript:calendar()'/> 至 "
+" <input name='txtBuyCategoryTimeEnd" + index + "' type='text' id='txtBuyCategoryTimeEnd" + index + "' style='height:15px;width:80px;' onclick='javascript:calendar()'/>";
div.appendChild(table);
index++;
document.getElementById("Myhid").value=index;
// alert(document.getElementById("Myhid").value);
}
这是JS
调用 <input type="button" id="addTable" value="增加条件" onclick="createTable();"/>
hch126163 2010-11-03
  • 打赏
  • 举报
回复
最好的方法是用ajax ,不让页面回发!!!!

claymore1114 2010-11-03
  • 打赏
  • 举报
回复
动态生成的? 贴一下代码
robin18 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wwfgu00ing 的回复:]
jquery

$("#tab").find("input").each(function(){
alert($(this).val());
});

....
[/Quote]jquery 完全看不懂啊
robin18 2010-11-03
  • 打赏
  • 举报
回复
每次提交后,动态生成的控件都消失了!
robin18 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 claymore1114 的回复:]
有很多html控件的话,用字典 保存
如下:

HTML code

<input name="txtInput" value='<%= dicClient.ContainsKey("txtInput")?dicClient["txtInput"]:""%>' />
<asp:Button ID="Button1" runat="server" Text="Button" OnCli……
[/Quote]
有个问题的,我的那些HTML控件 都是动态生成的!
直接绑定不了啊
claymore1114 2010-11-03
  • 打赏
  • 举报
回复
有很多html控件的话,用字典 保存
如下:

<input name="txtInput" value='<%= dicClient.ContainsKey("txtInput")?dicClient["txtInput"]:""%>' />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


public Dictionary<string, object> dicClient = new Dictionary<string, object>();
protected void Button1_Click(object sender, EventArgs e)
{
if (!dicClient.ContainsKey("txtInput"))
dicClient.Add("txtInput", Request.Form["txtInput"]);
else
{
dicClient.Remove("txtInput");
dicClient.Add("txtInput", Request.Form["txtInput"]);
}
}
claymore1114 2010-11-03
  • 打赏
  • 举报
回复
如下:

<input name="txtInput" value='<%= txtInput%>' />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


public string txtInput;
protected void Button1_Click(object sender, EventArgs e)
{
txtInput = Request.Form["txtInput"];
}
wwfgu00ing 2010-11-03
  • 打赏
  • 举报
回复
jquery

$("#tab").find("input").each(function(){
alert($(this).val());
});

....
shinehouse 2010-11-03
  • 打赏
  • 举报
回复
能放一些代码吗,大家来分析。。。。。

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧