需要帮助来理解我收到的此错误消息(Need help in understanding this error message that I'm getting)

请帮助我理解我得到的错误是什么:

lblTabCounter是在aspx页面中编码的标签,而lblc [index]是在页面加载期间在运行时创建的标签的集合。

页面加载之外的声明:

Label[] lblc = new Label[10];

内页加载事件:

for (int i = 0; i < 10; i++) { lblc[i] = new Label() { Text = (i + 1).ToString() }; this.Controls.Add(lblc[i]); }

在另一个名为NodeChanged的事件中:

int TabCount = Convert.ToInt32(lblTabCounter.Text.ToString()); int TabIndex = Convert.ToInt32(lblTabCounterIndex.Text.ToString()); if(TabCount <= 10) { divcont.Visible = true; string tabName = getURLName(uRL); MenuItem myItem = new MenuItem(tabName, TabIndex.ToString()); Menu1.Items.AddAt(TabIndex, myItem); //f1.Attributes["src"] = url; f1.Attributes.Add("src", lblURL.Text.ToString()); MultiView1.ActiveViewIndex = TabIndex; lblc[TabCount].Text = lblTabCounter.Text; lblc[TabCount + 1].Text = lblURL.Text; TabCount++; TabIndex++; lblTabCounter.Text = TabCount.ToString(); lblTabCounterIndex.Text = TabIndex.ToString(); tvPermissions.ExpandAll(); //tvPermissions.CollapseAll(); int i = ctr; }

注意:这些都在site.master中。

Please help me understand what is this error that I'm getting:

lblTabCounter is a label coded in the aspx page while the lblc[index] is a collection of label created at runtime during page load.

Declaration outside of page load:

Label[] lblc = new Label[10];

Inside Page Load Event:

for (int i = 0; i < 10; i++) { lblc[i] = new Label() { Text = (i + 1).ToString() }; this.Controls.Add(lblc[i]); }

Inside another event called NodeChanged:

int TabCount = Convert.ToInt32(lblTabCounter.Text.ToString()); int TabIndex = Convert.ToInt32(lblTabCounterIndex.Text.ToString()); if(TabCount <= 10) { divcont.Visible = true; string tabName = getURLName(uRL); MenuItem myItem = new MenuItem(tabName, TabIndex.ToString()); Menu1.Items.AddAt(TabIndex, myItem); //f1.Attributes["src"] = url; f1.Attributes.Add("src", lblURL.Text.ToString()); MultiView1.ActiveViewIndex = TabIndex; lblc[TabCount].Text = lblTabCounter.Text; lblc[TabCount + 1].Text = lblURL.Text; TabCount++; TabIndex++; lblTabCounter.Text = TabCount.ToString(); lblTabCounterIndex.Text = TabIndex.ToString(); tvPermissions.ExpandAll(); //tvPermissions.CollapseAll(); int i = ctr; }

Note: This are all inside site.master.

最满意答案

问题是您的网页正在刷新并丢失标签的状态。

Label[] lblc = new Label[10]; protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { lblc[i] = new Label(); this.Controls.Add(lblc[i]); if (Session["lblc" + i.ToString()] == null) Session["lblc" + i.ToString()] = lblc[i].Text = (i + 1).ToString(); else lblc[i].Text = (string)Session["lblc" + i.ToString()]; }

然后,当您要设置标签时,使用以下内容(当事件未刷新页面时)

lblc[4].Text = "cool"; Session["lblc4"] = "cool";

但是,由于您的点击事件正在刷新页面,因此它失去了与lblc的联系,因此您只需设置会话,因此在刷新后您将看到新的标签。 (当事件刷新页面时)

Session["lblc4"] = "cool";

由于您的特定事件,页面正在刷新,因此标签消失但会话状态仍然如此,当您在刷新时设置会话时,代码会抓取会话而不是将其设置为默认数字。 它不是在刷新时更改标签的文本,而是实际生成带有您设置的会话字符串的新标签。

还要确保在<system.web>下的Web.config文件中有<sessionState mode="InProc" /> <system.web>请在此处阅读有关会话状态的更多信息http://msdn.microsoft.com/en-us/library/87069683 (v = VS.80)的.aspx

The problem is your web page is refreshing and losing the state of the labels.

Label[] lblc = new Label[10]; protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { lblc[i] = new Label(); this.Controls.Add(lblc[i]); if (Session["lblc" + i.ToString()] == null) Session["lblc" + i.ToString()] = lblc[i].Text = (i + 1).ToString(); else lblc[i].Text = (string)Session["lblc" + i.ToString()]; }

Then when you want to set a label you use the following (when the page is not being refreshed by the event)

lblc[4].Text = "cool"; Session["lblc4"] = "cool";

However because your click event is refreshing the page it loses contact with the lblc so you only set the Session so upon refresh you will see your new Label. (when the page is being refreshed by the event)

Session["lblc4"] = "cool";

The page is in the process of refreshing as a result of your particular event so the label disappears but the session state remains so when you set the session upon refresh the code grabs the session instead of setting it to the default number. Rather than change the text of the label when it refreshes you are actually generating the new label with the session string you set.

Also make sure you have <sessionState mode="InProc" /> in your Web.config file under <system.web> Please read more on Session States here http://msdn.microsoft.com/en-us/library/87069683(v=vs.80).aspx

更多推荐