GridView和下拉菜单DropDownList结合

刚刚把那个程序编出来~~~呵呵,写一下  语言表达能力不强,看代码就ok了~~~
先来看看效果图:
更新前:

点击编辑后(专业那一列出现数据绑定的DropDownList):

点击更新后保存了结果(注意对比第一张政治经济学原理那一行的所属专业):

晕~~~~宿舍要断点了~~~明天把代码贴出来
前台代码:(主要是GridView控件的代码)

< asp:GridView ID = " GridView1 "  runat = " server "  AllowPaging = " True "  
                AutoGenerateColumns
= " False "  BackColor = " White "  BorderColor = " #CCCCCC "  
                BorderStyle
= " None "  BorderWidth = " 1px "  CellPadding = " 3 "  Width = " 438px "  
                AllowSorting
= " True "  onpageindexchanging = " GridView1_PageIndexChanging "  
                onrowdeleting
= " GridView1_RowDeleting "  PageSize = " 6 "  
                onrowcancelingedit
= " GridView1_RowCancelingEdit "  
                onrowediting
= " GridView1_RowEditing "  onrowupdating = " GridView1_RowUpdating " >
                
< PagerSettings FirstPageText = " 第一页 "  LastPageText = " 最后一页 "  
                    Mode
= " NextPreviousFirstLast "  NextPageText = " 下一页 "  PreviousPageText = " 上一页 "   />
                
< FooterStyle BackColor = " White "  ForeColor = " #000066 "   />
                
< RowStyle ForeColor = " #000066 "   />
                
< Columns >
                    
< asp:BoundField DataField = " Name "  HeaderText = " 课程名称 "   />
                    
< asp:TemplateField HeaderText = " 所属专业 " >
                        
< ItemTemplate >
                            
< asp:Label ID = " Label1 "  runat = " server "  Text = ' <%# Bind("Expr1") %> ' ></ asp:Label >
                        
</ ItemTemplate >
                        
< EditItemTemplate >
                            
< asp:DropDownList ID = " DropDownList2 "  runat = " server "  
                                DataSource
= " <%# ddlBind() %> "  DataTextField = " Name "  
                                DataValueField
= " ID " >
                            
</ asp:DropDownList >
                        
</ EditItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:BoundField DataField = " JoinTime "  HeaderText = " 加入时间 "  
                        DataFormatString
= " {0:yyyy-MM-dd} "   />
                    
< asp:CommandField HeaderText = " 编辑 "  ShowEditButton = " True "   />
                    
< asp:TemplateField HeaderText = " 删除 "  ShowHeader = " False " >
                        
< ItemTemplate >
                            
< asp:LinkButton ID = " LinkButton1 "  runat = " server "  CausesValidation = " False "  
                                CommandName
= " Delete "  Text = " 删除 "  OnClientClick = " return confirm('确认要删除吗?') " ></ asp:LinkButton >
                        
</ ItemTemplate >
                    
</ asp:TemplateField >
                
</ Columns >
                
< PagerStyle BackColor = " White "  ForeColor = " #000066 "  HorizontalAlign = " Left "   />
                
< SelectedRowStyle BackColor = " #669999 "  Font - Bold = " True "  ForeColor = " White "   />
                
< HeaderStyle BackColor = " #006699 "  Font - Bold = " True "  ForeColor = " White "   />
            
</ asp:GridView >
后台代码:
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Data.SqlClient;

public   partial   class  Admin_kecheng_xinxi : System.Web.UI.Page
{
    BassClass bc 
= new BassClass();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            Bind();
        }

    }


    
private void Bind()
    
{
        GridView1.DataSource 
= bc.GetDataSet("Select * From kecheng_zhuanye_view ORDER BY ID DESC""kecheng_zhuanye_view");
        GridView1.DataKeyNames 
= new String[] "ID" };
        GridView1.DataBind();
    }

    
public DataSet ddlBind()
    
{
        SqlConnection conn 
= bc.getCon();
        conn.Open();
        SqlDataAdapter adq 
= new SqlDataAdapter("select * from tb_Profession", conn);
        DataSet dataset 
= new DataSet();
        adq.Fill(dataset, 
"tb_Profession");
        conn.Close();
        
return dataset;
    }


    
protected void Button1_Click(object sender, EventArgs e)
    
{

    }

    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
string Name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
        DropDownList ddl 
= (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
        
int ofProfession =Convert.ToInt32( ddl.SelectedValue);
        
bool bl = bc.ExecSQL("update tb_Lesson set Name='" + Name + "' , ofProfession='"+ofProfession+"' where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'");
        
if (bl)
        
{
            Response.Write(bc.messageBox(
"更新成功!"));
        }

        
else
        
{
            Response.Write(bc.messageBox(
"更新失败!"));
        }

        GridView1.EditIndex 
= -1;
        Bind();
    }

    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        Bind();
    }

    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
bool bl = bc.ExecSQL("delete from tb_lesson where ID='" + GridView1.DataKeys[e.RowIndex] + "'");
        
if (bl)
        
{
            Response.Write(bc.messageBox(
"删除成功!"));
        }

        
else
        
{
            Response.Write(bc.messageBox(
"删除失败!"));
        }

    }

    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        Bind();
    }

    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        Bind();
    }

}

转载于:https://www.cnblogs.com/dushouke/archive/2008/03/27/1126232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值