<asp:templatefield HeaderText="性别">
<Itemtemplate><%# Eval("sex").ToString()=="1"?"男":"女" %></itemtemplate>
<EditItemTemplate>
 <asp:DropDownList id="ddl_sex" runat="server" AutoPostBack="true">
  <asp:ListItem Value="1" Text="男"  ></asp:ListItem>  ////如何根据数据库的值设置默认值
  <asp:ListItem Value="0" Text="女"  ></asp:ListItem>  ////
 </asp:DropDownList>
</EditItemTemplate>
</asp:templatefield>-------------------------
 要求就是按了编辑后,编辑模式下的性别列,能根据实际值,设定默认值。如果是女就默认显示“女”。而不是像现在这样怎么样都显示“男”。
         

解决方案 »

  1.   

    <asp:DropDownList id="ddl_sex" runat="server" AutoPostBack="true" SelectedValue='<%# Eval("sex")%>'
      <asp:ListItem Value="1" Text="男"  > </asp:ListItem>  
      <asp:ListItem Value="0" Text="女"  > </asp:ListItem>  
    </asp:DropDownList> 
      

  2.   

    ddl_sex.Items.FindByValue(......).Selected = true
      

  3.   


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.FindControl("ddl_sex") != null)
            {
                DropDownList dropSex = (DropDownList)e.Row.FindControl("ddl_sex");
                int a = Convert.ToInt32(((DataRowView)e.Row.DataItem)[0]); //找到Drop 对应的字段的值 自己写逻辑
                if (a % 2 == 0)
                {
                    dropSex.SelectedIndex = 1;
                }
                else
                {
                    dropSex.SelectedIndex = 0;
                }        }
        }
      

  4.   

    <asp:DropDownList id="ddl_sex" runat="server" AutoPostBack="true" SelectedValue=' <%# Eval("sex")%>'> 
      <asp:ListItem Value="1" Text="男"  > </asp:ListItem>  
      <asp:ListItem Value="0" Text="女"  > </asp:ListItem>  
    </asp:DropDownList>