简体   繁体   English

asp.net Dropdownlist Customvalidator servervalidate引用控件

[英]asp.net Dropdownlist Customvalidator servervalidate referencing control

I have a dropdownlist that I populate from the DB, and depending on business logic I need to be able to validate the selected item (TEXT) from the dropdownlist with server side vaildation. 我有一个从数据库填充的下拉列表,根据业务逻辑,我需要能够使用服务器端验证从该下拉列表中验证所选的项目(TEXT)。 Requirements state I cannot simply filter it out as part of the SQL statement. 需求状态我不能简单地将其作为SQL语句的一部分过滤掉。 The solution I have been trying to get to work is to simply create a customvalidation in the code behind. 我一直尝试使用的解决方案是在后面的代码中简单地创建一个customvalidation。

The validation is called, BUT I cannot figure out how to reference the ddl DataTextField value for the item selected. 称为验证,但我无法弄清楚如何为所选项目引用ddl DataTextField值。 When I try and do the server side code below the asp.net system states that my dropdownlist does not exist within the detailsview and provides a red underline as a result. 当我尝试执行asp.net系统下方的服务器端代码时,表明我的dropdownlist在detailsview中不存在,因此提供红色下划线。 In this instance it will always be insertmode. 在这种情况下,它将始终是insertmode。

Suggestions 建议


ASP Code ASP代码


<asp:DetailsView ID="dtlSample" runat="server" AutoGenerateEditButton="true" AutoGenerateRows="false">
      <Fields>

. . .

        <asp:TemplateField HeaderText="Position">
          <ItemTemplate>
            <%# Eval("Age") %>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:DropDownList ID="ddlPosition" runat="server" AutoPostBack="True" 
                LDataSource="Select Position, PositionId from ...." DataTextField="Position" DataValueField="PositionId"
                 ></asp:DropDownList>
          </EditItemTemplate>
          <InsertItemTemplate>
            <asp:DropDownList ID="ddlPosition" runat="server" AutoPostBack="True" 
                LDataSource="Select Position, PositionId from ...." DataTextField="Position" DataValueField="PositionId"
                 ></asp:DropDownList>
          </InsertItemTemplate>
    <asp:CustomValidator ID="cvPos" Display="Dynamic" ControlToValidate = "DDLPosition"
          OnServerValidate="ddlPos_Check" runat="server" ForeColor="Red" ErrorMessage="My error message"></asp:CustomValidator>
        </asp:TemplateField>
      </Fields>

CODE BEHIND 后面的代码

protected void ddlPos_Check(object sender, ServerValidateEventArgs args)

{ {

    if (ddPosition.SelectedItem.Text.Contains("some value")            
        args.IsValid = false;         
    else           
        args.IsValid = true;    

} }

Murphy's law, answer your own question a few hours after. 墨菲定律,几个小时后回答您自己的问题。

        DropDownList ddlList=DetailsView2.FindControl("ddlPosition") as DropDownList;

        if (ddlList != null)
        {

            if (ddlList.SelectedItem.Text.Contains("text")) {
                        args.IsValid = false;    
            }
             else
            {
                   args.IsValid = true;
            }

        }    

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

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