动态文本框不会改变ASP.Net和C#的值

时间:2022-12-02 11:47:42

I'm currently filling text boxes dynamically on page load. Then I'm using an OnClick command to pull the info from said text boxes

我目前正在页面加载时动态填充文本框。然后我使用OnClick命令从所述文本框中提取信息

My ASPX Table

我的ASPX表

<div class="mi_Container" id="Edit">
<div class="mi_Title">
    <asp:Label ID="App_Name_E" runat="server" />
</div>
<hr />
<table class="mi_Inner_Container">
    <tr>
        <td class="mi_Desc">
            Description: <br />
            <asp:TextBox ID="Description_E"  Rows="6" CssClass="mi_Textarea" TextMode="MultiLine" runat="server" />
        </td>
        <td class="mi_Center">
            Upload Test Script:<br />
        </td>
    </tr>
    <tr>
        <td class="mi_Rows">
            Application Owner: <br />
            <asp:TextBox ID="App_Owner_E" runat="server" />
        </td>
        <td class="mi_Center">
            <div class="mi_Img_Icons">
                Migration Status:<br />
                <asp:DropDownList ID="CS_E" runat="server" DataSourceID="Migration_Status_Source" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
            </div>
            <div class="mi_Img_Icons">
                Migration Progress:<br />
                <asp:DropDownList ID="CP_E" runat="server" DataSourceID="Migration_Progress_Source" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
            </div>
        </td>
    </tr>
    <tr>
        <td class="mi_Rows">
            Technical Service Owner: <br />
            <asp:TextBox ID="TSO_E" runat="server" />
        </td>
        <td class="mi_Center">   
            Migration Phase: <br />
            <asp:DropDownList ID="Migration_Phase_E" runat="server" DataSourceID="Migration_Phase_Source" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
        </td>
    </tr>
            <tr class="mi_Rows">
        <td>
            Responsible Manager: <br />
            <asp:TextBox ID="Responsible_Manager_E" runat="server" />
        </td>
        <td class="mi_Center">
            Next Steps: <br />
            <asp:TextBox ID="Next_Steps_E" runat="server" />
        </td>
    </tr>
    <tr class="mi_Rows">
        <td>
            Signed Off by: <br />
            <asp:TextBox ID="Sign_Off_E" runat="server" />
        </td>
        <td class="mi_Center">
            <asp:Button ID="Update" runat="server" Text="Update Application" onclick="Update_Click" />
        </td>
    </tr>
</table>

My C# Code Behind (Relevant)

我的C#代码背后(相关)

        protected void Page_Load(object sender, EventArgs e)
    {
        ((Label)Master.FindControl("titleBar")).Text = "More Information";

        float ID = float.Parse(Request.QueryString["ID"]);
        DataTable tbl = appsql.appSpecific(ID);

        App_Name_E.Text = tbl.Rows[0]["App_Name"].ToString();
        Description_E.Text = tbl.Rows[0]["Description"].ToString();
        App_Owner_E.Text = tbl.Rows[0]["App_Owner"].ToString();
        CS_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Status"].ToString()));
        CP_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Progress"].ToString()));
        TSO_E.Text = tbl.Rows[0]["TSO"].ToString();
        Migration_Phase_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Migration_Phase"].ToString())) + 1;
        Responsible_Manager_E.Text = tbl.Rows[0]["Responsible_Manager"].ToString();
        Next_Steps_E.Text = tbl.Rows[0]["Next_steps"].ToString();
        Sign_Off_E.Text = tbl.Rows[0]["Sign_Off"].ToString();
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        string DESC = Description_E.Text;
        string AO = App_Owner_E.Text;
        float CS = float.Parse(CS_E.SelectedValue);
        float CP = float.Parse(CP_E.SelectedValue);
        string TSO = TSO_E.Text;
        float MP = float.Parse(Migration_Phase_E.SelectedValue);
        string RM = Responsible_Manager_E.Text;
        string NS = Next_Steps_E.Text;
        string SO = Sign_Off_E.Text;


        using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["app_migrationConnectionString"].ConnectionString))
        {

            SqlCommand cmd = new SqlCommand("UPDATE Apps SET Description = @DESC, App_Owner = @AO, Coexistence_Progress = @CP, Coexistence_Status = @CS, TSO = @TSO, Migration_Phase = @MP, Responsible_Manager = @RM, Next_Steps = @NS, Sign_Off = @SO, Last_Update = @LU Where ID = @ID", connection);

            cmd.Parameters.Add("@ID", SqlDbType.Float);
            cmd.Parameters["@ID"].Value = float.Parse(Request.QueryString["ID"]);

            cmd.Parameters.Add("@DESC", SqlDbType.VarChar, -1);
            cmd.Parameters["@DESC"].Value = DESC;

            cmd.Parameters.Add("@AO", SqlDbType.VarChar, -1);
            cmd.Parameters["@AO"].Value = AO;

            cmd.Parameters.Add("@CS", SqlDbType.Float);
            cmd.Parameters["@CS"].Value = CS;

            cmd.Parameters.Add("@CP", SqlDbType.Float);
            cmd.Parameters["@CP"].Value = CP;

            cmd.Parameters.Add("@TSO", SqlDbType.VarChar, -1);
            cmd.Parameters["TSO"].Value = TSO;

            cmd.Parameters.Add("@MP", SqlDbType.VarChar, -1);
            cmd.Parameters["@MP"].Value = MP;

            cmd.Parameters.Add("@RM", SqlDbType.VarChar, -1);
            cmd.Parameters["@RM"].Value = RM;

            cmd.Parameters.Add("@NS", SqlDbType.VarChar, -1);
            cmd.Parameters["@NS"].Value = NS;

            cmd.Parameters.Add("@SO", SqlDbType.VarChar, -1);
            cmd.Parameters["@SO"].Value = SO;

            cmd.Parameters.Add("@LU", SqlDbType.DateTime);
            cmd.Parameters["@LU"].Value = DateTime.Now;
            try
            {
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

So, when I run the OnClick it does run the SQL Command correctly because the last update field copies correctly and updates the SQL Row. My problem is that all of the other variables are not set to the new value.

因此,当我运行OnClick时,它会正确运行SQL命令,因为上一个更新字段正确复制并更新SQL行。我的问题是所有其他变量都没有设置为新值。

I've been posting questions lately all for this one project, which happens to be the first time I've worked with ASP

我最近一直在为这个项目发布问题,这恰好是我第一次使用ASP

I'm assuming it has something to do with how the page is handling the variables. Me just changing the values isn't setting them again.

我假设它与页面处理变量的方式有关。我只是更改值不再设置它们。

Any and all help appreciated.

任何和所有帮助赞赏。

2 个解决方案

#1


2  

My problem is that all of the other variables are not set to the new value.

我的问题是所有其他变量都没有设置为新值。

I assume that the values don't change in database since you're always loading them in Page_Load before they get updated in the event-handler.

我假设数值在数据库中没有变化,因为在事件处理程序中更新它们之前总是在Page_Load中加载它们。

You just have to wrap it in a !PostBack-check:

你只需要把它包装成!PostBack-check:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        ((Label)Master.FindControl("titleBar")).Text = "More Information";

        float ID = float.Parse(Request.QueryString["ID"]);
        DataTable tbl = appsql.appSpecific(ID);

        App_Name_E.Text = tbl.Rows[0]["App_Name"].ToString();
        Description_E.Text = tbl.Rows[0]["Description"].ToString();
        App_Owner_E.Text = tbl.Rows[0]["App_Owner"].ToString();
        CS_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Status"].ToString()));
        CP_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Progress"].ToString()));
        TSO_E.Text = tbl.Rows[0]["TSO"].ToString();
        Migration_Phase_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Migration_Phase"].ToString())) + 1;
        Responsible_Manager_E.Text = tbl.Rows[0]["Responsible_Manager"].ToString();
        Next_Steps_E.Text = tbl.Rows[0]["Next_steps"].ToString();
        Sign_Off_E.Text = tbl.Rows[0]["Sign_Off"].ToString();
    }
}

#2


0  

It looks like your controls may be resetting to the old values in the Page_Load event. Typically, the block of populating the controls is wrapped in if (!IsPostback) { ... } so that they are only populated when the page loads the first time (ie. is not a postback).

看起来您的控件可能会重置为Page_Load事件中的旧值。通常,填充控件的块包含在if(!IsPostback){...}中,以便仅在页面第一次加载时填充它们(即不是回发)。

So, in your case, you would have the following:

那么,在您的情况下,您将拥有以下内容:

protected void Page_Load(object sender, EventArgs e)
{
    ((Label)Master.FindControl("titleBar")).Text = "More Information";

    if (!IsPostback)
    {
        float ID = float.Parse(Request.QueryString["ID"]);
        DataTable tbl = appsql.appSpecific(ID);
        App_Name_E.Text = tbl.Rows[0]["App_Name"].ToString();
        Description_E.Text = tbl.Rows[0]["Description"].ToString();
        App_Owner_E.Text = tbl.Rows[0]["App_Owner"].ToString();
        CS_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Status"].ToString()));
        CP_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Progress"].ToString()));
        TSO_E.Text = tbl.Rows[0]["TSO"].ToString();
        Migration_Phase_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Migration_Phase"].ToString())) + 1;
        Responsible_Manager_E.Text = tbl.Rows[0]["Responsible_Manager"].ToString();
        Next_Steps_E.Text = tbl.Rows[0]["Next_steps"].ToString();
        Sign_Off_E.Text = tbl.Rows[0]["Sign_Off"].ToString();
    }
}

This will stop your controls from being reinitialized each time on Page_Load, which will allow your Update_Click method to pick up the new values and update the database correctly.

这将阻止您的控件每次在Page_Load上重新初始化,这将允许您的Update_Click方法获取新值并正确更新数据库。

#1


2  

My problem is that all of the other variables are not set to the new value.

我的问题是所有其他变量都没有设置为新值。

I assume that the values don't change in database since you're always loading them in Page_Load before they get updated in the event-handler.

我假设数值在数据库中没有变化,因为在事件处理程序中更新它们之前总是在Page_Load中加载它们。

You just have to wrap it in a !PostBack-check:

你只需要把它包装成!PostBack-check:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        ((Label)Master.FindControl("titleBar")).Text = "More Information";

        float ID = float.Parse(Request.QueryString["ID"]);
        DataTable tbl = appsql.appSpecific(ID);

        App_Name_E.Text = tbl.Rows[0]["App_Name"].ToString();
        Description_E.Text = tbl.Rows[0]["Description"].ToString();
        App_Owner_E.Text = tbl.Rows[0]["App_Owner"].ToString();
        CS_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Status"].ToString()));
        CP_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Progress"].ToString()));
        TSO_E.Text = tbl.Rows[0]["TSO"].ToString();
        Migration_Phase_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Migration_Phase"].ToString())) + 1;
        Responsible_Manager_E.Text = tbl.Rows[0]["Responsible_Manager"].ToString();
        Next_Steps_E.Text = tbl.Rows[0]["Next_steps"].ToString();
        Sign_Off_E.Text = tbl.Rows[0]["Sign_Off"].ToString();
    }
}

#2


0  

It looks like your controls may be resetting to the old values in the Page_Load event. Typically, the block of populating the controls is wrapped in if (!IsPostback) { ... } so that they are only populated when the page loads the first time (ie. is not a postback).

看起来您的控件可能会重置为Page_Load事件中的旧值。通常,填充控件的块包含在if(!IsPostback){...}中,以便仅在页面第一次加载时填充它们(即不是回发)。

So, in your case, you would have the following:

那么,在您的情况下,您将拥有以下内容:

protected void Page_Load(object sender, EventArgs e)
{
    ((Label)Master.FindControl("titleBar")).Text = "More Information";

    if (!IsPostback)
    {
        float ID = float.Parse(Request.QueryString["ID"]);
        DataTable tbl = appsql.appSpecific(ID);
        App_Name_E.Text = tbl.Rows[0]["App_Name"].ToString();
        Description_E.Text = tbl.Rows[0]["Description"].ToString();
        App_Owner_E.Text = tbl.Rows[0]["App_Owner"].ToString();
        CS_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Status"].ToString()));
        CP_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Coexistence_Progress"].ToString()));
        TSO_E.Text = tbl.Rows[0]["TSO"].ToString();
        Migration_Phase_E.SelectedIndex = (Convert.ToInt16(tbl.Rows[0]["Migration_Phase"].ToString())) + 1;
        Responsible_Manager_E.Text = tbl.Rows[0]["Responsible_Manager"].ToString();
        Next_Steps_E.Text = tbl.Rows[0]["Next_steps"].ToString();
        Sign_Off_E.Text = tbl.Rows[0]["Sign_Off"].ToString();
    }
}

This will stop your controls from being reinitialized each time on Page_Load, which will allow your Update_Click method to pick up the new values and update the database correctly.

这将阻止您的控件每次在Page_Load上重新初始化,这将允许您的Update_Click方法获取新值并正确更新数据库。