简体   繁体   English

ASP.NET Web窗体更新命令不起作用

[英]ASP.NET Web Form Update Command Not Working

I have a web form that I am using to alter a particular record in a database. 我有一个用于更改数据库中特定记录的Web表单。 The fields are populated by data via the C# code behind using SqlDataReader as follows: 字段使用SqlDataReader通过C#代码通过数据填充,如下所示:

  protected void Page_Load(object sender, EventArgs e)
    {
        string str = "Data Source=My_DB;Initial Catalog=BillingAuths;Integrated Security=True";
        string queryString = 
            "SELECT * FROM [AuthRecords] WHERE [ID] = @RecordID;";

        using (SqlConnection connection =
                   new SqlConnection(str))
        {
            SqlCommand command = new SqlCommand(queryString,connection);
            connection.Open();
            int qRecord = Convert.ToInt32(Request.QueryString["RecordID"]);
            command.Parameters.AddWithValue("@RecordID", qRecord);
            SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
               {
                EmailCntyDate.Text = (reader["EMailCountyDate"].ToString());
                EmailCtyCnct.Text = (reader["EMailCountyContact"].ToString());
                EmailCntyAppDate.Text = (reader["EMailCountyAppDate"].ToString());
               }

This works fine: The fields are populated with the correct values from the DB. 效果很好:使用数据库中的正确值填充字段。 From here, the intent is for the user to edit the information, then activate an UPDATE command by clicking a button. 从这里开始,目的是让用户编辑信息,然后通过单击按钮激活UPDATE命令。 The code for this is in HTML. 此代码是HTML。

<asp:SqlDataSource ID="DB_Auths" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" UpdateCommand="
    UPDATE [dbo].[Records]
    SET [EMailCountyContact]=@uEMailCountyContact,
        [EMailCountyDate]=@uEMailCountyDate,
        [EMailCountyApp]=@uEMailCountyApp
    WHERE [ID] = @RecordID">
  <UpdateParameters>
     <asp:QueryStringParameter Name="RecordID" Type="Int32" DefaultValue="null" QueryStringField="RecordID" />
     <asp:ControlParameter ControlID="EmailCntyDate" Name="uEMailCountyDate" />
     <asp:ControlParameter ControlID="EmailCtyCnct" Name="uEMailCountyContact" />
     <asp:ControlParameter ControlID="EmailCntyApp" Name="uEMailCountyApp" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:Button runat="server" ID="Sumbit" Text="Save" OnClick="Sumbit_Click" />

This is the code-behind activation (in theory) for the UPDATE process: 这是UPDATE过程的代码隐藏激活(理论上):

    protected void Sumbit_Click(object sender, EventArgs e)
    {
        try
        {
            DB_Auths.Update();
            Response.Redirect("~/Default.aspx");
        }
        catch (Exception error)
        {
            Response.Write("WARNING: WRITE TO DATA WAREHOUSE FAILED! Error:" + error);
        }
    }

When I click the button, no error message is returned and I redirect as of the update were successful; 当我单击该按钮时,没有错误消息返回,并且自更新成功以来我已重定向。 however, when I check the database, the record is exactly the same as it was before. 但是,当我检查数据库时,记录与以前完全相同。

What gives? 是什么赋予了?

Wrap your page_load code block w/ this statement: 使用以下语句包装您的page_load代码块:

if (!IsPostBack) { }

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.page.ispostback(v=vs.110).aspx

This prevents code from being executed and overwriting the textbox inputs when the submit button is pressed. 这样可以防止在按下提交按钮时执行代码并覆盖文本框输入。

can you please keep a break point and check what value is passing in the RecordID i guess it may be cleared on the post back. 您能否保留一个断点并检查RecordID中传递的是什么值,我想它可能会在回发时清除。 i'm not sure but please double check it 我不确定,但请仔细检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM