【发布时间】:2025-12-25 03:20:13
【问题描述】:
我正在 ASP.net 中构建一个页面,其中包含我从 xml 解析的数百个问题。 我可以通过将动态创建的单选按钮列表呈现到名为 sectionHolder 的占位符来呈现我的第一页问题。
在 javascript 验证之后,我想继续讨论下一个问题。 这就是我想要实施最佳解决方案的地方。
如果我回调我的服务器并从我的 sectionHolder 中删除所有控件,然后添加新问题,我的页面没有任何变化。这可能是因为我需要刷新我的页面。
问题是当我使用 javascript 刷新页面时:
setTimeout("location.reload(true);", 250);
我丢失了未保存在会话对象中的所有内容。 (在我的班级中声明的所有变量都会被重置) 我想知道我是否真的需要保存我想保留在该会话对象中的所有这些变量,因为我认为这会使我的代码非常混乱。 (从会话不断转换为值,...)
我知道我可以使用函数通过生成的 html 代码从 asp.net 调用我的 javascript,然后更改 div 的 innerhtml,但我想继续使用我的代码生成所有问题到占位符使用asp 的 radiobuttonlist 和 listitem 因为我所有的其他代码都是基于它的。
我真的必须将所有内容都放在我的会话中或自己生成 javascript 代码还是这里有任何其他方法?
非常感谢!
---编辑---
//这里是我提出问题的一些代码 (在开始时和每次页面重新加载时调用的函数)
protected void AddQuestions()
{
//lstsections is parsed from xml, it contains all the topics
var cal = _lstSections.ElementAt(iCurrentSection);
//limit questions to maxnumber
var iNumQuestions = (cal._lstQuestions.Count < iNumQuestionsPerPage)
? cal._lstQuestions.Count
: iNumQuestionsPerPage;
for (int a = 0; a < iNumQuestions; a++)
{
//we offset our counter to determine page
var iCurrentID = a + iCurrentQuestionStartIndex;
//q is our current question
var q = cal._lstQuestions.ElementAt(iCurrentID);
//check if we dont have too many questions on this page
var lbl = new Label() { Text = q.GetText(), ToolTip = q.GetToolTip() };
var list = new RadioButtonList()
{
ID = "RadioButtonList" + iCurrentID,
RepeatDirection = RepeatDirection.Horizontal
};
list.Attributes.Add("runat", "Server");
list.Attributes.Add("OnClick", "processText(" + (iCurrentID) + ")");
list.EnableViewState = false;
list.CssClass = "clsRadioButtonList";
sectionHolder.Controls.Add(lbl);
sectionHolder.Controls.Add(list);
foreach (var opt in q._lstOptions)
{
var item = new ListItem() { Text = opt.Text, Value = opt.Value };
list.Items.Add(item);
}
sectionHolder.Controls.Add(new LiteralControl("<br>"));
}
}
【问题讨论】:
-
确定具体是什么部分?
-
对不起,我还是不明白这个问题,我在你的代码中没有看到 Session。但正如 Mez 所说,您应该使用 AJAX 用户
UpdatePanels进行部分页面更新。
标签: c# javascript asp.net refresh