Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 错误:从页面上的控件获取按钮单击事件的值_Asp.net - Fatal编程技术网

Asp.net 错误:从页面上的控件获取按钮单击事件的值

Asp.net 错误:从页面上的控件获取按钮单击事件的值,asp.net,Asp.net,下面的代码只是从数据库获取数据并将数据填充到asp.net页面上的文本框中,当我单击按钮b时,文本框上的任何更改都会反映到数据库中。 但是,当我单击btnSave按钮时,我从文本框中获取的值是来自Page_Load事件的初始值,而不是我更改文本框上的值之后的值 public partial class Settings : System.Web.UI.Page { string conString = System.Configuration.ConfigurationManager.C

下面的代码只是从数据库获取数据并将数据填充到asp.net页面上的文本框中,当我单击按钮b时,文本框上的任何更改都会反映到数据库中。 但是,当我单击btnSave按钮时,我从文本框中获取的值是来自Page_Load事件的初始值,而不是我更改文本框上的值之后的值

public partial class Settings : System.Web.UI.Page
{
    string conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;    

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtHostName.Text =  dr["CompanyAUserName"].ToString();
            txtHostPassword.Text = dr["CompanyAPassword"].ToString();
            txtHostEmail.Text = dr["CompanyAEmail"].ToString();
            txtClientName.Text = dr["CompanyBUserName"].ToString();
            txtClientPassword.Text = dr["CompanyBPassword"].ToString();
            txtAgentAName.Text = dr["AgentAName"].ToString();
            txtAgentBName.Text = dr["AgentBName"].ToString();
            txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
        }
        con.Close();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {        
        SqlConnection con = new SqlConnection(conString);
        con.Open();                
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;        
        try
        {
            cmd.CommandText =
            "UPDATE COMPANY_SETTINGS SET CompanyAUserName = @CompanyAUserName, CompanyAPassword = @CompanyAPassword, CompanyAEmail = @CompanyAEmail, CompanyBUserName = @CompanyBUserName, CompanyBPassword = @CompanyBPassword, AgentAName = @AgentAName, AgentBName = @AgentBName, ItemShortageNotification = @ItemShortageNotification";
            cmd.Parameters.AddWithValue("@CompanyAUserName", txtHostName.Text);
            cmd.Parameters.AddWithValue("@CompanyAPassword", txtHostPassword.Text);
            cmd.Parameters.AddWithValue("@CompanyAEmail", txtHostEmail.Text);
            cmd.Parameters.AddWithValue("@CompanyBUserName", txtClientName.Text);
            cmd.Parameters.AddWithValue("@CompanyBPassword", txtClientPassword.Text);
            cmd.Parameters.AddWithValue("@AgentAName", txtAgentAName.Text);
            cmd.Parameters.AddWithValue("@AgentBName", txtAgentBName.Text);
            cmd.Parameters.AddWithValue("@ItemShortageNotification", txtItemQty.Text.Trim());
            cmd.ExecuteNonQuery();
            Response.Redirect("~/Settings.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error: " + ex.Message + "')</script>");            
        }        
        con.Close();        
    }
}
公共部分类设置:System.Web.UI.Page
{
字符串构造=System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”]。ConnectionString;
受保护的无效页面加载(对象发送方、事件参数e)
{
SqlConnection con=新的SqlConnection(构造);
con.Open();
SqlCommand cmd=new SqlCommand(“从公司设置中选择公司用户名、公司密码、公司电子邮件、公司用户名、公司密码、代理名称、代理名称、项目短代理通知”,con);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
txtHostName.Text=dr[“CompanyUserName”].ToString();
txtHostPassword.Text=dr[“公司密码”].ToString();
txtHostEmail.Text=dr[“CompanyEmail”].ToString();
txtClientName.Text=dr[“CompanyUserName”].ToString();
txtClientPassword.Text=dr[“companypassword”].ToString();
txtagentName.Text=dr[“agentName”].ToString();
txtAgentBName.Text=dr[“AgentBName”].ToString();
txtItemQty.Text=dr[“ItemShortAgentation”].ToString();
}
con.Close();
}
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{        
SqlConnection con=新的SqlConnection(构造);
con.Open();
SqlCommand cmd=新的SqlCommand();
cmd.Connection=con;
尝试
{
cmd.CommandText=
“更新公司设置设置CompanyUserName=@CompanyUserName,CompanyPassword=@CompanyPassword,CompanyEmail=@CompanyEmail,CompanyUserName=@CompanyUserName,CompanyPassword=@CompanyPassword,AgentName=@AgentName,AgentName=@AgentName,ItemShortAgentNotification=@ItemShortAgentNotification”;
cmd.Parameters.AddWithValue(“@CompanyAUserName”,txtHostName.Text);
cmd.Parameters.AddWithValue(“@CompanyAPassword”,txtHostPassword.Text);
cmd.Parameters.AddWithValue(“@companyaeil”,txtHostEmail.Text);
cmd.Parameters.AddWithValue(“@companyUserName”,txtClientName.Text);
cmd.Parameters.AddWithValue(“@companypassword”,txtClientPassword.Text);
cmd.Parameters.AddWithValue(“@agentName”,txtagentName.Text);
cmd.Parameters.AddWithValue(“@AgentBName”,txtAgentBName.Text);
cmd.Parameters.AddWithValue(“@ItemShortageNotification”,txtItemQty.Text.Trim());
cmd.ExecuteNonQuery();
Response.Redirect(“~/Settings.aspx”);
}
捕获(例外情况除外)
{
响应。写入(“警报('错误:“+ex.Message+”)”);
}        
con.Close();
}
}
下面是.aspx页面的部分代码

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" Runat="Server">
    <div style="height:100%;" runat="server" id="divSettings">        
        <h3 style="padding:0; margin:0">Settings</h3>
        <h5 style="padding: 5px">(This page is only visible to the host)</h5>
        <table class="SignUpTable" runat="server">
            <tr runat="server">
                <td align="center" colspan="2" runat="server">
                    <h1>Company A Information</h1>
                    <p>This is host account infromation. </p>
                </td>
            </tr>
            <tr runat="server">
                <td class="label" runat="server" >
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserNameLabel">User Name: </asp:Label><br />
                    <div class="small"> Minimun of 4 characters </div>
                </td>
                <td runat="server">
                    <asp:TextBox ID="txtHostName" runat="server" CssClass="textBox"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                        ControlToValidate="txtHostName" Text="Host name is required." 
                        ToolTip="Enter Host Name" Display="Dynamic" 
                        ValidationGroup="ValidationSettings" SetFocusOnError="True"/>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" SetFocusOnError="True"
                            runat="server" ControlToValidate="txtHostName" Display="Dynamic" 
                        ValidationExpression="[0-9a-zA-Z]{4,}" ValidationGroup="ValidationSettings">Host name must be more than 4 characters.</asp:RegularExpressionValidator>
                </td>
            </tr> 

设置
(此页面仅对主机可见)
A公司信息
这是主机帐户信息

用户名:
最少4个字符
主机名必须超过4个字符。
我尝试使用FindControl(),但仍然得到相同的值。
请告诉我,你知道我错过了什么。谢谢。

您不应该在每次回发时都从数据库加载,只有在
的情况下才可以!Page.IsPostback

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        // do the databinding ....
    }
}
旁注:对于实现
IDisposable
的任何内容,尤其是
连接,都应该使用。您不需要关闭它,即使在
dispose
中出现异常,它也会关闭

using(SqlConnection con = new SqlConnection(conString))
{
    con.Open();
    using(SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con))
    {
        // ...
    }
}

您不应该在每次回发时都从数据库加载,但只有在
的情况下!Page.IsPostback

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        // do the databinding ....
    }
}
旁注:对于实现
IDisposable
的任何内容,尤其是
连接,都应该使用。您不需要关闭它,即使在
dispose
中出现异常,它也会关闭

using(SqlConnection con = new SqlConnection(conString))
{
    con.Open();
    using(SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con))
    {
        // ...
    }
}

更改代码以将支票添加到
IsPostBack
。发生的情况是,您正在btn_Save事件触发之前加载数据

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
       loadData();
}

private void loadData()
{

    SqlConnection con = new SqlConnection(conString);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        txtHostName.Text =  dr["CompanyAUserName"].ToString();
        txtHostPassword.Text = dr["CompanyAPassword"].ToString();
        txtHostEmail.Text = dr["CompanyAEmail"].ToString();
        txtClientName.Text = dr["CompanyBUserName"].ToString();
        txtClientPassword.Text = dr["CompanyBPassword"].ToString();
        txtAgentAName.Text = dr["AgentAName"].ToString();
        txtAgentBName.Text = dr["AgentBName"].ToString();
        txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
    }
    con.Close();
}

更改代码以将支票添加到
IsPostBack
。发生的情况是,您正在btn_Save事件触发之前加载数据

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
       loadData();
}

private void loadData()
{

    SqlConnection con = new SqlConnection(conString);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        txtHostName.Text =  dr["CompanyAUserName"].ToString();
        txtHostPassword.Text = dr["CompanyAPassword"].ToString();
        txtHostEmail.Text = dr["CompanyAEmail"].ToString();
        txtClientName.Text = dr["CompanyBUserName"].ToString();
        txtClientPassword.Text = dr["CompanyBPassword"].ToString();
        txtAgentAName.Text = dr["AgentAName"].ToString();
        txtAgentBName.Text = dr["AgentBName"].ToString();
        txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
    }
    con.Close();
}

您需要添加回发检查:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword,         CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName,         ItemShortageNotification FROM COMPANY_SETTINGS", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtHostName.Text =  dr["CompanyAUserName"].ToString();
            txtHostPassword.Text = dr["CompanyAPassword"].ToString();
            txtHostEmail.Text = dr["CompanyAEmail"].ToString();
            txtClientName.Text = dr["CompanyBUserName"].ToString();
            txtClientPassword.Text = dr["CompanyBPassword"].ToString();
            txtAgentAName.Text = dr["AgentAName"].ToString();
            txtAgentBName.Text = dr["AgentBName"].ToString();
            txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
        }
        con.Close();
    }
}

您需要添加回发检查:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword,         CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName,         ItemShortageNotification FROM COMPANY_SETTINGS", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtHostName.Text =  dr["CompanyAUserName"].ToString();
            txtHostPassword.Text = dr["CompanyAPassword"].ToString();
            txtHostEmail.Text = dr["CompanyAEmail"].ToString();
            txtClientName.Text = dr["CompanyBUserName"].ToString();
            txtClientPassword.Text = dr["CompanyBPassword"].ToString();
            txtAgentAName.Text = dr["AgentAName"].ToString();
            txtAgentBName.Text = dr["AgentBName"].ToString();
            txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
        }
        con.Close();
    }
}

你的错误是我的错!Page.IspostBack


因此,每当您试图保存它时,都会重新填充文本框并获取值。

您的错误是由于!Page.IspostBack

因此,每当您尝试保存它时,都会重新填充文本框并获取值