我有一个Login控件,其中我添加了一个额外的文本框,我想在登录时将日期时间,用户名和文本框数据保存到数据库中(登录时没有任何问题)。日期时间正在保存,但用户名和文本框文本未保存。下面是我的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>
</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>
答案 0 :(得分:0)
使用此代码检索登录用户的用户名:
MembershipUser u;
string uname;
u = Membership.GetUser();
uname = u.UserName;
请注意,我通常不会使用C#编码,因此您可能需要稍微调整上述代码。
关于textbox1
控件,您是否检查过呈现的页面以查看 textbox1 是否仍然是控件的ID? ASP毕竟动态呈现这些ID。尝试将此属性作为属性添加到控件中,并查看当前代码是否有效:
ClientIDMode="Static"