午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

GridView和下拉菜單DropDownList結(jié)合

 glxym 2014-01-28

GridView和下拉菜單DropDownList結(jié)合


先來看看效果圖:

更新前:

680x282

點擊編輯后(專業(yè)那一列出現(xiàn)數(shù)據(jù)綁定的DropDownList):

點擊更新后保存了結(jié)果(注意對比第一張政治經(jīng)濟學(xué)原理那一行的所屬專業(yè)):

610x427

前臺代碼:(主要是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="所屬專業(yè)">
                        <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('確認(rèn)要刪除嗎?')"></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();
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多