控制回发 [英] postback of controls

查看:127
本文介绍了控制回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题。我有一个单选按钮,我可以动态生成一个文本框,

i也有一个按钮id =btn和一个id为status的标签同一页

基本上我的目标是每当检查单选按钮时,动态生成一个文本框,并且必须在id =status的标签中检索在tex框中写入的任何文本。按钮单击=btn,但我面临的问题是,每当

i点击按钮,文本框只是消失,我无法检索文本,请有人帮助我???

  protected   void  radiobutton_checked( object  sender,EventArgs e)
{
if (radiobtn1.Checked == true
{

TextBox txtbox = new TextBox();
pnl.Controls.Add(txtbox); // (pnl是放置文本框的面板)//
}



  protected   void  Upd_Click( object  sender,EventArgs e)
{
if (txtbox.Text!= string .empty)
{

status.Text = 文本框中的文字是 + txtbox.Text;



}

解决方案

恢复ASP.NET应用程序中动态创建控件的状态 [ ^ ]

Quote:

在运行时动态地向ASP.NET页面添加控件时,对象引用会在回发时丢失,因为它们在代码隐藏中没有句柄。在回发后访问这些对象时,用户输入的值不可用,并且它们看起来是空的。本文介绍如何使用ViewState重新创建并将回发值重新插入到对象中以访问它们的值。


我认为Tadit Dash已发现您的大规模问题,但我还想指出你的代码存在缺陷:事实上,你的代码不应该首先构建,你应该收到如下错误消息:



当前上下文中不存在名称'文本框'



名称'文本框仅存在于'radioButton_Checked EventHandler的范围内。



是的,您的代码确实创建了一个新的TextBox,并且TextBox被添加到Panel的ControlsCollection中;所以,TextBox存在,但你没有有效的引用来访问它...除非你潜入Panel的ControlsCollection。



By顺便说一句,我建议您使用Button而不是RadioButton来处理这种类型的UI任务。 RadioButton旨在与其他RadioButtons结合使用,让用户有机会从一组选项中选择一个选项



如果你想允许用户打开/关闭能够创建新的TextBox,你可以考虑使用CheckBox:当它被选中时,启用了创建TextBoxes的Button,并在取消选中时禁用它。



另请注意,如果您没有设置您创建的TextBox的位置,它们可能只是简单地放在彼此之上。


< blockquote>尝试此链接,希望有人帮助你。点击

ASP.NET中动态创建的控件 [ ^ ] <无线电通信/>
如何在ASP.NET中动态创建控件并从中检索值 [ ^ ]


i am getting a problem.i have a radio button on whose click i am generating a textbox dynamically,
i also have a button id="btn" and a label with id "status" on the same page
basically my objective is that whenever the radiobutton is checked,a textbox is generated dynamically,and whatsoever text is written in the tex box must be retrieved in the label with id="status" on the button click="btn",but the problem i am facing is that whenever
i click the button,the text box simply dissappears and i cant retrieve the text,please can someone help me???

protected void radiobutton_checked(object sender, EventArgs e)
  {
 if (radiobtn1.Checked == true)
    {
          
          TextBox txtbox = new TextBox();
          pnl.Controls.Add(txtbox);  //(pnl is a panel in whch i m placing the textbox)//
}


protected void Upd_Click(object sender, EventArgs e)
  {
      if (txtbox.Text != string.empty)
      {
          
          status.Text ="text in textbox was"+txtbox.Text;



      }

解决方案

Retaining State for Dynamically Created Controls in ASP.NET applications[^]

Quote:

When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values.


I think Tadit Dash has found your larger-scale problem, but I also would like to point out that your code is flawed: in fact, your code should not build in the first place, and you should get an error message like:

"The name 'textbox' does not exist in the current context"

The name 'textbox exists only in the scope of the 'radioButton_Checked EventHandler.

Yes, your code does create a new TextBox, and that TextBox is added to the ControlsCollection of a Panel; so, that TextBox exists, but you have no valid reference to it to use to access it ... unless you go diving into the ControlsCollection of the Panel.

By the way, I'd like to recommend you use a Button, not a RadioButton, for this type of UI task. A RadioButton is meant to be used in conjunction with other RadioButtons to give the user a chance to select one option from a set of options.

If you want to allow the user to turn-on/off being able to create new TextBoxes, you might consider having a CheckBox: when it's checked the Button that creates TextBoxes is enabled, and you disable it when its unchecked.

Also note that if you do not set the position of the TextBoxes you create, they may simply be placed exactly on top of each other.


try this link, I hope some help you.click
Dynamically Created Controls in ASP.NET[^]
How to create controls dynamically in ASP.NET and retrieve values from it[^]


这篇关于控制回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆