隐藏字段 Web 控件

发布于 2024-11-16 07:47:32 字数 2390 浏览 2 评论 0原文

            foreach (var item in AnketSoru)
        {
            r = new HtmlTableRow();
            c = new HtmlTableCell();
            c.InnerHtml = item.new_question_text.ToString();
            r.Cells.Add(c);

            switch (item.new_question_type.ToString())
            {
                case "2": //FreeText
                    c = new HtmlTableCell();
                    TxtFreeText = new TextBox();
                    TxtFreeText.ID = "Txt_" + item.new_survey_questionid.ToString();
                    TxtFreeText.TextMode = TextBoxMode.MultiLine;
                    TxtFreeText.Width = 300;
                    TxtFreeText.Height = 50;
                    TxtFreeText.EnableViewState = true;
                    c.Controls.Add(TxtFreeText);

                    HiddenField txthfield = new HiddenField();
                    txthfield.Value = item.new_name.ToString();
                    c.Controls.Add(txthfield);

                foreach (Control c in plc.Controls)
            {
                System.Web.UI.HtmlControls.HtmlTable Survey_Inner = (System.Web.UI.HtmlControls.HtmlTable)c.FindControl("Survey_Inner");

                foreach (System.Web.UI.HtmlControls.HtmlTableRow r in Survey_Inner.Rows)
                {
                    foreach (Control ctr in r.Cells)
                    {
                        foreach (Control ct in ctr.Controls)
                        {
                            if (ct.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                            {

                                string freeTxtQues = ?? ;

                                string TextCevap = ((System.Web.UI.WebControls.TextBox)ct).Text;

                                string deger = ct.ID.ToString();



                                Guid QuestionId = new Guid(deger.Substring(4));

                                SaveAnswers(this._PortalUserHelper.UserProxy.ContactId, EgitimKatilimcisi, QuestionId, TextCevap, freeTxtQues);

                            }

尝试过

string freeTxtQues = ((System.Web.UI.WebControls.HiddenField)ct).Value;

但返回错误。 “用户代码未处理 InvalidCastException。”

“System.Web.UI.WebControls.TextBox”是其他“System.Web.UI.WebControls.HiddenField”。

我正在尝试达到隐藏字段值并将它们设置为 freeTxtQues 值,但目前无法做到这一点。有什么帮助我该怎么做吗?

            foreach (var item in AnketSoru)
        {
            r = new HtmlTableRow();
            c = new HtmlTableCell();
            c.InnerHtml = item.new_question_text.ToString();
            r.Cells.Add(c);

            switch (item.new_question_type.ToString())
            {
                case "2": //FreeText
                    c = new HtmlTableCell();
                    TxtFreeText = new TextBox();
                    TxtFreeText.ID = "Txt_" + item.new_survey_questionid.ToString();
                    TxtFreeText.TextMode = TextBoxMode.MultiLine;
                    TxtFreeText.Width = 300;
                    TxtFreeText.Height = 50;
                    TxtFreeText.EnableViewState = true;
                    c.Controls.Add(TxtFreeText);

                    HiddenField txthfield = new HiddenField();
                    txthfield.Value = item.new_name.ToString();
                    c.Controls.Add(txthfield);

and

                foreach (Control c in plc.Controls)
            {
                System.Web.UI.HtmlControls.HtmlTable Survey_Inner = (System.Web.UI.HtmlControls.HtmlTable)c.FindControl("Survey_Inner");

                foreach (System.Web.UI.HtmlControls.HtmlTableRow r in Survey_Inner.Rows)
                {
                    foreach (Control ctr in r.Cells)
                    {
                        foreach (Control ct in ctr.Controls)
                        {
                            if (ct.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                            {

                                string freeTxtQues = ?? ;

                                string TextCevap = ((System.Web.UI.WebControls.TextBox)ct).Text;

                                string deger = ct.ID.ToString();



                                Guid QuestionId = new Guid(deger.Substring(4));

                                SaveAnswers(this._PortalUserHelper.UserProxy.ContactId, EgitimKatilimcisi, QuestionId, TextCevap, freeTxtQues);

                            }

i tryed

string freeTxtQues = ((System.Web.UI.WebControls.HiddenField)ct).Value;

but returns me error. "InvalidCastException was unhandled by user code."

'System.Web.UI.WebControls.TextBox' türündeki nesne 'System.Web.UI.WebControls.HiddenField' türüne atılamadı.

I'm trying to reach hiddenfields value's and set them to the freeTxtQues value but couldn't able to do it for now. Any help for how can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2024-11-23 07:47:32

很难理解你的问题,但我会抛出一些东西......

当你创建动态控件时,你需要在 Init 事件上创建它们,这样当应用 ViewState 时,他会找到控件并设置它们的值。如果你不是在Init中创建控件而是稍后创建控件,你会发现该控件没有假设的值!

Hard to understand your question/problem but I will throw something...

When you create dynamic controls you need to create them on Init event so when ViewState is applied he finds the controls and sets their values. If you are not creating the controls in Init but later, you will found out that the control doesn't have the supposed value!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文