2011-11-29 118 views
1

当我点击按钮的信息不会加载到我的表中。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-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> 
     &nbsp;</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> 

这里是我的frmManageUsers

public partial class frmManageUsers : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    protected void btnAddUser_Click1(object sender, EventArgs e) 
    { 
       //string userName, userPassword; 



     if (txtUserName.Text == "" || txtUserName.Text == null) 
     { 
      lblError.Text = ("User Name may not be empty"); 
      lblError.ForeColor = System.Drawing.Color.Red; 
      return; 
     } 
     // else 

      // userName = (txtUserName.Text); 


     if (txtPassword.Text == "" || txtPassword.Text == null) 
     { 
      lblError.Text = ("Password may not be empty"); 
      lblError.ForeColor = System.Drawing.Color.Red; 
      return; 
     } 
     //else 
      // userPassword = (txtPassword.Text); 




     Session["UserName"] = txtUserName.Text; 
     Session["Password"] = txtPassword.Text; 
     Session["SecurityLevel"] = drpdwnlstSecurityLevel.SelectedValue; 
     Server.Transfer("frmManageUsers.aspx"); 



     //Server.Transfer("grdUserLogin"); 

    } 
    protected void drpdwnlstSecurityLevel_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 
} 
+0

虽然'SqlDatSource'足够聪明,但它不能更新你的数据,而不指定'UpdateCommand'和'UpdateParameters' –

回答

1

你的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(); 
     } 

PS。在你的if语句,如:

if (txtUserName.Text == "" || txtUserName.Text == null){//....} 

尝试使用

if(String.IsNullOrEmpty(txtUserName.Text)){//...} 

这是减少代码一个漂亮的小方法。