UPDATE(MySQL)无法与C#和ASP.NET结合使用

时间:2017-04-25 16:57:28

标签: c# jquery mysql asp.net datagridview

所以,我正在使用datagridview,我想从我更新的标签中选择值,但是当我刷新页面时,它只是我以前的代码的值,就像这样atm:

重要说明:我使用jquery将标签转换为文本框,所以我猜问题就在那里。我覆盖标签的值,对更改后的文本

不执行任何操作

foreach工作肯定所以不要看那里。我很高兴这是事先确定的感谢!

protected void Button3_Click(object sender, EventArgs e)
    {
        _controller = new Controller();

        //Variablen
        string afspraak ="";
        string uitleg ="";
        string id="";
        string id1 = "";

        //Values uit uit gridview halen halen
        foreach (GridViewRow row in GridView1.Rows)
        {
            //Label1 = value van afspraak
            afspraak = ((Label)row.FindControl("Label1")).Text;

            //Label2 = value van uitleg
            uitleg = ((Label)row.FindControl("Label2")).Text;

            //Label3 = value van idcategorie
            id = ((Label)row.FindControl("Label3")).Text;

            //Label4 = value van idafspraken
            id1 = ((Label)row.FindControl("Label4")).Text;
        }

        //Methode om record te bewerken
        _controller.RecordUpdatenTblAfspraken(afspraak, uitleg, Convert.ToInt32(id), Convert.ToInt32(id1));

        //Pagina refreshen
        Response.Redirect(Request.RawUrl);

    }

PersistCode:

#region UpdateRecordsAfsprakenAdmin
    public void UpdateRecordsAfsprakenAdmin(string Afspraak, string Uitleg, int IDCategorieën, int IDAfspraken)
    {
        MySqlConnection conn = new MySqlConnection(_connectionstring);
        conn.Open();
        _sqlquery = "UPDATE knowledge_base.tblafspraken SET Afspraak = @afspraak, Uitleg = @uitleg, IDCategorieën=@id WHERE IDAfspraken=@id1";

        MySqlCommand cmd = new MySqlCommand(_sqlquery, conn);
        cmd.Parameters.AddWithValue("@afspraak", Afspraak);
        cmd.Parameters.AddWithValue("@uitleg", Uitleg);
        cmd.Parameters.AddWithValue("@id", IDCategorieën);
        cmd.Parameters.AddWithValue("@id1", IDAfspraken);

        cmd.ExecuteNonQuery();
        conn.Close();
    }

    #endregion

JavaScript的:

$(function () {
    //Loop through all Labels with class 'editable'.
    $(".editable").each(function () {
        //Reference the Label.
        var label = $(this);

        //Add a TextBox next to the Label.
        label.after("<input type = 'text' style = 'display:none' />");

        //Reference the TextBox.
        var textbox = $(this).next();

        //Set the name attribute of the TextBox.
        var id = this.id.split('_')[this.id.split('_').length - 1];
        textbox[0].name = id.replace("lbl", "txt");

        //Assign the value of Label to TextBox.
        textbox.val(label.html());

        //When Label is clicked, hide Label and show TextBox.
        label.click(function () {
            $(this).hide();
            $(this).next().show();
        });

        //When focus is lost from TextBox, hide TextBox and show Label.
        textbox.focusout(function () {
            $(this).hide();
            $(this).prev().html($(this).val());
            $(this).prev().show();
        });
    });
});

1 个答案:

答案 0 :(得分:0)

您需要对here

的解决方案进行一些修改
  1. 由于您的标签正在生成Label1,Label2,Label3
  2. 的ID
    var id = this.id.split('_')[this.id.split('_').length - 1];
            textbox[0].name = id.replace("lbl", "txt");
    

    这不会奏效 你必须改变文本框[0] .name = id.replace(&#34; lbl&#34;,&#34; txt&#34;); to textbox [0] .name = id.replace(&#34; Label&#34;,&#34; txt&#34;);

    1. 您不能拥有多个具有相同ID的元素,您需要添加逻辑来为您的标签设置新ID

    2. 如果您看到该示例,则它已获取文本框并将其值分配给服务器端的标签。您没有这样做,您需要获取文本框值而不是标签值