[ASP.net Web Form] GridView的EditItemTemplate塞下拉選單Binding資料

[ASP.net Web Form] GridView的EditItemTemplate塞下拉選單Binding資料

資料面

Create table Person
(
 id int Not NULL identity,
 title varchar(50),
 Sex varchar(50)

)
Go
Insert into Person Values ('Shadow','M'),('Microsoft','F')

demo001

 

.aspx

 

<%@ Page Debug="true" Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource runat="server" ID="sds_Persion" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            DeleteCommand="DELETE FROM [Person] WHERE [id] = @id" 
            InsertCommand="INSERT INTO [Person] ([title], [Sex]) VALUES (@title, @Sex)" 
            SelectCommand="SELECT id,title, Sex FROM [Person]" 
            UpdateCommand="UPDATE [Person] SET [title] = @title, [Sex] = @Sex WHERE [id] = @id" >
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="title" Type="String" />
                <asp:Parameter Name="Sex" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="title" Type="String" />
                <asp:Parameter Name="Sex" Type="String" />
                <asp:Parameter Name="id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="sds_Persion" 
            onrowdatabound="GridView1_RowDataBound" onrowupdating="GridView1_RowUpdating" 
            >
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                <asp:TemplateField HeaderText="Sex" SortExpression="Sex">
                    <EditItemTemplate>
                    <!--給下拉選單撈資料用-->
                        <asp:Literal id="li_Sex" Text='<%# Eval("Sex") %>' Visible="false" runat="server" />
                        <asp:DropDownList runat="server" ID="ddl_Sex"    >
                            <asp:ListItem Text="男性" Value="M"  />
                            <asp:ListItem Text="女性" Value="F"/>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType==DataControlRowType.DataRow)
        {
            Label labelSex = ((Label)e.Row.FindControl("Label1"));
            if (labelSex!=null)
            {
                
                if (labelSex.Text == "M")
                {
                    e.Row.Cells[2].Text = "男性";
                }
                else
                {
                    e.Row.Cells[2].Text = "女性";
                }

                
            }

            Literal li_Sex = (Literal)e.Row.FindControl("li_Sex");
            if (li_Sex!=null)
            {
                DropDownList ddl_Sex = (DropDownList)e.Row.FindControl("ddl_Sex");
                ddl_Sex.SelectedValue = li_Sex.Text;
            }
           
        }
    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       DropDownList ddl_Sex =  (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddl_Sex");
       if (ddl_Sex!=null)
       {
           sds_Persion.UpdateParameters["Sex"].DefaultValue = ddl_Sex.SelectedValue;
       }
    }
}

 

一開始的執行畫面:

點選"編輯"

demo002

選擇女性

demo003

按下更新後

demo005