发生案件时更改标签文本

时间:2017-06-02 10:27:46

标签: c# asp.net

我试图改变lblsource和lblmessage,如果发生这种情况我怎么做?

 <div class="form-horizontal well container-cus">         
 <div class="row">             
 <div class="col-lg-12">                 
 <br />                 
 <br />                 
 <div style="width: 350px; box-shadow: 5px 0px 40px rgba(0,0,0, .2);" class="panel panel-warning center-block">                     
 <div class="panel-heading">
 </div>                     
 <div class="panel-body">                         
 <div class="form-group">                             
 <div class="row">                                 
 <div class="text-center">                                     
 <img class="profile-img" src="/Images/user_icon.png" />                                 
 </div>                            
 </div>                             
 <br />                             
 <div class="row">                                 
 <div class="col-lg-12">                                     
 <asp:TextBox ID="txtUsername" CssClass="form-control" placeholder="Username" runat="server" required></asp:TextBox>                                 
 </div>                             
 </div>                         
 </div>                         
 <div class="form-group">                             
 <div class="row">                                 
 <div class="col-lg-12">                                     
 <asp:TextBox ID="txtPassword" class="form-control" placeholder="Password" runat="server" TextMode="Password" required></asp:TextBox>                                 
 </div>                             
 </div>                         
 </div>                         
 <br />                         
 <br />                         
 <div class="panel-footer">                             
 <div class="row">                                 
 <div class="col-lg-12">                                     
 <asp:Button ID="btnLogin" Style="width: 290px;" class="btn btn-warning btn-lg pull-right" runat="server" Text="LOGIN" OnClick="btnLogin_Click" />                                 
 </div>                             
 </div>                         
 </div>                      
 </div>                     
 <asp:Label ID="lblsource" runat="server" CssClass="text-danger" Text=""></asp:Label>                     
 <br />                     
 <asp:Label ID="lblmessage" runat="server" CssClass="text-danger" Text=""></asp:Label>                     
 <br />                     
 <asp:Label ID="Label1" runat="server" CssClass="text-danger" Text="Invalid Username or Password" Visible="false"></asp:Label>                 
 </div>                              
 </div>         
 </div>     
 </div>

上面的代码是我的.aspx,当登录lblsource时,lblmessage .text值必须在switch语句中输入某个case时改变

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Account_Suppliers_Login : System.Web.UI.Page
{
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(Helper.GetCon()))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT su.SupplierID, su.Status, ut.Username, ut.Password, ut.UserID FROM UserTable ut INNER JOIN Suppliers su ON su.UserID = ut.UserID Where ut.Username = '" + txtUsername.Text + "' AND ut.Password = '" + Helper.CreateSHAHash(txtPassword.Text) + "'", con);

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Session["SupplierID"] = dr["SupplierID"].ToString();
                    Session["Status"] = dr["Status"].ToString();
                    Session["Username"] = dr["Username"].ToString();
                    Session["UserID"] = dr["UserID"].ToString();
                }

                string statuscode = Session["Status"].ToString();


                switch (statuscode)
                {
                    case "Active":
                        Session["Status"] = null;
                        Session.Remove("Status");
                        Response.Redirect("\\Account\\Suppliers\\Default.aspx");
                        break;

                    case "Inactive":

                        lblsource.Text = "Login Page: INACTIVE";
                        lblmessage.Text = "Welcome Back! Please let us know on how we can help you better.";
                        Response.Redirect("\\Account\\Suppliers\\Login.aspx");
                        break;

                    case "Pending":
                        lblsource.Text = "Login Page: PENDING";
                        lblmessage.Text = "Thank you for registering with us. We will process your request shortly.";
                        lblsource.Visible = true;
                        lblmessage.Visible = true;

                        Response.Redirect("\\Account\\Suppliers\\Login.aspx");
                        break;

                    case "Blocked":
                        lblsource.Text = "Login Page: BLOCKED";
                        lblmessage.Text = "We are so glad to hear from you. Please settle your account.";
                        lblsource.Visible = true;
                        lblmessage.Visible = true;
                        Response.Redirect("\\Account\\Suppliers\\Login.aspx");
                        break;
                }

            }
            else
            {
                Label1.Visible = true;
            }




    }

    private DataSet GetRecordSuppplier(string TableName)
    {
        using (SqlConnection con = new SqlConnection(Helper.GetCon()))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM " + TableName + " WHERE SupplierID = '" + Session["SupplierID"] + "'";
            SqlDataAdapter da;
            da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            DataSet ds = new DataSet();
            da.Fill(ds, TableName);

            return ds;
                }
}

上面的代码是我的.aspx.cs 我只想在案件发生时更改lblsource和lblmessage的值

2 个答案:

答案 0 :(得分:0)

您不需要Response.Redirect是否在同一页上。因此,您重定向到默认值是有道理的,但重定向到登录页面不会,因为您已经在登录页面上。

只需删除这些重定向,然后让页面完成处理,您就会看到按预期应用的标签更新。

答案 1 :(得分:0)

您的按钮点击代码应如下所示:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>

  <body id="barcode">
     <img id="imageid" src="" alt="image">
  </body>

  <script>
    window.onload = function() {
      var url = "http://lorempixel.com/400/200/"
      var canvas = document.createElement('canvas');
      canvas.width = 200;
      canvas.height = 100;
      document.getElementById('barcode').appendChild(canvas);
      var ctx = canvas.getContext('2d');
      var image = new Image();
      image.src = url;
      ctx.drawImage(image, 0, 0);
      google.script.run.withSuccessHandler(function(a) {
         document.getElementById("imageid").src=a;
      }).im(canvas.toDataURL());
    }
  </script>
</html>