简体   繁体   English

两者都动态创建的updatepanel中的radiobuttonlist不会保留回发值

[英]radiobuttonlist in updatepanel both dynamically created don't retain value on postback

I'm dynamically creating controls on my page (aspx) using the code below, but when the asynchronous postback triggers the radio button doesn't retain it's selection and the method uptheup doesn't get called. 我正在使用下面的代码在页面(aspx)上动态创建控件,但是当异步回发触发单选按钮时,单选按钮不会保留它的选择,并且不会调用uptheup方法。 I'm guessing it's to do with the view state not returning the values, but I thought if the control ID was the same and it's created in Page_Init the in Page_Load the value should automatically be set from view state?? 我猜想这与视图状态不返回值有关,但是我认为如果控件ID是相同的,并且它是在Page_Init中创建的,则应在Page_Load中从视图状态自动设置值?

What I want is if someone selects No for the textbox to become visible. 我想要的是,如果有人选择“否”使文本框变得可见。 The control IDs are set from a database and are the same each time it loads as the code is used several times (I've replaced the IDs for easy reading below) 控件ID是从数据库中设置的,每次加载时控件ID都是相同的,因为代码使用了几次(我已在下面替换了ID以方便阅读)

the following is called from Page_Init 以下是从Page_Init调用的

RadioButtonList rbtnl = new RadioButtonList();
                        rbtnl.ID = "rbl_1";
                        rbtnl.Items.Add("Yes");
                        rbtnl.Items.Add("No");
                        rbtnl.AutoPostBack = true;
                        rbtnl.EnableViewState = true;
                        rbtnl.SelectedIndexChanged += new EventHandler(uptheup);
                        rbtnl.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
                        scriptmanager1.RegisterAsyncPostBackControl(rbtnl);

                        TextBox tbx = new TextBox();
                        tbx.ID = "tb-1";
                        tbx.CssClass = "form-control";
                        tbx.Visible = false;

                        UpdatePanel upx = new UpdatePanel();
                        upx.ID = "up-1";
                        upx.ContentTemplateContainer.Controls.Add(rbtnl);
                        upx.ContentTemplateContainer.Controls.Add(tbx);
                        upx.UpdateMode = UpdatePanelUpdateMode.Always;
                        upx.EnableViewState = true;
                        upx.ChildrenAsTriggers = true;


                        plcEvalBody.Controls.Add(upx);

OK so the code is fine and it works well, the issue was me assuming the IDs weren't the issue - they are - NEVER USE $ in control ID's. 好的,所以代码很好并且运行良好,问题是我假设ID并不是问题-它们是-绝对不要在控件ID中使用$。 I'd used this to separate two sections of the ID and it is not valid. 我用它来分隔ID的两个部分,它是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM