001.
<%@ Page Language=
"C#"
Debug=
"true"
%>
002.
<%@ import Namespace=
"System.Data"
%>
003.
<%@ import Namespace=
"MySql.Data.MySqlClient"
%>
004.
<script runat=
"server"
>
005.
String strKeyWord;
006.
void
Page_Load(
object
sender,EventArgs e)
007.
{
008.
strKeyWord =
this
.txtKeyWord.Text;
009.
}
010.
011.
void
BindData()
012.
{
013.
MySqlConnection objConn =
new
MySqlConnection();
014.
MySqlCommand objCmd =
new
MySqlCommand();
015.
MySqlDataAdapter dtAdapter =
new
MySqlDataAdapter();
016.
DataSet ds =
new
DataSet();
017.
String strConnString,strSQL;
018.
019.
strConnString =
"Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
;
020.
strSQL =
"SELECT * FROM customer WHERE (Name like '%"
+ strKeyWord +
"%' OR Email like '%"
+ strKeyWord +
"%') "
;
021.
022.
objConn.ConnectionString = strConnString;
023.
objCmd.Connection = objConn;
024.
objCmd.CommandText = strSQL ;
025.
objCmd.CommandType = CommandType.Text;
026.
027.
028.
dtAdapter.SelectCommand = objCmd;
029.
030.
dtAdapter.Fill(ds);
031.
032.
033.
myGridView.DataSource = ds;
034.
myGridView.DataBind();
035.
036.
dtAdapter =
null
;
037.
objConn.Close();
038.
objConn =
null
;
039.
040.
}
041.
042.
void
myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
043.
{
044.
045.
Label lblCustomerID = (Label)(e.Row.FindControl(
"lblCustomerID"
));
046.
if
(lblCustomerID !=
null
)
047.
{
048.
lblCustomerID.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"CustomerID"
);
049.
}
050.
051.
052.
Label lblName = (Label)(e.Row.FindControl(
"lblName"
));
053.
if
(lblName !=
null
)
054.
{
055.
lblName.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"Name"
);
056.
}
057.
058.
059.
Label lblEmail = (Label)(e.Row.FindControl(
"lblEmail"
));
060.
if
(lblEmail !=
null
)
061.
{
062.
lblEmail.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"Email"
);
063.
}
064.
065.
066.
Label lblCountryCode = (Label)(e.Row.FindControl(
"lblCountryCode"
));
067.
if
(lblCountryCode !=
null
)
068.
{
069.
lblCountryCode.Text = (
string
)DataBinder.Eval(e.Row.DataItem,
"CountryCode"
);
070.
}
071.
072.
073.
Label lblBudget = (Label)(e.Row.FindControl(
"lblBudget"
));
074.
if
(lblBudget !=
null
)
075.
{
076.
lblBudget.Text = DataBinder.Eval(e.Row.DataItem,
"Budget"
).ToString();
077.
}
078.
079.
080.
Label lblUsed = (Label)(e.Row.FindControl(
"lblUsed"
));
081.
if
(lblUsed !=
null
)
082.
{
083.
lblUsed.Text = DataBinder.Eval(e.Row.DataItem,
"Used"
).ToString();
084.
}
085.
}
086.
087.
void
btnSearch_Click(Object sender, EventArgs e)
088.
{
089.
BindData();
090.
}
091.
092.
void
ShowPageCommand(Object s, GridViewPageEventArgs e)
093.
{
094.
myGridView.PageIndex = e.NewPageIndex;
095.
BindData();
096.
}
097.
098.
</script>
099.
<html>
100.
<head>
101.
<title>ThaiCreate.Com ASP.NET - MySql</title>
102.
</head>
103.
<body>
104.
<form id=
"form1"
runat=
"server"
>
105.
<asp:Label id=
"lblKeyword"
runat=
"server"
text=
"Keyword"
></asp:Label>
106.
<asp:TextBox id=
"txtKeyWord"
runat=
"server"
></asp:TextBox>
107.
<asp:Button id=
"btnSearch"
onclick=
"btnSearch_Click"
runat=
"server"
Text=
"Search"
></asp:Button>
108.
<br />
109.
<br />
110.
<asp:GridView id=
"myGridView"
runat=
"server"
AllowPaging=
"True"
111.
AutoGenerateColumns=
"False"
onRowDataBound=
"myGridView_RowDataBound"
112.
OnPageIndexChanging=
"ShowPageCommand"
PageSize=
"2"
>
113.
<HeaderStyle backcolor=
"#cccccc"
></HeaderStyle>
114.
<AlternatingRowStyle backcolor=
"#e8e8e8"
></AlternatingRowStyle>
115.
<Columns>
116.
<asp:TemplateField HeaderText=
"CustomerID"
>
117.
<ItemTemplate>
118.
<asp:Label id=
"lblCustomerID"
runat=
"server"
></asp:Label>
119.
</ItemTemplate>
120.
</asp:TemplateField>
121.
<asp:TemplateField HeaderText=
"Name"
>
122.
<ItemTemplate>
123.
<asp:Label id=
"lblName"
runat=
"server"
></asp:Label>
124.
</ItemTemplate>
125.
</asp:TemplateField>
126.
<asp:TemplateField HeaderText=
"Email"
>
127.
<ItemTemplate>
128.
<asp:Label id=
"lblEmail"
runat=
"server"
></asp:Label>
129.
</ItemTemplate>
130.
</asp:TemplateField>
131.
<asp:TemplateField HeaderText=
"CountryCode"
>
132.
<ItemTemplate>
133.
<asp:Label id=
"lblCountryCode"
runat=
"server"
></asp:Label>
134.
</ItemTemplate>
135.
</asp:TemplateField>
136.
<asp:TemplateField HeaderText=
"Budget"
>
137.
<ItemTemplate>
138.
<asp:Label id=
"lblBudget"
runat=
"server"
></asp:Label>
139.
</ItemTemplate>
140.
</asp:TemplateField>
141.
<asp:TemplateField HeaderText=
"Used"
>
142.
<ItemTemplate>
143.
<asp:Label id=
"lblUsed"
runat=
"server"
></asp:Label>
144.
</ItemTemplate>
145.
</asp:TemplateField>
146.
</Columns>
147.
</asp:GridView>
148.
</form>
149.
</body>
150.
</html>