|
|
|
ASP.NET จะแสดงข้อมูลที่เป็น DropDownList บนใน GridView ทำไงครับ |
|
|
|
|
|
|
|
จะแสดงข้อมูลที่เป็น dropdownlist บนใน gridview ทำไงครับ
โค้ดของผมประมานนี้ครับ
Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminGridView.aspx.cs" Inherits="WebApp.AdminGridView" %>
<!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>
<style type="text/css">
.style1
{
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong><span class="style1">Admin</span></strong><br />
<br />
<asp:GridView ID="myGridView" runat="server"
AutoGenerateColumns="False"
ShowFooter="True"
DataKeyNames="UserName"
OnRowCancelingEdit="modCancelCommand"
OnRowCommand="myGridView_RowCommand"
OnRowDeleting="modDeleteCommand"
OnRowEditing="modEditCommand"
OnRowUpdating="modUpdateCommand" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label id="lblUserName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditUserName" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddUserName" size="5" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label id="lblPassword" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditPassword" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddPassword" size="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmailAdddress">
<ItemTemplate>
<asp:Label id="lblEmailAdddress" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmailAdddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditEmailAdddress" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmailAdddress") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddEmailAdddress" size="20" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อ - สกุล">
<ItemTemplate>
<asp:Label id="lblFullName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FullName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditFullName" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FullName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddFullName" size="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="หน่วยงาน">
<ItemTemplate>
<asp:Label id="lblAgencies" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Agencies") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditAgencies" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Agencies") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddAgencies" size="6" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ตำแหน่ง">
<ItemTemplate>
<asp:Label id="lblPosition" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Position") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditPosition" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Position") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddPosition" size="6" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ประเภทสมาชิก">
<ItemTemplate>
<asp:Label id="lblUserType" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserType") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditUserType" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserType") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddUserType" size="6" runat="server"></asp:TextBox>
<asp:Button id="btnAdd" runat="server" Text="เพิ่ม" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CancelText="ยกเลิก" DeleteText="ลบ" EditText="แก้ไข" UpdateText="บันทึก" HeaderText="แก้ไข" />
<asp:CommandField ShowDeleteButton="True" HeaderText="ลบ" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
</form>
</body>
</html>
Code (C#)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApp
{
public partial class AdminGridView : System.Web.UI.Page
{
SqlConnection objConn;
SqlCommand objCmd;
String strSQL;
protected void Page_Load(object sender, EventArgs e)
{
String strConnString;
strConnString = "Server=localhost;UID=sa;PASSWORD=12345;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection(strConnString);
objConn.Open();
if (!Page.IsPostBack)
{
BindData();
}
}
protected void BindData()
{
String strSQL;
strSQL = "SELECT * FROM Register";
SqlDataReader dtReader;
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to GridView ***//
myGridView.DataSource = dtReader;
myGridView.DataBind();
dtReader.Close();
dtReader = null;
}
protected void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
protected void modEditCommand(object sender, GridViewEditEventArgs e)
{
myGridView.EditIndex = e.NewEditIndex;
myGridView.ShowFooter = false;
BindData();
}
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
{
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
protected void modDeleteCommand(object sender, GridViewDeleteEventArgs e)
{
strSQL = "DELETE FROM Register WHERE UserName = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myGridView.EditIndex = -1;
BindData();
}
protected void modUpdateCommand(object sender, GridViewUpdateEventArgs e)
{
//*** UserName ***//
TextBox txtUserName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditUserName");
//*** Password ***//
TextBox txtPassword = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditPassword");
//*** Email ***//
TextBox txtEmailAdddress = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditEmailAdddress");
//*** FullName ***//
TextBox txtFullName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditFullName");
//*** Agencies ***//
TextBox txtAgencies = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditAgencies");
//*** Position ***//
TextBox txtPosition = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditPosition");
//*** UserType ***//
TextBox txtUserType = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditUserType");
strSQL = "UPDATE Register SET UserName = '" + txtUserName.Text + "' " +
" ,Password = '" + txtPassword.Text + "' " +
" ,EmailAdddress = '" + txtEmailAdddress.Text + "' " +
" ,FullName = '" + txtFullName.Text + "' " +
" ,Agencies = '" + txtAgencies.Text + "' " +
" ,Position = '" + txtPosition.Text + "' " +
" ,UserType = '" + txtUserType.Text + "' " +
" WHERE UserName = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
//*** UserName ***//
TextBox txtUserName = (TextBox)myGridView.FooterRow.FindControl("txtAddUserName");
//*** Password ***//
TextBox txtPassword = (TextBox)myGridView.FooterRow.FindControl("txtAddPassword");
//*** Email ***//
TextBox txtEmailAdddress = (TextBox)myGridView.FooterRow.FindControl("txtAddEmailAdddress");
//*** FullName ***//
TextBox txtFullName = (TextBox)myGridView.FooterRow.FindControl("txtAddFullName");
//*** Agencies ***//
TextBox txtAgencies = (TextBox)myGridView.FooterRow.FindControl("txtAddAgencies");
//*** Position ***//
TextBox txtPosition = (TextBox)myGridView.FooterRow.FindControl("txtAddPosition");
//*** UserType ***//
TextBox txtUserType = (TextBox)myGridView.FooterRow.FindControl("txtAddUserType");
strSQL = "INSERT INTO Register (UserName,Password,EmailAdddress,FullName,Agencies,Position,UserType) " +
" VALUES ('" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtEmailAdddress.Text + "' " +
" ,'" + txtFullName.Text + "','" + txtAgencies.Text + "','" + txtPosition.Text + "','" + txtUserType.Text + "') ";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
BindData();
}
}
}
}
Tag : .NET, Ms SQL Server 2005, C#
|
|
|
|
|
|
Date :
2012-12-13 21:11:25 |
By :
Kaen17 |
View :
1368 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยหน่อยครับ TT
|
|
|
|
|
Date :
2012-12-13 23:07:52 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะเอาคอลัมไหนเปน dropdownlist ครับ
วิธีการ
Code (VB.NET)
<Columns>
<asp:TemplateField HeaderText="UserName">
<EditItemTemplate>
<asp:dropdownlist id="txteditinstructor" backColor=Orange runat="server"
value='<%# DataBinder.Eval(Container, "DataItem.instructor") %>'>
</asp:dropdownlist>
</EditItemTemplate>
</Columns>
วิธีการเรียกใช้
Code (VB.NET)
Dim txteditinstrutor As DropDownList = CType(myGridView.Rows(e.RowIndex).FindControl("txteditinstructor"), DropDownList)
|
|
|
|
|
Date :
2012-12-14 08:38:31 |
By :
yo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะเอาคอลัม
Agencies
Position
UserType
เป็น C# ครับ
|
|
|
|
|
Date :
2012-12-14 09:32:44 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|