|
 |
|
ผมต้องการเลือกข้อมลด้วย dropdownlist ตามรูปแล้วถ้าเลือก "กรุณาเลือก" ข้อมูลจะเคลียหมด เพื่อเป็นการสร้าง employee คนใหม่ |
|
 |
|
|
 |
 |
|
ที่ department ไม่แสดงเพราะว่าคุณัยงไม่ได้ select department ที่เป็น ชื่อของมันออกมาครับ ถ้ามันเก็บแต่ id คุณก็ต้อง select อีก tb ของ department มาด้วยครับเพื่อที่จะเอาชื่อมาครับ ส่วนทำให้ textbox เป็นค่าว่างใช้ nothing ครับ แบบนี้ครับ textbox.text = nothing
|
 |
 |
 |
 |
Date :
2010-11-23 12:59:25 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แฮนเดิลอีเว็นต์ onselectedindexchange --> ตรวจสอบ --> ถ้าใช่ใน clear
|
 |
 |
 |
 |
Date :
2010-11-23 13:22:02 |
By :
ตังค์แมน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณต้องเขียนใน event ของ dropdowlist ครับ ชื่อ event คุณ ตังค์แมน เข้าก็บอกแล้วส่วนการทำให้ textbox เป็นค่าว่าง ผมก้บอกไปแล้ว
|
 |
 |
 |
 |
Date :
2010-11-23 13:38:41 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณก็ต้องเขียนใน form load ไงครับ ว่าถ้าเลือก อะไรจะให้แสดงอะไร ใช้ combobox.selectindex ครับ
|
 |
 |
 |
 |
Date :
2010-11-23 15:17:49 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestProgrammerCsharp
{
public partial class EditFRM : System.Web.UI.Page
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter objAdapter;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlDataReader objReader;
String strConnString;
protected void Page_Load(object sender, EventArgs e)
{
this.txtEmployeeID.ReadOnly = true;
strConnString = "Data Source=.\\SQL2008;Initial Catalog=XXXXXXX;Integrated Security=true";
objConn = new SqlConnection(strConnString);
objConn.Open();
if (!Page.IsPostBack)
{
BindDataEdit();
BindDataDept();
}
}
private void BindDataEdit()
{
SqlDataAdapter objAdapter;
DataTable dt = new DataTable();
// string strSQL = "SELECT * FROM Employee WHERE EmployeeID ='" + Request.QueryString["EmployeeID"] + "'";
string strSQL = "SELECT * FROM Employee e LEFT JOIN Departments d ON e.DepartmentID=d.DepartmentID WHERE e.EmployeeID=" + Request.QueryString["EmployeeID"] + "";
//เรียก department ของพนักงาน
string strSQLDept = "SELECT * FROM Departments";
DataTable dtDept = new DataTable();
objAdapter = new SqlDataAdapter(strSQLDept,objConn);
objAdapter.Fill(dtDept);
//ปิดการเรียก department ของพนักงาน
objAdapter = new SqlDataAdapter(strSQL, objConn);
objAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
this.txtEmployeeID.Text = dt.Rows[0]["EmployeeID"].ToString();
this.txtEmployeeName.Text = dt.Rows[0]["EmployeeName"].ToString();
this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
this.txtTelephone.Text = dt.Rows[0]["Telephone"].ToString();
this.lblDeptarment.Text = dt.Rows[0]["DepartmentID"].ToString();//แสดง id department ของพนักงาน
//ผมคิดว่าบรรทัดนี้ต้อง ดึงข้อมูล Department มาใส่ใน DropdownList
}
else
{
Response.Write("<b><font color='red'>ไม่พบข้อมูล</font></b>");
}
}
protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlDept.SelectedItem.Value == "0")
{
this.txtEmployeeID.Text = "";
this.txtEmployeeID.ReadOnly = false;
this.txtEmployeeName.Text = "";
this.txtAddress.Text = "";
this.txtTelephone.Text = "";
this.btnCreate.Visible =true;
}
}
void BindDataDept()
{
ddlDept.Items.Clear();
string selectSQLDept;
selectSQLDept = "SELECT * FROM Departments";
objConn = new SqlConnection(strConnString);
objCmd = new SqlCommand(selectSQLDept, objConn);
SqlDataReader reader;
try
{
objConn.Open();
reader = objCmd.ExecuteReader();
// For each item, add the author name to the displayed
// list box text, and store the unique ID in the Value property.
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["DepartmentName"].ToString();
newItem.Value = reader["DepartmentID"].ToString();
ddlDept.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
//lblResultDept.Text = "Error reading list of DepartmentName.";
//lblResultDept.Text += err.Message;
}
finally
{
objConn.Close();
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
if (this.ddlDept.SelectedItem.Value=="0")
{
Response.Write("<b><font color='red'>กรุณาเลือกแผนกก่อนสร้างข้อมูล!!</font></b>");
}
else
{
string strSQL = "UPDATE Employee SET " +
" EmployeeName ='" + this.txtEmployeeName.Text + "'" +
" ,Address ='" + this.txtAddress.Text + "'" +
" ,Telephone ='" + this.txtTelephone.Text + "'" +
" ,DepartmentID ='" + this.ddlDept.SelectedItem.Value + "'" +
" WHERE EmployeeID ='" + Request.QueryString["EmployeeID"] + "'";
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
try
{
objCmd.ExecuteNonQuery();
Response.Write("<b><font color='red'>Update successful</font></b>");
}
catch (Exception ex)
{
Response.Write("<b><font color='red'>Update failed (" + ex.Message + ")</font></b>");
}
}
}
}
}
|
ประวัติการแก้ไข 2010-11-23 16:14:32 2010-11-23 16:16:41
 |
 |
 |
 |
Date :
2010-11-23 16:12:09 |
By :
chon2008 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าคลิกปุ่มยกเลิกแล้วต้องการให้เคลียร์ค่าใน ใน dropdownlist เป็นค่าแรก หรือ "กรุณาเลือก" ต้องทำไงคะ
|
 |
 |
 |
 |
Date :
2012-01-23 22:13:34 |
By :
plykhem |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|