zoukankan      html  css  js  c++  java
  • DropDownList在GridView编辑时设置默认选项

    我们在数据库中有一个student数据表:

    注:studentSex为Bit类型,其中1代表男,0代表女

    在GridView中显示情况:

    我们在选择Edit后,需要在Sex列中以DropDownList形式展现,并且展现的DropDownList的默认选项当前学生的性别。如图所示:

    Sex列代码:

     1                <asp:TemplateField HeaderText="Sex">
     2                    <EditItemTemplate>
     3                        &nbsp;<asp:DropDownList ID="sex" runat="server">
     4                        </asp:DropDownList>
     5                    </EditItemTemplate>
     6                    <ItemTemplate>
     7                        <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToBoolean(Eval("studentSex"))?"男":"女" %>'></asp:Label>
     8                    </ItemTemplate>
     9                    <HeaderStyle Width="50px" />
    10                </asp:TemplateField>

    后台代码:

     1    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     2    {
     3        bool sex = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "studentSex"));
     4        if (e.Row.Cells[2].FindControl("sex"!= null)
     5        {
     6            DropDownList ddl = (DropDownList)e.Row.Cells[2].FindControl("sex");
     7            ListItem li1=new ListItem("","1");
     8            if (sex)
     9            {
    10                li1.Selected = true;
    11            }

    12            ddl.Items.Add(li1);
    13            ListItem li2 = new ListItem("""0");
    14            if (!sex)
    15            {
    16                li2.Selected = true;
    17            }

    18            ddl.Items.Add(li2);
    19        }

    20    }
  • 相关阅读:
    linux内核(四)内存管理单元MMU
    open函数详解
    linux内核(三)文件系统
    C++中数字与字符串之间的转换 scanf string总结(复习必读)
    hello程序的运行过程-从计算机系统角度
    剑指offer第12题打印从1到n位数以及大整数加法乘法
    2017-10-11第二次万革始面经
    为什么需要半关闭
    Ubuntu指令
    143. Reorder List
  • 原文地址:https://www.cnblogs.com/cdutedu/p/1281833.html
Copyright © 2011-2022 走看看