I've got a GridView. My application connects to MySql database, collects data and it should display it in GridView, but it doesn't. When I do it the same way in Windows Forms application - it fills DataGridView and there is no problem. But when I run my ASP.NET application in any browser - it shows label and button, but when I click "Fill grid" button - it doesn't show the GridView. I copied MySql.Data.dll and System.Data.dll to "bin" directory. There's no compilation error. Connection string is ok.
This is my ASP.NET application:
<%@ Page Language="C#" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<%@ Assembly Name = "MySql.Data" %>
<%@ Import Namespace = "System.Data" %>
<%@ Assembly Name = "System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnFillData_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Database=autolot;Uid=root;Pwd=pass;");
conn.Open();
MySqlCommand cmd = new MySqlCommand("select * from inventory;", conn);
MySqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
carsGridView.DataSource = dt;
carsGridView.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hello World!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblInfo" runat="server"
Text="Click on the button to fill the grid">
</asp:Label>
<br />
<br />
<asp:GridView ID="carsGridView" runat="server">
</asp:GridView>
<br />
<asp:Button ID="btnFillData" runat="server" Text="Fill grid" />
</div>
</form>
</body>
</html>
It doesn't look like the click handler is actually assigned to the button's click event. So the button is causing a post-back, but not invoking the handler. (I bet if you debug the current code you'll find that the btnFillData_Click
method is never even invoked.)
Try assigning it explicitly:
<asp:Button ID="btnFillData" runat="server" Text="Fill grid" OnClick="btnFillData_Click" />
您未将功能分配给HTML代码上的按钮,您需要将OnClick="btnFillData_Click"
添加到按钮Tag
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.