(C#) ASP.NET RadioButtonList & DataBinding |
(C#) ASP.NET RadioButtonList & DataBinding เป็นการใช้ RadioButtonList เรียกข้อมูลจาก Database ผ่าน ADO.NET ตัวอย่างนี้ผมได้ยกตัวอย่างการใช้บน DataTable,TableRows,SortedList รวมทั้งการเพิ่ม Item เข้าไปใน Control
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetRadioButtonListDataBind.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
RadioButtonListDataTable();
RadioButtonListDataTableRows();
RadioButtonListSortedList();
RadioButtonListAddInsertItem();
}
}
//*** RadioButtonList & DataTable ***//
void RadioButtonListDataTable()
{
OleDbConnection objConn;
OleDbDataAdapter dtAdapter;
DataTable dt = new DataTable();
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+
Server.MapPath("database/mydatabase.mdb")+"";
objConn = new OleDbConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM country";
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
//*** RadioButtonList ***//
this.myRdoList1.DataSource = dt;
this.myRdoList1.DataTextField = "CountryName";
this.myRdoList1.DataValueField = "CountryCode";
this.myRdoList1.DataBind();
//*** Default Value ***//
myRdoList1.SelectedIndex = myRdoList1.Items.IndexOf(myRdoList1.Items.FindByValue("TH")); //*** By DataValueField ***//
//myRdoList1.SelectedIndex = myRdoList1.Items.IndexOf(myRdoList1.Items.FindByText("Thailand")); //*** By DataTextField ***//
}
//*** RadioButtonList & TableRows ***//
void RadioButtonListDataTableRows()
{
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);
//*** RadioButtonList ***//
this.myRdoList2.DataSource = dt;
this.myRdoList2.DataTextField = "SexDesc";
this.myRdoList2.DataValueField = "Sex";
this.myRdoList2.DataBind();
//*** Default Value ***//
myRdoList2.SelectedIndex = myRdoList2.Items.IndexOf(myRdoList2.Items.FindByValue("W")); //*** By DataValueField ***//
//myRdoList2.SelectedIndex = myRdoList2.Items.IndexOf(myRdoList2.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** RadioButtonList & SortedList ***//
void RadioButtonListSortedList()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** RadioButtonList ***//
this.myRdoList3.DataSource = mySortedList;
this.myRdoList3.DataTextField = "Value";
this.myRdoList3.DataValueField = "Key";
this.myRdoList3.DataBind();
//*** Default Value ***//
myRdoList3.SelectedIndex = myRdoList3.Items.IndexOf(myRdoList3.Items.FindByValue("W")); //*** By DataValueField ***//
//myRdoList3.SelectedIndex = myRdoList3.Items.IndexOf(myRdoList3.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** Add/Insert Items ***//
void RadioButtonListAddInsertItem()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** RadioButtonList ***//
this.myRdoList4.DataSource = mySortedList;
this.myRdoList4.DataTextField = "Value";
this.myRdoList4.DataValueField = "Key";
this.myRdoList4.DataBind();
//*** Add & Insert New Item ***//
String strText,strValue;
//*** Insert Item ***//
strText = "";
strValue = "";
ListItem InsertItem = new ListItem(strText, strValue);
myRdoList4.Items.Insert(0, InsertItem);
//*** Add Items ***//
strText = "Guy";
strValue = "G";
ListItem AddItem = new ListItem(strText, strValue);
myRdoList4.Items.Add(AddItem);
//*** Default Value ***//
myRdoList4.SelectedIndex = myRdoList4.Items.IndexOf(myRdoList4.Items.FindByValue("")); //*** By DataValueField ***//
//myRdoList4.SelectedIndex = myRdoList4.Items.IndexOf(myRdoList4.Items.FindByText("")); //*** By DataTextField ***//
}
void Button1_OnClick(object sender, EventArgs e)
{
this.lblText1.Text = this.myRdoList1.SelectedItem.Value; //*** Or this.myRdoList1.SelectedItem.Text ***//
this.lblText2.Text = this.myRdoList2.SelectedItem.Value; //*** Or this.myRdoList2.SelectedItem.Text ***//
this.lblText3.Text = this.myRdoList3.SelectedItem.Value; //*** Or this.myRdoList3.SelectedItem.Text ***//
this.lblText4.Text = this.myRdoList4.SelectedItem.Value; //*** Or this.myRdoList4.SelectedItem.Text ***//
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - RadioButtonList & DataBind</title>
</head>
<body>
<form id="form1" runat="server">
<asp:RadioButtonList id="myRdoList1" runat="server"></asp:RadioButtonList><hr />
<asp:RadioButtonList id="myRdoList2" runat="server"></asp:RadioButtonList><hr />
<asp:RadioButtonList id="myRdoList3" runat="server"></asp:RadioButtonList><hr />
<asp:RadioButtonList id="myRdoList4" runat="server"></asp:RadioButtonList>
<asp:Button id="Button1" onclick="Button1_OnClick" runat="server" Text="Button"></asp:Button>
<hr />
<asp:Label id="lblText1" runat="server"></asp:Label><br /><br />
<asp:Label id="lblText2" runat="server"></asp:Label><br /><br />
<asp:Label id="lblText3" runat="server"></asp:Label><br /><br />
<asp:Label id="lblText4" runat="server"></asp:Label><br /><br />
</form>
</body>
</html>
Screenshot
ASP.NET & AccessDataSource and RadioButtonList
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-22 09:25:21 /
2017-03-28 20:47:32 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|