|
 |
|
ในบทเรียน DataListFindControl อยากให้เลือก Checkbox ได้ข้อเดียวจะทำได้ไหมครับ |
|
 |
|
|
 |
 |
|
Code (C#)
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM category";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataList ***//
myDataList.DataSource = dtReader;
myDataList.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void myDataList_ItemDataBound(Object sender, DataListItemEventArgs e)
{
//*** Image ***//
Image img = (Image)(e.Item.FindControl("imgPicture"));
if (img != null)
{
img.ImageUrl = (string)DataBinder.Eval(e.Item.DataItem, "Picture");
//img.Attributes.Add("OnClick","window.location='https://www.thaicreate.com?Cateid=" + DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString() + "'");
//img.Style.Add("cursor","hand");
}
//*** lblCateID ***//
Label lblID = (Label)(e.Item.FindControl("lblCateID"));
if (lblID != null)
{
lblID.Text = DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();
lblID.Visible = false; //*** Hide ***//
}
//*** HyperLink ***//
HyperLink hplCate = (HyperLink)(e.Item.FindControl("hplCategory"));
if (hplCate != null)
{
hplCate.Text = (string)DataBinder.Eval(e.Item.DataItem, "CategoryName");
hplCate.ToolTip =(string)DataBinder.Eval(e.Item.DataItem, "CategoryName");
hplCate.NavigateUrl = "https://www.thaicreate.com?Cateid=" + DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();
}
}
void Button1_Click(object sender,EventArgs e)
{
CheckBox chkCate;
Label lblID;
int i;
lblText.Text = "";
for( i = 0; i <= myDataList.Items.Count - 1; i++)
{
chkCate = (CheckBox)myDataList.Items[i].FindControl("chkCateID");
lblID = (Label)myDataList.Items[i].FindControl("lblCateID");
if(chkCate.Checked)
{
//*** Have lblID.Text ***//
this.lblText.Text = this.lblText.Text + "<br>" + lblID.Text;
}
}
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DataList</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList id="myDataList" runat="server" borderstyle="inset" cellspacing="2" cellpadding="2" RepeatColumns="2" onItemDataBound="myDataList_ItemDataBound">
<HeaderTemplate>
<b>My Category</b> <br />
</HeaderTemplate>
<ItemTemplate>
<div style="width:100px" align="center">
<asp:Image id="imgPicture" runat="server"></asp:Image>
<br />
<asp:Label id="lblCateID" runat="server"></asp:Label>
<asp:CheckBox id="chkCateID" runat="server"></asp:CheckBox>
<asp:HyperLink id="hplCategory" runat="server"></asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Quote:จากบทเรียนเรื่องนี้อะครับ ถ้าจะให้เป็นเลือกได้ทีละข้อต้องทำอย่างไรหรอครับ
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-09-09 13:53:09 |
By :
nongbreesh |
View :
1237 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Checkbox เป็น Control สำหรับหลายทางเลือกครับ ลองเปลี่ยนเป็น Radio Button แทนครับ
|
 |
 |
 |
 |
Date :
2009-09-10 09:29:39 |
By :
Gg |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|