Hiddenfield WebControl

时间:2011-06-21 09:10:56

标签: c# asp.net hiddenfield

            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'türündekiesne'System.Web.UI.WebControls.HiddenField'türüneatılamadı。

我正在尝试达到hiddenfields值并将它们设置为freeTxtQues值,但暂时无法执行此操作。有什么帮助,我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

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

创建动态控件时,需要在Init事件中创建它们,因此在应用ViewState时,他会找到控件并设置它们的值。如果您没有在Init中创建控件但稍后会发现该控件没有所谓的值!