detailsview 在编辑的时候使用dropdownlist

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            bindData();
        }

    }

    
public void bindData()
    
{
        
string strSql = ConfigurationManager.AppSettings["strcon"].ToString();
        SqlConnection con 
= new SqlConnection(strSql);
        SqlCommand cmd 
= new SqlCommand("select * from test where id =2",con);
        con.Open();
        SqlDataReader read 
= cmd.ExecuteReader();
        DetailsView1.DataSource
=read;
        DetailsView1.DataBind();
        DropDownList drpsex 
= DetailsView1.FindControl("drpsex"as DropDownList;
        Label lblsex 
= DetailsView1.FindControl("lblsex"as Label;
        
//SqlDataReader reader = bindToDrop();
        
//drpsex.DataSource = reader;
        
//drpsex.DataBind();
        
//drpsex.SelectedIndex = drpsex.Items.IndexOf(drpsex.Items.FindByText(lblsex.Text.Trim())); //lblsex.Text.Trim();
        
    }

    
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    
{
        
//if (e.CommandName.Equals("Edit"))//实现一
        
//{
        
//    DetailsView1.ChangeMode(DetailsViewMode.Edit);
           
        
//}
       
        
//bindData();
    }

    
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    
{
        
//DetailsView1.ChangeMode(DetailsViewMode.Edit);
        
//bindData();
        
//实现二
        if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
        
{
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
        }

        
else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
        
{
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }

        bindData();
        
    }


    
public SqlDataReader bindToDrop()
    
{
        
string strSql = ConfigurationManager.AppSettings["strcon"].ToString();
        SqlConnection con 
= new SqlConnection(strSql);
        SqlCommand cmd 
= new SqlCommand("select distinct sex from test", con);
        con.Open();
        SqlDataReader read 
= cmd.ExecuteReader();
        
return read;
    }


    
protected void DetailsView1_ModeChanged(object sender, EventArgs e)
    
{
       
    }

    
protected void DetailsView1_DataBound(object sender, EventArgs e)
    
{
        
    }

    
protected void DetailsView1_PreRender(object sender, EventArgs e)
    
{
        
//实现dropdownlist 在编辑的时候显示一样的值
        if (this.IsPostBack)
        
{
            
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            
{
                DropDownList drpsex 
= DetailsView1.FindControl("drpsex"as DropDownList;
                Label lblsex 
= DetailsView1.FindControl("lblsex"as Label;
                drpsex.DataSource 
= bindToDrop();
                drpsex.DataBind();
                drpsex.SelectedValue 
= lblsex.Text.Trim();
            }

        }

    }

}


==============================================================================================
前台
 
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:DetailsView ID = " DetailsView1 "  runat = " server "   AutoGenerateRows = " False "   Height = " 50px "  Width = " 125px "  OnItemCommand = " DetailsView1_ItemCommand "  OnModeChanging = " DetailsView1_ModeChanging "  OnModeChanged = " DetailsView1_ModeChanged "  OnDataBound = " DetailsView1_DataBound "  OnPreRender = " DetailsView1_PreRender " >
            
< Fields >
                
< asp:TemplateField HeaderText = " Name " >
                    
< EditItemTemplate >
                        
< asp:TextBox ID = " TextBox1 "  runat = " server "  Text = ' <%# Bind("name") %> ' ></ asp:TextBox >
                    
</ EditItemTemplate >
                    
< InsertItemTemplate >
                        
< asp:TextBox ID = " TextBox1 "  runat = " server "  Text = ' <%# Bind("name") %> ' ></ asp:TextBox >
                    
</ InsertItemTemplate >
                    
< ItemTemplate >
                        
< asp:Label ID = " Label1 "  runat = " server "  Text = ' <%# Bind("name") %> ' ></ asp:Label >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField HeaderText = " Sex " >
                    
< EditItemTemplate >
                         
< asp:DropDownList ID = " drpsex "  runat = " server "  DataSource = " <%# bindToDrop() %> "   DataTextField = " sex " ></ asp:DropDownList >
                    
</ EditItemTemplate >
                    
< InsertItemTemplate >
                        
< asp:TextBox ID = " TextBox2 "  runat = " server "  Text = ' <%# Bind("sex") %> ' ></ asp:TextBox >
                    
</ InsertItemTemplate >
                    
< ItemTemplate >
                        
< asp:Label ID = " Label2 "  runat = " server "  Text = ' <%# Bind("sex") %> ' ></ asp:Label >
                       
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField >
                   
< ItemTemplate >
                      
< asp:Label ID = " lblsex "  runat = " server "  Text = ' <%# Bind("sex") %> ' ></ asp:Label >
                   
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField ShowHeader = " False " >
                    
< EditItemTemplate >
                        
< asp:Button ID = " Button1 "  runat = " server "  CausesValidation = " True "  CommandName = " Update "
                            Text
= " 更新 "   />& nbsp; < asp:Button ID = " Button2 "  runat = " server "  CausesValidation = " False "
                                CommandName
= " Cancel "  Text = " 取消 "   />
                    
</ EditItemTemplate >
                    
< ItemTemplate >
                        
< asp:Button ID = " Button1 "  runat = " server "  CausesValidation = " False "  CommandName = " Edit "
                            Text
= " 编辑 "   />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
            
</ Fields >
        
</ asp:DetailsView >
    
    
</ div >
    
</ form >

你可能感兴趣的:(detailsview 在编辑的时候使用dropdownlist)