首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态TextBoxes不更改值ASP.Net和C#

动态TextBoxes不更改值ASP.Net和C#
EN

Stack Overflow用户
提问于 2013-01-19 05:14:42
回答 2查看 1.8K关注 0票数 0

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

我的ASPX表

代码语言:javascript
复制
<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>

我的C#代码(相关)

代码语言:javascript
复制
        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;
            }
        }
    }

因此,当我运行OnClick时,它确实正确地运行了SQL Command,因为最后一个更新字段正确地复制并更新了SQL Row。我的问题是,所有其他变量都没有设置为新值。

我最近一直在发布关于这个项目的问题,这是我第一次使用ASP

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

感谢任何人的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-19 05:19:25

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

我假设这些值在数据库中不会改变,因为您总是在事件处理程序中更新它们之前将它们加载到Page_Load中。

您只需将其包装在!PostBack-check中:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-01-19 05:21:37

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

代码语言:javascript
复制
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();
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14407752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档