我使用下面的代码。我有2个下拉列表它工作正常。但问题是当我点击更新按钮时,第二个drodownlist值不存储在数据库中。它给出了空值引用未处理的错误。form.aspx.cs
我认为标签cmd.Parameters.AddWithValue("@department", DropDownList2.SelectedItem.ToString());
上的值,但在此它取值为空值
<script type="text/javascript">
$(function() {
//$('#<%=DropDownList2.ClientID %>').attr('disabled', 'disabled');
$('#<%=DropDownList2.ClientID %>').append('<option selected="selected" value="0">Select Post</option>');
$('#<%=DropDownList1.ClientID %>').change(function() {
var country = $('#<%=DropDownList1.ClientID%>').val()
$('#<%=DropDownList2.ClientID %>').removeAttr("disabled");
$.ajax({
type: "POST",
url: "http://localhost:50384/update_add_staff.aspx/BindPosts",
data: "{'country':'" + country + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var j = jQuery.parseJSON(msg.d);
var options;
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
}
$('#<%=DropDownList2.ClientID %>').html(options)
},
error: function(data) {
alert('Something Went Wrong')
}
});
});
$('#<%=DropDownList2.ClientID %>').append('<option selected="selected" value="0">Select Post</option>');
$("#DropDownList2").children("option").is("selected").text()
$('#<%=DropDownList2.ClientID %>').change(function() {
var stateid = $('#<%=DropDownList2.ClientID%>').val()
var myVar =('#DropDownList2').find(':selected').text();
$.ajax({
type: "POST",
url: "http://localhost:50384/update_add_staff.aspx/BindPosts",
data: "{'state':'" + post_id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var j = jQuery.parseJSON(msg.d);
var options;
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
}
$('#<%=DropDownList2.ClientID %>').html(options)
$("option:selected", myVar).text()
},
error: function(data) {
alert('Something Went Wrong')
}
});
})
})
</script>
我的c#代码是
private void Bindcategorydown()
{
String strQuery = "SELECT [dept_no],[department_name] FROM [first].[dbo].[dept_add]";
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "department_name";
DropDownList1.DataValueField = "dept_no";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select Department", "0"));
con.Close();
}
}
}
[WebMethod]
public static string BindPosts(string country)
{
StringWriter builder = new StringWriter();
String strQuery = "SELECT [post_id],[post_name] FROM [first].[dbo].[add_post] where group_id=@dept_no";
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Parameters.AddWithValue("@dept_no", country);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
}
DataTable dt = ds.Tables[0];
builder.WriteLine("[");
if (dt.Rows.Count > 0)
{
builder.WriteLine("{\"optionDisplay\":\"Select Post\",");
builder.WriteLine("\"optionValue\":\"0\"},");
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["post_name"] + "\",");
builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["post_id"] + "\"},");
}
}
else
{
builder.WriteLine("{\"optionDisplay\":\"Select Post\",");
builder.WriteLine("\"optionValue\":\"0\"},");
}
string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
returnjson = returnjson + "]";
return returnjson.Replace("\r", "").Replace("\n", "");
}
并在更新按钮上使用此
cmd.Parameters.AddWithValue("@department", DropDownList2.SelectedItem.ToString());
答案 0 :(得分:0)
cmd.Parameters.AddWithValue("@department", DropDownList2.SelectedItem.Text);
如果问题仍然存在,请向我们发送错误的堆栈跟踪。