我如何把选择的值到一个DropDownList的顶部? [英] How do I bring the chosen value to the top of a DropDownList?

查看:113
本文介绍了我如何把选择的值到一个DropDownList的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  如何在下拉列表中的数值选用救?

我有一个 GridView控件,并用的DropDownList 而不是文本框。下拉有2个值(1.No,2.Yes)。

I have a GridView and a column with a DropDownList instead of TextBox. The drop down has 2 values (1.No, 2.Yes).

问题是,我只能选择Yes值。在code背后适用于好是,我还增加了价值没有。

The problem is, I can only choose the value "Yes". The code behind works for good for yes and I also added for value No.

和另一件事是,如果我选择一个值(是,例如),我怎么可以把它的顶部的DropDownList ,因为它总是呈现默认值(值否)。

And another thing is if I choose a value ("Yes", for example), how can I bring it to the top of the DropDownList, because it's always showing the default value (the value "No").

推荐答案

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 -->

code背后

 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();

    }
 }

这篇关于我如何把选择的值到一个DropDownList的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆