-1

我有一个剑道下拉菜单,在选定的 id 值上,我想在按钮单击事件上更新数据库中特定行的数据。但我的 dat 没有更新,旧值一次又一次地显示。请建议我怎么做。我的代码是

<script src="Js/jquery1.7.1.min.js" type="text/javascript"></script>
<script src="CSS/kendo.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
  $(document).ready(function(){
    $("#multi").kendoDropDownList();
    $("#editor").kendoEditor();
 });
</script>
<form id="form1" runat="server">
<div>
   Select Template: <select id="multi" runat="server"></select>
</div>
<br />
<div>
  Template Name: <input type="text" id="txtName" name="Name" runat="server" />
</div>
<div>
Description:<textarea name="comments" id="txtComment" cols="25" rows="5"   
runat="server"></textarea>
<br />
</div>
<div>
Active: <input type="checkbox" id="chkActive" name="vehicle" runat="server" />
</div>
<div id="example" class="k-content">
<textarea id="editor" rows="10" cols="30" style="width:740px;height:440px"   
runat="server"></textarea>
</div>
<div>

<button type="button" id="btnUpdate" runat="server"   onserverclick="    
btnUpdate_onclick" >Save</button>

</div>
</form>
</body>

我的 .cs 代码是

 protected void Page_Load(object sender, EventArgs e)
{


    Bind();
}

private void Bind()
{
    SqlConnection con = new SqlConnection("Data Source=ST0022;Initial 
Catalog=QuickMove_Globe;User ID=sa;Password=good");
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.SelectCommand = new SqlCommand("Select * from Templates", con);
    con.Open();
    da.SelectCommand.ExecuteNonQuery();
    da.Fill(ds);
    con.Close();
    multi.DataTextField = "ID";
    multi.DataValueField = "Name";
    multi.DataValueField = "Description";

    multi.DataSource = ds.Tables[0];
    multi.DataBind();
}

protected void btnUpdate_onclick(object sender, EventArgs e)
{



    SqlConnection con = new SqlConnection(@"Data Source=ST0022;Initial 
Catalog=QuickMove_Globe;Persist Security Info=True;User ID=sa;Password=good");
    SqlCommand cmd= new SqlCommand("Update_Temp", con);

    cmd.CommandType = CommandType.StoredProcedure;
    //passing parameters
    cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(multi.SelectedIndex));
    cmd.Parameters.AddWithValue("@Name",Convert.ToString(txtName.Value));
    cmd.Parameters.AddWithValue("@Description", Convert.ToString(txtComment.Value));
    cmd.Parameters.AddWithValue("@Content", Convert.ToString(editor.Value));
    cmd.Parameters.AddWithValue("@Active", (chkActive.Value).ToString());

    SqlDataAdapter da = new SqlDataAdapter();

    da.UpdateCommand=cmd;
    con.Open();






    cmd.ExecuteNonQuery();
    con.Close();

}
4

2 回答 2

0

您可能想考虑添加一些错误处理,但试试这个。您不需要存储过程中的返回。您需要做的其他事情是整理您的绑定。您将错误的值绑定到下拉列表。值需要是 ID 和 Text,无论您想显示什么。然后在更新 Convert.ToInt32(multi.SelectedIndex) 应该是 SelectedValue 而不是 SelectedIndex。

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

}



        protected void btnUpdate_onclick(object sender, EventArgs e)
    {



        SqlConnection con = new SqlConnection(@"Data Source=ST0022;Initial 
 Catalog=QuickMove_Globe;Persist Security Info=True;User ID=sa;Password=good");
        SqlCommand cmd = new SqlCommand("Update_Temp", con);

        cmd.CommandType = CommandType.StoredProcedure;
        //passing parameters
        cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(multi.SelectedIndex));
        cmd.Parameters.AddWithValue("@Name", Convert.ToString(txtName.Value));
        cmd.Parameters.AddWithValue("@Description", Convert.ToString(txtComment.Value));
        cmd.Parameters.AddWithValue("@Content", Convert.ToString(editor.Value));
        cmd.Parameters.AddWithValue("@Active", (chkActive.Value).ToString());
        con.Open();

        cmd.ExecuteNonQuery();
        con.Close();

        Bind();
    }
于 2012-11-05T09:54:31.423 回答
0

检查下拉列表中的正确值是否将用于代码隐藏方法。您可以更改按钮命令,如下所示:

<asp:Button ID="btnUpdate" runat="server" 
            onclick="btnUpdate_Click" Text="Save" />
于 2012-11-05T09:22:51.003 回答