绑定到转发器中的Textbox Maxlength字段

时间:2013-02-13 19:19:29

标签: asp.net .net repeater

我遇到一个问题,MaxLength字段似乎不符合代码隐藏中设置的值。

MaxLength='<%#Convert.ToInt32(Eval("AdditionalOptionInfo.MaxFieldLength"))%>'

如果我设置MaxLength =“10”例如它似乎工作正常。

编辑:整个代码

<asp:Repeater ID="rptList" runat="server">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
            <li>
                <div class="asdf">
                    <asp:LinkButton ID="adsf" CssClass="asdf" CommandArgument='<%#Eval("OptionAvailable")%>' runat="server" OnDataBinding="lnkList_OnDataBinding" OnClientClick="contactDetailOptionClick(this); return false;">
                        <%#Eval("DisplayText")%>
                        <table class="tbox" runat="server" visible='<%#Eval("IsAdditionalInfoApplicable")%>'>
                            <tbody>
                                <tr>
                                    <td>
                                        <asp:Label runat="server" CssClass="asdf" Text='<%#Eval("AdditionalOptionInfo.InstructionText")%>' />
                                        <br />
                                        <asp:TextBox placeholder='<%#Eval("AdditionalOptionInfo.PlaceHolderText")%>' RetainValueAfterPostback="true" ellipsis="true" AutoComplete="Off" runat="server" MaxLength='<%#Convert.ToInt32(Eval("AdditionalOptionInfo.MaxFieldLength"))%>'  />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                </asp:LinkButton>
                </div>
            </li>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>

在Andrew的建议之后我的实施:

public int GetMaxLength(object additionalOptionInfo)
    {
        var option = additionalOptionInfo as TypeXYZ<string>;

        return option != null ? option.MaxFieldLength : 0;
    }

1 个答案:

答案 0 :(得分:0)

如果父表可见性为false,则仍会触发最大长度eval,因为所有这些代码都位于前端。

我会做这样的事情:

MaxLength='<%# GetMaxLength(Eval("AdditionalOptionInfo.MaxFieldLength"))%>'

public string GetMaxLength(object optionInfo)
{
     TypeOfOptionInfo option = (TypeOfOptionInfo) optionInfo;

     if (option != null)
     {
         return optionInfo.MaxFieldLength;
     }
     else
     {
           return "0";
     }
}