C# Asp.net按钮不工作
当我单击按钮时,信息不会加载到我的表中C# Asp.net按钮不工作,c#,asp.net,C#,Asp.net,当我单击按钮时,信息不会加载到我的表中 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmManageUsers.aspx.cs" Inherits="frmManageUsers" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transiti
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmManageUsers.aspx.cs" Inherits="frmManageUsers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/CoolBiz_Productions_logo.JPG" PostBackUrl="~/frmMain.aspx" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]">
</asp:SqlDataSource>
</div>
<div align="center">
<asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
<asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
<asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"
onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>U</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Button ID="btnAddUser" runat="server" Text="Add User"
onclick="btnAddUser_Click1" />
</p>
<p>
<asp:Label ID="lblError" runat="server"></asp:Label>
</p>
</div>
<p>
</p>
<div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
SortExpression="UserID"></asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName"></asp:BoundField>
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword"
SortExpression="UserPassword"></asp:BoundField>
<asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"
SortExpression="SecurityLevel"></asp:BoundField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
您的SQL数据源仅绑定到DATAGRID控件。我看不出您在btnAddUser_Click1事件处理程序中的何处调用以将记录插入数据库。您将需要添加缺少的逻辑,因为现有控件没有连接到该逻辑。你有代码漏洞。您将需要执行与以下内容相近的操作。这只是一个示例,我将其从我的一个数据访问层中取出,并为查看目的对其进行了修改
string _connectionString = string.Empty;
SqlCommand sqlCommand = new SqlCommand();
SqlConnection sqlConnection = new SqlConnection();
._connectionString = config["connectionString"];
sqlConnection.ConnectionString = "<your connection string>";
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure;
//add your parameters here
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
//Repeat for each parameter in your record.
int returnVal = 0;
sqlCommand.CommandText = "< insert record stored procedure name"
try
{
sqlConnection.Open();
using (sqlCommand)
{
returnVal = sqlCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//possibly put some logging inforaation here
}
finally
{
sqlConnection.Close();
}
试用
if(String.IsNullOrEmpty(txtUserName.Text)){//...}
这是一个减少代码的漂亮的小方法。您的SQL数据源仅绑定到您的DATAGRID控件。我看不出您在btnAddUser_Click1事件处理程序中的何处调用以将记录插入数据库。您将需要添加缺少的逻辑,因为现有控件没有连接到该逻辑。你有代码漏洞。您将需要执行与以下内容相近的操作。这只是一个示例,我将其从我的一个数据访问层中取出,并为查看目的对其进行了修改
string _connectionString = string.Empty;
SqlCommand sqlCommand = new SqlCommand();
SqlConnection sqlConnection = new SqlConnection();
._connectionString = config["connectionString"];
sqlConnection.ConnectionString = "<your connection string>";
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure;
//add your parameters here
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
sqlCommand.Parameters.Add("@ParameterNameWithinProcedure", <your value goes here>);
//Repeat for each parameter in your record.
int returnVal = 0;
sqlCommand.CommandText = "< insert record stored procedure name"
try
{
sqlConnection.Open();
using (sqlCommand)
{
returnVal = sqlCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//possibly put some logging inforaation here
}
finally
{
sqlConnection.Close();
}
试用
if(String.IsNullOrEmpty(txtUserName.Text)){//...}
这是一个减少代码的漂亮的小方法。虽然
SqlDatSource
具有足够的智能控制能力,但如果没有指定的UpdateCommand
和UpdateParameters
它就不能更新数据,而SqlDatSource
具有足够的智能控制能力,如果没有指定的UpdateCommand
和UpdateParameters