占位符控件集合在回发空 [英] Placeholder controls collection empty on postback

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

问题描述

我在一个ASP.NET的DataGrid以下code:

 < ASP:的TemplateField的HeaderText =可见=真>
                            <&ItemTemplate中GT;
                            <% - < ASP:文本框ID =txtAnswer文本='<%#绑定(答)%GT;' =服务器
                                       的TextMode =多行HEIGHT =76pxMAXLENGTH =2000WIDTH =377px>
                            < / ASP:文本框> - %GT;
                            < ASP:HiddenField ID =hfQuestionID值='<%#绑定(QuestionID)%>' =服务器/>
                            < ASP:HiddenField ID =hfAnswerID值='<%#绑定(AnswerID)%>' =服务器/>
                            < ASP:HiddenField ID =hfAnswer值='<%#绑定(答)%GT;' =服务器/>
                            < ASP:占位符ID =PH1=服务器/>
                            <% - <%#GetAnswerControl(DataBinder.Eval的(的Container.DataItem,QuestionID)的ToString()
                                DataBinder.Eval的(的Container.DataItem,答案)的ToString())%GT; - %GT;
                            < / ItemTemplate中>
                           <&EditItemTemplate的GT;
                            < ASP:HiddenField ID =hfQuestionID值='<%#绑定(QuestionID)%>' =服务器/>
                            < ASP:HiddenField ID =hfAnswerID值='<%#绑定(AnswerID)%>' =服务器/>
                            < ASP:HiddenField ID =hfAnswer值='<%#绑定(答)%GT;' =服务器/>
                            < ASP:占位符ID =PH1=服务器/>
                               <% - < ASP:文本框ID =txtAnswer文本='<%#绑定(答)%GT;' =服务器
                                       的TextMode =多行HEIGHT =76pxMAXLENGTH =2000WIDTH =377px>< / ASP:文本框> - %GT;
                           < / EditItemTemplate中>
                         < / ASP:的TemplateField>

占位符动态填充任何一个文本框或根据用户需要给答案的类型,一个DropDownList。在code渲染最初的回答字段(文本框或下拉)工作正常。

然而,当我火我的按钮单击事件,占位符的控制属性始终是空的。这究竟是为什么?我一直令人头大我的大脑在这个日子,并没有任何意义,我。

在那里我试图访问数据网格的动态添加控件的代码片段如下......我究竟做错了什么?

 的foreach(在gridUserSupplierTypeQuestionsAndAnswers.Rows GridViewRow R)
        //如果(IsRowModified(R))
        // {
        // gridUserSupplierTypeQuestionsAndAnswers.UpdateRow(r.RowIndex,FALSE);
        //}
        {
            占位符pH值=(预留)r.FindControl(PH1);
            控制C = ph.FindControl(答);
            串答案=的String.Empty;
            如果(c.GetType()== typeof运算(文本框))
            {
                文本框TB =(文本框)C;
                回答= tb.Text;
            }
            否则,如果(c.GetType()== typeof运算(的DropDownList))
            {
                DropDownList的DL =(DropDownList的)C;
                回答= dl.SelectedValue;
            }


解决方案

我结束了放弃占位符,而是用两个控件去 - 一个文本框和一个DropDownList - 与自己的知名度设置为false

我在数据库中存储的回答类型,并根据该值通过一个辅助函数转向相应的控制上。可能有更优雅的方式来处理这个问题,但我的时间紧,只需要得到这个工作。

I have the following code in an ASP.NET datagrid:

<asp:TemplateField HeaderText="" Visible="True" >
                            <ItemTemplate>
                            <%--<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
                                       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px"  >
                            </asp:TextBox>--%>
                            <asp:HiddenField ID="hfQuestionID" Value='<%# Bind("QuestionID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswerID" Value='<%# Bind("AnswerID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswer" Value='<%# Bind("Answer") %>' runat="server" />
                            <asp:PlaceHolder ID="ph1" runat="server" />
                            <%--<%# GetAnswerControl(DataBinder.Eval(Container.DataItem,"QuestionID").ToString(), 
                                DataBinder.Eval(Container.DataItem,"Answer").ToString()) %>--%>
                            </ItemTemplate>
                           <EditItemTemplate>
                            <asp:HiddenField ID="hfQuestionID" Value='<%# Bind("QuestionID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswerID" Value='<%# Bind("AnswerID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswer" Value='<%# Bind("Answer") %>' runat="server" />
                            <asp:PlaceHolder ID="ph1" runat="server" />
                               <%--<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
                                       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px"  ></asp:TextBox>--%>
                           </EditItemTemplate>
                         </asp:TemplateField>

The placeholder is dynamically populated with either a textbox or a dropdownlist depending on the type of answer the user needs to give. The code to render the initial answer fields (textbox or dropdown) works fine.

However, when I fire my button click event, the controls property of the placeholder is always empty. Why is this happening? I've been wracking my brain for days over this and it doesn't make any sense to me.

A snippet where I'm trying to access the datagrid's dynamically added controls follows...what am I doing wrong?

foreach (GridViewRow r in gridUserSupplierTypeQuestionsAndAnswers.Rows)
        //if (IsRowModified(r))
        //{
        //    gridUserSupplierTypeQuestionsAndAnswers.UpdateRow(r.RowIndex, false);
        //}
        {
            PlaceHolder ph =(PlaceHolder) r.FindControl("ph1");
            Control c = ph.FindControl("Answer");
            string answer = string.Empty;
            if(c.GetType() == typeof(TextBox))
            {
                TextBox tb = (TextBox) c;
                answer = tb.Text;
            }
            else if (c.GetType() == typeof(DropDownList))
            {
                DropDownList dl = (DropDownList)c;
                answer = dl.SelectedValue;
            }

解决方案

I wound up abandoning the placeholder and instead went with two controls - a textbox and a dropdownlist - with their visibility set to false.

I store the answer type in the database, and depending on that value turn the appropriate control on through a helper function. There are probably more elegant ways to handle this, but I'm tight on time and just need to get this working.

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

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