读书人

小弟我用了无刷新联动在插入的时候如

发布时间: 2013-05-02 09:39:29 作者: rapoo

我用了无刷新联动,在插入的时候怎么取值?
本帖最后由 msy33 于 2013-04-23 00:14:25 编辑 '" + shen.SelectedItem.Text + "','" + shi.SelectedValue + "','" + xian.SelectedValue + "'


联动是做出来了。但在插入数据库的时候,第一个值能插入数据为(就是省)。可是取,第二个(市),第三个(县、区)只要一用到SelectedItem.Text,在插入的时候就报错。
if (!IsPostBack) 这个加了。

我用的是AJAx,case来做的。

我第一次用asp.net 做网站,也不太了解JS,有朋友说用DrowDownList按下的时候,用JS给label赋值。但我不会写吗。.

a.aspx代码。
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" EnableEventValidation="false" CodeFile="Mt_Fb.aspx.cs" Inherits="Mt_Fb_Mt_Fb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script language="javascript">
<!--
function SelectProvince()
{
var drp2=document.getElementById("shi");//市县一级
var drp3=document.getElementById("xian");//区一级
for (var i=drp2.options.length;i>=0;i--)
{
drp2.remove(i);
}
for (var i=drp3.options.length;i>=0;i--)
{
drp3.remove(i);
}
var oHttpReq=new ActiveXObject("MSXML2.XMLHTTP");
var oDoc=new ActiveXObject("MSXML2.DOMDocument");
var province=document.getElementById("shen").value;
oHttpReq.open("post","b.aspx?flag=1&province="+province,false);
oHttpReq.setrequestheader("content-type","application/x-www-form-urlencoded");
oHttpReq.send("");
var result=oHttpReq.responseText;
var newOption_0 = document.createElement("OPTION");
newOption_0.text = "-请选择-";
newOption_0.value = '-1';
drp2.options.add(newOption_0);
oDoc.loadXML(result);
//这里要说一下,items1和items2是你在后面程序中得到的一个xml表。对于//NewDataSet/city/name是怎么来的,你可以用alert(result);看一下就行
items1 = oDoc.selectNodes("//NewDataSet/city/name");
items2 = oDoc.selectNodes("//NewDataSet/city/code");



var itemsLength=items1.length;

for(i=0;i<itemsLength;i++)

//将小类的类名和编号赋予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}


function SelectCity()
{
var drp3=document.getElementById("xian");
for (var i=drp3.options.length;i>=0;i--)
{
drp3.remove(i);
}

var oHttpReq=new ActiveXObject("MSXML2.XMLHTTP");
var oDoc=new ActiveXObject("MSXML2.DOMDocument");
var city=document.getElementById("shi").value;
oHttpReq.open("post","b.aspx?flag=2&city="+city,false);
oHttpReq.setrequestheader("content-type","application/x-www-form-urlencoded");
oHttpReq.send("");
var result=oHttpReq.responseText;
var newOption_0 = document.createElement("OPTION");
newOption_0.text = "-请选择-";
newOption_0.value = '-1';
drp3.options.add(newOption_0);
oDoc.loadXML(result);
var items = oDoc.selectNodes("//NewDataSet/area");
for (var item = items.nextNode();item;item = items.nextNode())
{ 
 
 var areaName = item.selectSingleNode("name").nodeTypedValue;
 var areaCode = item.selectSingleNode("code").nodeTypedValue;
var newOption = document.createElement("OPTION"); 
 newOption.text = areaName; 
 newOption.value = areaCode; 
 drp3.options.add(newOption); 
} 
}
-->
</script>
</head>
<body onload="openwin()">
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="shen" runat="server">


</asp:DropDownList> <asp:DropDownList ID="shi" runat="server">
</asp:DropDownList> <asp:DropDownList ID="xian" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>

a.aspx.cs 代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;

public partial class Mt_Fb_Mt_Fb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conna = new SqlConnection("server=.;uid=sa;pwd=m3223261.33;database=ty;");

SqlDataAdapter daa = new SqlDataAdapter("select * from province where right(code,4)='0000'", conna);
DataSet dsa = new DataSet();

daa.Fill(dsa, "Province");
this.shen.DataSource = dsa.Tables["Province"];
this.shen.DataTextField = "name";
this.shen.DataValueField = "code";
this.shen.DataBind();

this.shen.Attributes.Add("onchange", "SelectProvince()");
this.shi.Attributes.Add("onchange", "SelectCity()");



Socut.Data.ExecuteNonQuery("INSERT INTO Mt_Fb(Shen,Shi,Xian) VALUES('" + shen.SelectedItem.Text + "','" + shi.SelectedItem.Text+ "','" + xian.SelectedItem.Text+ "'“)

联动是做出来了。但在插入数据库的时候,第一个值能插入数据为(就是省)。可是取,第二个(市),第三个(县、区)只要一用到SelectedItem.Text,在插入的时候就报错。假如改成
shi.SelectedValue 和 xian.SelectedValue 就是可以插入,但插入的值是空的。



B.aspx.cs 代码

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Mt_Fb_b : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)


{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=m3223261.33;database=ty;");
int flag = Convert.ToInt32(Request.QueryString["flag"].ToString());
if (Request.QueryString["flag"] != null || Request.QueryString["flag"].ToString() != "")
{
switch (flag)
{
case 1:

string strprv = Request.QueryString["province"].ToString();
SqlDataAdapter da = new SqlDataAdapter("select * from city where left(code,2)='" + strprv.Substring(0, 2).ToString() + "' and right(code,2)='00' and right(code,4)<>'0000'", conn);
DataSet ds = new DataSet();

da.Fill(ds, "city");
XmlTextWriter writerCity = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writerCity.Formatting = Formatting.Indented;
writerCity.Indentation = 4;
writerCity.IndentChar = ' ';
ds.WriteXml(writerCity);
writerCity.Flush();
Response.End();
writerCity.Close();
break;
case 2:
string strcity = Request.QueryString["city"].ToString();
SqlDataAdapter da1 = new SqlDataAdapter("select * from area where left(code,4)='" + strcity.Substring(0, 4).ToString() + "' and right(code,2)<>'00'", conn);
DataSet ds1 = new DataSet();



da1.Fill(ds1, "area");
XmlTextWriter writerCity1 = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writerCity1.Formatting = Formatting.Indented;
writerCity1.Indentation = 4;
writerCity1.IndentChar = ' ';
ds1.WriteXml(writerCity1);
writerCity1.Flush();
Response.End();
writerCity1.Close();
break;
}
}
}
}



-->
</script>

</head> DropDownList
[解决办法]
一级下拉可以直接取值,二级\三级 要用Request.Form[shi.UniqueID](shi为控件ID),

读书人网 >asp.net

热点推荐