【问题标题】:Display Dropdown When Editing GridView编辑 GridView 时显示下拉菜单
【发布时间】:2011-05-19 14:31:14
【问题描述】:

当用户编辑行时,我想在我的 GridView 中将一个字段显示为 DropDownList。 DropDownList 将预先填充两个值,“Yes”和“No”,根据用户选择的值,我想设置一个变量。

例子:

我有一个名为 active 的字段。 1 = 活动,0 = 不活动。尽管对于用户,我希望他们将 Active 设置为“是”(1)或“否”(0)。当编辑一行时,他们可以从下拉列表中选择“是”或“否”,它会将变量设置为 1 或 0,因此我可以在 SQL 更新中将其发回。

我找到了这个MSDN article

但它只告诉我如何从 DataSource 填充 DropDownList,这对我不起作用,因为每个字段都有一个是或否表示活动。即使只是在查看 GridView 时,它也会显示一个下拉列表,而不仅仅是在编辑时。

我希望这是有道理的,谢谢你的帮助。

编辑

这是我现在拥有的代码,它几乎可以像我想要的那样工作。我现在需要做的就是如果 Active 的值为“True”,则将标签文本更改为“Yes”,如果 Active 的值为“False”,则将文本更改为“No”。

<asp:TemplateField HeaderText="Active" SortExpression="Active" >
                        <ItemTemplate>
                             <asp:Label ID="lblActive" runat="server" text='<%# Eval("Active") %>'/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Active")  %>'>
                                <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                                <asp:ListItem Text="No" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>

【问题讨论】:

  • 感谢所有回复。我还有一个问题,见上文。
  • 我将 Eval 值传递给后面代码中的方法来解决问题。

标签: c# asp.net sql gridview drop-down-menu


【解决方案1】:

您可以像...一样添加列表项

<asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
    </asp:DropDownList>

您可以绑定SelectedValue,它会自动向数据库传递1 or o

【讨论】:

  • 如果我只想让用户设置 Active 值,这将起作用,但我需要为 Active 提取当前值,然后在 GridView 的“编辑”模式下通过下拉更改该值.
  • 这就是绑定 SelectedValue 属性将为您做的事情。
【解决方案2】:

使用EditTemplate 中的下拉菜单并使用标签在ItemTemplate 中显示选择 请参阅msdn post 中的此示例(下拉列表场景一直滚动到末尾)。

【讨论】:

    【解决方案3】:
    1. 在edititem模板中放置一个标签控件(显示数据库中的当前值)和下拉列表

      <asp:Label ID="lbl" runat="server" Text='<%#Eval("authostatus") %>' Visible="false"></asp:Label>
      <asp:DropDownList ID="Autharisationddl" runat="server">
        <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
      </asp:DropDownList>
      
    2. gridview rowdatabound 事件中根据标签控件值设置下拉值。

      if ((e.Row.RowState & DataControlRowState.Edit) > 0)
      {
        Label lbl= (Label)e.Row.FindControl("lbl");
        DropDownList ddl= (DropDownList)e.Row.FindControl("ddl");
      
        if (lbl!= null)
        {
          if (lbl.Text == "1")
            ddl.SelectedValue = "1";
          else if (lbl.Text == "0")
            ddl.SelectedValue = "0";
        }
      }
      

    谢谢

    【讨论】:

    • 这很有帮助,但我想根据从 SQL 查询中获得的值设置标签。查看我对原始帖子的编辑。
    【解决方案4】:

    您不需要行数据绑定,只需将您的列转换为模板字段。

    【讨论】:

      【解决方案5】:

      这是迟到的回复,但它会帮助遇到相同情况的其他人。

      codezone4 tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-13
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 2016-02-03
        相关资源
        最近更新 更多