【问题标题】:How do I bring the chosen value to the top of a DropDownList? [duplicate]如何将所选值带到 DropDownList 的顶部? [复制]
【发布时间】:2012-03-18 14:52:20
【问题描述】:

可能重复:
How to save in the dropdown the value choosed?

我有一个GridView 和一个带有DropDownList 而不是TextBox 的列。下拉菜单有 2 个值(1.No,2.Yes)。

问题是,我只能选择“是”这个值。后面的代码是好的,我也添加了价值。

另一件事是,如果我选择一个值(例如“是”),我如何将它带到DropDownList 的顶部,因为它始终显示默认值(值“否”)。

【问题讨论】:

  • 使用 selectedindex=1 显示值 yes。
  • 是的,它现在工作了我如何保存价值?如果我在更新时选择某些内容,我希望下拉列表中的值被选中

标签: c# asp.net sql


【解决方案1】:

HTML

 <!--Add other attributes as you need to the grid view-->
 <!--NOTE: **OnRowDataBound="GridView_RowDataBound"** -->
 <asp:GridView ID="GridView1" runat="server" 
       OnRowDataBound="GridView1_RowDataBound">
 <Columns>

   <asp:TemplateField HeaderText="Dropdown Column">
       <ItemTemplate>
          <asp:DropDownList ID="ddlYesNo" runat="server">
              <asp:ListItem Value="1" Text="No"></asp:ListItem>
              <asp:ListItem Value="2" Text="Yes"></asp:ListItem>
          </asp:DropDownList>
       </ItemTemplate>
   </asp:TemplateField>
   <!-- OTHER COLUMNS -->
 </Columns>
 <!-- REST OF THE STUFF -->

代码隐藏

 protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
 {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = (DropDownList)e.Row.FindControl("ddlYesNo");
        ddl.SelectedValue = 
          ((System.Data.DataRowView)e.Row.DataItem) ["dataColName"].ToString();

    }
 }

【讨论】:

  • 这是做什么的? ddl.SelectedValue = ((System.Data.DataRowView)e.Row.DataItem) ["dataColName"].ToString();检查选定的值是否为“dataColName”我需要将 dataColName 替换为?
  • 即更新数据库时保存ddl.SelectedValue的列名。如果您需要选择更新的值,则需要将该值分配回下拉列表控件。这就是它正在做的事情。有意义吗?
  • 好吧,更新时我希望选择的值在单元格中可见
  • @InziIrina 在下拉列表中使用 SelectedValue=。
猜你喜欢
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
  • 2017-09-13
  • 1970-01-01
相关资源
最近更新 更多