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    }
  • 相关阅读:
    SQL0668N Operation not allowed for reason code "3" on table "TEST". SQLSTATE=57016
    为何存在requests库,pycharm依然报错解决方法 --转载
    vmware12启动centos6.8报错ACPI:memory_hp:Memory online failed
    deepin升级之后打不开控制中心
    ubuntu中接一个摄像头会出现两个/dev/video
    VMware16中Ubuntu不显示共享文件夹的解决办法
    opencv获取当前帧数据问题
    libusb函数
    设置ubuntu、deppin(等linux系统)和window双系统启动引导顺序
    window和ubuntu双系统删除"ubuntu"
  • 原文地址:https://www.cnblogs.com/cdutedu/p/1281833.html
Copyright © 2011-2022 走看看