|
|
|
ต้องการเพิ่มข้อมูลในอีก ฟอร์ม-1ให้ลง db เเละให้ฟอร์ม-2 ดึงค่ามาจาก db เป็น dropdownlist ครับ c# |
|
|
|
|
|
|
|
Code (C#)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataReader dtReader;
String strSQL;
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DropDownListDataTable();
DropDownListDataTableRows();
DropDownListSortedList();
DropDownListAddInsertItem();
}
}
//*** DropDownList & DataTable ***//
void DropDownListDataTable()
{
SqlConnection objConn;
SqlDataReader dtReader;
DataTable dt = new DataTable();
String strConnString;
strConnString = "Server=localhost;UID=sa;PASSWORD=12345;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM country";
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
dtReader = null;
objConn.Close();
objConn = null;
//*** DropDownList ***//
this.myDDL1.DataSource = dt;
this.myDDL1.DataTextField = "CountryName";
this.myDDL1.DataValueField = "CountryCode";
this.myDDL1.DataBind();
//*** Default Value ***//
myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByValue("TH")); //*** By DataValueField ***//
//myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByText("Thailand")); //*** By DataTextField ***//
}
//*** DropDownList & TableRows ***//
void DropDownListDataTableRows()
{
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("Sex");
dt.Columns.Add("SexDesc");
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "M";
dr["SexDesc"] = "Man";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "W";
dr["SexDesc"] = "Woman";
dt.Rows.Add(dr);
//*** DropDownList ***//
this.myDDL2.DataSource = dt;
this.myDDL2.DataTextField = "SexDesc";
this.myDDL2.DataValueField = "Sex";
this.myDDL2.DataBind();
//*** Default Value ***//
myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** DropDownList & SortedList ***//
void DropDownListSortedList()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL3.DataSource = mySortedList;
this.myDDL3.DataTextField = "Value";
this.myDDL3.DataValueField = "Key";
this.myDDL3.DataBind();
//*** Default Value ***//
myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** Add/Insert Items ***//
void DropDownListAddInsertItem()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL4.DataSource = mySortedList;
this.myDDL4.DataTextField = "Value";
this.myDDL4.DataValueField = "Key";
this.myDDL4.DataBind();
//*** Add & Insert New Item ***//
String strText,strValue;
//*** Insert Item ***//
strText = "";
strValue = "";
ListItem InsertItem = new ListItem(strText, strValue);
myDDL4.Items.Insert(0, InsertItem);
//*** Add Items ***//
strText = "Guy";
strValue = "G";
ListItem AddItem = new ListItem(strText, strValue);
myDDL4.Items.Add(AddItem);
//*** Default Value ***//
myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByValue("")); //*** By DataValueField ***//
//myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByText("")); //*** By DataTextField ***//
}
void Button1_OnClick(object sender, EventArgs e)
{
this.lblText1.Text = this.myDDL1.SelectedItem.Value; //*** Or this.myDDL1.SelectedItem.Text ***//
this.lblText2.Text = this.myDDL2.SelectedItem.Value; //*** Or this.myDDL2.SelectedItem.Text ***//
this.lblText3.Text = this.myDDL3.SelectedItem.Value; //*** Or this.myDDL3.SelectedItem.Text ***//
this.lblText4.Text = this.myDDL4.SelectedItem.Value; //*** Or this.myDDL4.SelectedItem.Text ***//
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DropDownList & DataBind</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList id="myDDL1" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL2" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL3" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL4" runat="server"></asp:DropDownList>
<asp:Button id="Button1" onclick="Button1_OnClick" runat="server" Text="Button"></asp:Button>
<hr />
<asp:Label id="lblText1" runat="server"></asp:Label><br />
<asp:Label id="lblText2" runat="server"></asp:Label><br />
<asp:Label id="lblText3" runat="server"></asp:Label><br />
<asp:Label id="lblText4" runat="server"></asp:Label><br />
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</form>
</body>
</html>
ลองเเล้วครับ เเต่ dropdownlist ไม่มาอะครับ
|
|
|
|
|
Date :
2012-12-18 13:33:50 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยหน่อยครับ ไม่ error เเต่ใน dropdownlist ไม่ขึนอะครับ
|
|
|
|
|
Date :
2012-12-18 19:23:05 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|