未在RequiredFieldValidator DDL 中设置InitialValue

发布于 2024-11-07 14:07:04 字数 2148 浏览 3 评论 0原文

由于某种原因,下拉列表的初始值未设置。它总是加载一些随机值。当我查看标记时,我可以看到所有列表项,包括值为 -1 的列表项。我还尝试清除浏览器缓存并显式将代码中的 SelectedIndex/Value 设置为 0/"-1",但似乎无法弄清楚发生了什么。有什么想法吗?

我是这样做的:

<asp:DropDownList ID="ddlGender" runat="server" CssClass="select_Box" OnPreRender="LoadGenders">
                            </asp:DropDownList>
                            <div class="error"> 
                                <asp:RequiredFieldValidator                                     
                                    ID="RequiredFieldValidatorGender"
                                    Runat="server"
                                    Enabled="true"
                                    InitialValue="-1"
                                    ControlToValidate="ddlGender"
                                    SetFocusOnError="true"                                        
                                    Display="Dynamic">Gender Required</asp:RequiredFieldValidator>
                            </div>

生成 HTML:

<select name="pagecolumns_0$pagecontent_1$contentleftcol_0$ctl00$ddlGender" id="pagecolumns_0_pagecontent_1_contentleftcol_0_ctl00_ddlGender" class="select_Box">
<option value="-1">--Select Gender--</option>
<option selected="selected" value="M">Male</option>
<option value="F">Female</option></select>

如您所见,它随机选择 Male。另外,我注意到,上次填写表格时,我在点击提交之前将其设置为“男性”。可能是它缓存了选择?

这是.cs:

protected void LoadGenders(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            ((DropDownList)sender).Items.AddRange(Constants.GenderWithSelect);
        }
public static ListItem[] GenderWithSelect = (new[] { new ListItem("--Select Gender--", "-1") }).Concat(Gender).ToArray();
 public static ListItem[] Gender = new[]
                                              {
                                                  (new ListItem("Male","M")),
                                                  (new ListItem("Female","F"))
                                              };


For some reason the dropdownlist's initial value doesnt get set. It always loads some random value instead. When I look into the markup I can see all the list items including the one with -1 value. I also tried clearing browser cache and explicitly setting the SelectedIndex/Value in code to 0/"-1", but cant seem to figure out what is going on. Any idea?

Here is how I am doing it:

<asp:DropDownList ID="ddlGender" runat="server" CssClass="select_Box" OnPreRender="LoadGenders">
                            </asp:DropDownList>
                            <div class="error"> 
                                <asp:RequiredFieldValidator                                     
                                    ID="RequiredFieldValidatorGender"
                                    Runat="server"
                                    Enabled="true"
                                    InitialValue="-1"
                                    ControlToValidate="ddlGender"
                                    SetFocusOnError="true"                                        
                                    Display="Dynamic">Gender Required</asp:RequiredFieldValidator>
                            </div>

Generated HTML:

<select name="pagecolumns_0$pagecontent_1$contentleftcol_0$ctl00$ddlGender" id="pagecolumns_0_pagecontent_1_contentleftcol_0_ctl00_ddlGender" class="select_Box">
<option value="-1">--Select Gender--</option>
<option selected="selected" value="M">Male</option>
<option value="F">Female</option></select>

as you can see, it is randomly selecting Male. Alsow, I notice that, last time I fill the form I set it to Male before hitting submit. May be is it caching the selection?

Here is the .cs:

protected void LoadGenders(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            ((DropDownList)sender).Items.AddRange(Constants.GenderWithSelect);
        }
public static ListItem[] GenderWithSelect = (new[] { new ListItem("--Select Gender--", "-1") }).Concat(Gender).ToArray();
 public static ListItem[] Gender = new[]
                                              {
                                                  (new ListItem("Male","M")),
                                                  (new ListItem("Female","F"))
                                              };


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

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

发布评论

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

评论(2

尾戒 2024-11-14 14:07:04

我尝试了您的代码,似乎生成的 html 没有绑定到选择选项的“值”的正确字段。

您必须获得的内容必须具有以下格式:

<select name="ctl00$MainContent$ddlGender" id="MainContent_ddlGender" class="select_Box">
  <option selected="selected" value="-1">not set</option>
  <option value="0">0</option>
  <option value="1">1</option>
</select>

为了实现此目的,我必须设置 DropDownList 的 DataValueField:(

<asp:DropDownList ID="ddlGender" runat="server" CssClass="select_Box" OnPreRender="ddlGender_PreRender" DataValueField="Value" DataTextField="Text">

只需将 DataTextField 和 DataValueField 替换为您的类的属性名称)

I tried your code, and it appears that the generated html didn't have the correct field bound to the "value" of the select options.

What you must get must have this format :

<select name="ctl00$MainContent$ddlGender" id="MainContent_ddlGender" class="select_Box">
  <option selected="selected" value="-1">not set</option>
  <option value="0">0</option>
  <option value="1">1</option>
</select>

In order to have this, I had to set the DataValueField of the DropDownList:

<asp:DropDownList ID="ddlGender" runat="server" CssClass="select_Box" OnPreRender="ddlGender_PreRender" DataValueField="Value" DataTextField="Text">

(Just replace the DataTextField and DataValueField with the name of the properties of your class)

淡紫姑娘! 2024-11-14 14:07:04

事实证明这与控件的加载顺序有关。因此,将 selectedindex 添加到 LoadGenders 就可以了。

protected void LoadGenders(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        ((DropDownList)sender).Items.AddRange(Constants.GenderWithSelect);
        ((DropDownList) sender).SelectedIndex = 0;
    }

It turns out it has something to do with load sequence of the controls. So, adding selectedindex to LoadGenders did the trick.

protected void LoadGenders(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        ((DropDownList)sender).Items.AddRange(Constants.GenderWithSelect);
        ((DropDownList) sender).SelectedIndex = 0;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文