c# - 登录用户名和文本框文本未保存在数据库中

标签 c# asp.net textbox username login-control

我有一个登录控件,在其中添加了一个额外的文本框,我想在登录时将日期时间,用户名和文本框数据保存到数据库中(登录没有任何问题)。日期时间正在保存,但用户名和文本框文本未保存。以下是我的aspx.cs代码:-

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox Txt = (TextBox)Login1.FindControl("TextBox1"); //Login1 is loginID and TextBox1 is textbox ID
                if (Txt != null)
                {
                    Txt.Focus();
                }
            if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true))
            {
                    // If User is Authenticated then 
                    if (User.Identity.IsAuthenticated)
                    //call the method to execute insert to the database
                    ExecuteInsert(Txt.Text,
                               DateTime.Now,
                               Login1.UserName); //Login1 is login ID
                }
            }

            if (!this.IsPostBack)
                ViewState["LoginErrors"] = 0;
        }     


插入方法:

     private void ExecuteInsert(string AuditorComments, DateTime AuditDate, string Auditor)
            {
                SqlConnection conn = new SqlConnection("Data Source = usftdl0008; Initial Catalog = *******;Persist Security Info=True;User ID=*****;Password=*****; Integrated Security = TRUE");
                string sql = "UPDATE TBLhsCaser SET AuditorComments = @AuditorComments, AuditDate = @AuditDate, Auditor = @Auditor WHERE AuditDate IS NULL ";

                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlParameter[] param = new SqlParameter[3];
                    //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
                    param[0] = new SqlParameter("@AuditorComments", SqlDbType.VarChar, 50);
                    param[1] = new SqlParameter("@AuditDate", SqlDbType.DateTime, 50);
                    param[2] = new SqlParameter("@Auditor", SqlDbType.VarChar, 50);

                    param[0].Value = AuditorComments;
                    param[1].Value = AuditDate;
                    param[2].Value = Auditor;


                    for (int i = 0; i < param.Length; i++)
                    {
                        cmd.Parameters.Add(param[i]);
                    }

                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    string msg = "Insert Error:";
                    msg += ex.Message;
                    throw new Exception(msg);
                }
                finally
                {
                    conn.Close();
                }

            }


我的用户控件

 <asp:Login ID="Login1" runat="server" MembershipProvider="LocalSqlServer" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4"
                        BorderStyle="Solid" BorderWidth="1px" DisplayRememberMe="False" Font-Names="Verdana"
                        Font-Size="0.8em" ForeColor="#333333" Height="242px" LoginButtonText="Audit"
                        TitleText="Auditor Log In" UserNameLabelText="Auditor Name:" Visible="False"
                        Width="423px" OnAuthenticate="Login1_Authenticate">
                        <TextBoxStyle Font-Size="0.8em" />

                        <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
                            Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
                        <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
                        <TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
                        <LayoutTemplate>
                            <table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse">
                                <tr>
                                    <td>
                                        &nbsp;</td>
                                </tr>
                            </table>
                                        <table border="0" cellpadding="0" style="width: 422px; height: 253px">
                                            <tr>
                                                <td align="center" colspan="2" style="font-weight: bold; font-size: 0.9em; color: white;
                                                    background-color: #507cd1">
                                                    Auditor Log In</td>
                                            </tr>
                                            <tr>
                                                <td align="right">
                                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Auditor Name:</asp:Label></td>
                                                <td>
                                                    <asp:TextBox ID="UserName" runat="server" Height="16px" Width="126px"></asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                        ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="right">
                                                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                                                <td>
                                                    <asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password" Height="16px" Width="126px"></asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                        ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                                </td>
                                            </tr>
                                            <tr>
                                            <td align="right" style="width: 120px">
                                            <asp:Label ID="Label4" runat="server" ForeColor="Black" Height="21px" Text="Comments:"
                                                        Width="102px"></asp:Label></td>
                                              <td>
                                              <asp:TextBox ID="TextBox1" runat="server" Height="62px" Width="224px" >No Comments!</asp:TextBox>

                                            </td>
                                            </tr>
                                            <tr>
                                                <td align="center" colspan="2" style="color: red">
                                                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="right" colspan="2">
                                                    <asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
                                                        BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
                                                        Font-Size="0.8em" ForeColor="#284E98" Text="Audit" ValidationGroup="Login1" />
                                                </td>
                                            </tr>
                                        </table>
                        </LayoutTemplate>
                    </asp:Login>

最佳答案

使用以下代码检索登录用户的用户名:

MembershipUser u;
string uname;

u = Membership.GetUser();
uname = u.UserName;


请注意,我通常不使用C#编写代码,因此您可能需要稍微调整上面的代码。



关于textbox1控件,您是否检查了呈现的页面以查看textbox1是否仍然是控件的ID?毕竟,ASP会动态呈现这些ID。尝试将此作为属性添加到控件,并查看当前代码是否有效:

ClientIDMode="Static"

关于c# - 登录用户名和文本框文本未保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867066/

相关文章:

c# - asp,net 使用 site.master 页面

javascript - 避免重复的 JavaScript

c# - 如何在扩展类中向文本框添加属性

c# - 赋予委托(delegate)属性的优雅方式

c# - "valuetype?"属性的 getter 和 setter 可以有不同的返回类型吗?

c# - 不使用接口(interface)掌握 API

css - 如何在调整 RadToolBar 大小时对齐 RadToolBar 中调整大小的元素?

c# - 在下拉列表上使用 confirmbuttonextender 时出现重复值

c# - 嵌套字符串插值

html - 更正完形填空的文本输入宽度