我不太擅长.net或sql。
问题是我有一个Web表单和一个数据库。该表格将允许用户在文本框和下拉列表中输入信息。下拉列表中的数据将保存在表格中。
因此,我正在从表中读取值,当用户填写表格并从下拉列表中选择所需的选项时,应将文本框中的数据和选定的下拉列表发送回去,以保存在数据库中。
我已经成功地从数据库中读取了该值,并将其显示在下拉列表中,如以下代码所示:
public class state
{
public string stateID { get; set; }
public string stateName { get; set; }
}
[WebMethod]
public static List<state> PopulateDropDownList()
{
DataTable dt = new DataTable();
List<state> objDept = new List<state>();
SqlConnection con = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true");
{
using (SqlCommand cmd = new SqlCommand("SELECT STATE_ID, STATE_Name FROM state", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
objDept.Add(new state
{
stateID = dt.Rows[i]["STATE_ID"].ToString(),
stateName = dt.Rows[i]["STATE_Name"].ToString()
});
}
}
return objDept;
}
}
}
<script src="
http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
//url is the path of our web method (Page name/functionname)
url: "Default.aspx/PopulateDropDownList",
data: "{}",
dataType: "json",
//called on jquery ajax call success
success: function (result) {
$('#ddlstate').empty();
$('#ddlstate').append("<option value='0'>-Select-</option>");
$.each(result.d, function (key, value) {
$("#ddlstate").append($("<option></option>").val(value.stateID).html(value.stateName));
});
},
//called on jquery ajax call failure
error: function ajaxError(result) {
alert(result.status + ' : ' + result.statusText);
}
});
});
</script>
<p>State</p>
<asp:DropDownList ID="ddlstate" runat="server" Width="160px" />
但是,即使我成功调用了要显示在下拉列表中的数据,也无法将所选数据以及文本框中的数据重新插入数据库中。换句话说,数据不会保存到数据库中。
这是我在“单击提交时”插入数据的代码:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Label1.Text = ("**Thanks for entering your information");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection vid = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true"))
{
vid.Open();
using (SqlCommand xp = new SqlCommand("insert into LCF2016 (Fname, Lname, Email, Birthdate, Phone, Address, City, STATE_ID, Zip, Country_ID, Days_Per_Month, Primary_Language, Secondary_Language, Occupation_ID, HearAbout_ID, Other_Skills) Values(@Fname, @Lname, @Email, @Birthdate, @Phone, @Address, @City, @STATE_ID, @Zip, @Country_ID, @Days_Per_Month, @Primary_Language, @Secondary_Language, @Occupation_ID, @HearAbout_ID @Other_Skills)", vid))
{
xp.Parameters.AddWithValue("@Fname", TextBox1.Text);
xp.Parameters.AddWithValue("@Lname", TextBox2.Text);
xp.Parameters.AddWithValue("@Email", TextBox3.Text);
xp.Parameters.AddWithValue("@Birthdate", TextBox4.Text);
xp.Parameters.AddWithValue("@Phone", TextBox5.Text);
xp.Parameters.AddWithValue("@Address", TextBox6.Text);
xp.Parameters.AddWithValue("@City", TextBox7.Text);
xp.Parameters.AddWithValue("@STATE_ID", ddlstate.SelectedValue);
xp.Parameters.AddWithValue("@Zip", TextBox8.Text);
xp.Parameters.AddWithValue("@country_ID", ddlcountry.SelectedValue);
xp.Parameters.AddWithValue("@Days_Per_Month", TextBox10.Text);
xp.Parameters.AddWithValue("@Primary_Language", ddllangp.SelectedValue);
xp.Parameters.AddWithValue("@Secondary_Language", ddllangs.SelectedValue);
xp.Parameters.AddWithValue("@Occupation_ID", ddloccup.SelectedValue);
xp.Parameters.AddWithValue("@HearAbout_ID", ddlhearabout.SelectedValue);
xp.Parameters.AddWithValue("@Other_Skills", TextBox15.Text);
xp.ExecuteNonQuery();
}
}
我得到的错误是
无效的回发或回调参数。使用配置或页面中的<%@页面EnableEventValidation =“ true”%>启用事件验证。为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它们的服务器控件。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据以进行验证。
尝试,通过jQuery
$( "#ddlstate" ).change(function() {
$('[id*=Hiddenfield1]').attr('value', $( "#ddlstate" ).val());
});
身体
<asp:HiddenField ID="Hiddenfield1" runat="server">
代码
xp.Parameters.AddWithValue("@STATE_ID", Hiddenfield1.vlue);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句