类似的问题,第三次求救~

wang_jf411 2009-08-19 10:36:55
其实,丫根前台用JS 或者 AJAX写入到服务器控件中的值,在后台不认。

怎么来解决这个问题?
...全文
176 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
shankaipingo 2009-08-19
  • 打赏
  • 举报
回复
:)
wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
兄弟,真是感谢你了 shankaipingo
shankaipingo 2009-08-19
  • 打赏
  • 举报
回复
汗.你是想做二级联动吧.LISTBOX吓我一跳.
MS有1处错误:
1. var o_option = document.createElement("OPTION") ;
o_option.value = v.Tables[0].Rows[i].SmallClassName;
o_option.text = v.Tables[0].Rows[i].SmallClassName;
document.all("select1").add(o_option) ; 这里没有select1控件啊

我试一下
strSmallClassName = this.Request.Form["SmallClass"]是可以取到值滴!!!!


wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
upload.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.OleDb;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default)); //必要的
if (!IsPostBack) BindDc();
}

/// <summary>
/// 数据库连接
/// </summary>
/// <returns></returns>
public OleDbConnection myConn()
{
//string ConnectionStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr"].ToString());//从Web.config文件中获取数据库连接字符串
string ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionStr"]);

OleDbConnection AccessConnection = new OleDbConnection(ConnectionStr);
try
{
AccessConnection.Open();
return AccessConnection;
}
catch
{
throw;
}
}

/// <summary>
/// 获取下级分类
/// </summary>
[AjaxPro.AjaxMethod]
public DataSet getNextClass(string cid)
{
//因为不希望页面可以知道字段名称 所以 as txt,id as vol 如果是sql ser 可以用 =
//页面获取的 列名称 必须跟这个一同样 而且区分大小写 一般都是这个地方容易疏忽了
//所以二级分类没变化
string sql = @"select SmallClassName from Class where lb = '1' and BigClassName = '" + cid + "'";
try
{
return getDs(sql);
}
catch
{
//throw;
return null;
}
}


/// <summary>
/// 返回一个DataSet
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public DataSet getDs(string SQL)
{
OleDbConnection conn = myConn();
DataSet Ds = new DataSet();
OleDbDataAdapter Da = new OleDbDataAdapter(SQL, conn);
try
{
Da.Fill(Ds);
int k = Ds.Tables[0].Rows.Count;
return Ds;
}
catch(Exception ee)
{
string aa = ee.Message;
return null;
//throw;
}

}
/// <summary>
/// //数据绑定
/// </summary>
private void BindDc()
{
//第一个
string sql = @"select BigClassName from Class where lb = '0' order by Id";
BigClass.DataSource = getDs(sql);
BigClass.DataTextField = "BigClassName";
BigClass.DataValueField = "BigClassName";
BigClass.DataBind();
if (BigClass.DataSource != null) BigClass.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'SmallClass');");


//可以先判断 DropDownList.SelectedItem.Value
//第二个
sql = @"select SmallClassName from Class where lb = '1' and BigClassName = '" + BigClass.SelectedItem.Value + "'"; ;
SmallClass.DataSource = getDs(sql);
SmallClass.DataTextField = "SmallClassName";
SmallClass.DataValueField = "SmallClassName";
SmallClass.DataBind();

}


protected void UpFile_Click(object sender, EventArgs e)
{
if (FilePath.Value == "")
{
Response.Write("<SCRIPT language='JavaScript'>alert('请选择上传文件本地路径'); </SCRIPT>");

}
//else if (BigClass.SelectedItem.Text == "")
//{
// Response.Write("<SCRIPT language='JavaScript'>alert('请选择类别1!'); </SCRIPT>");
//}
//else if (SmallClass.SelectedItem.Text == "")
//{
// Response.Write("<SCRIPT language='JavaScript'>alert('请选择类别12!'); </SCRIPT>");
//}
else
{
string strFilePath, strFileName, strFileExe, strFileSize, strUpDate, strBigClassName, strSmallClassName;

string[] strFileArray;
strFileArray = FilePath.PostedFile.FileName.ToString().Split('\\');
strFileName = strFileArray[strFileArray.Length - 1].ToString();//文件名
strFilePath = "upload/" + BigClass.SelectedValue.ToString() + "/" + SmallClass.SelectedValue.ToString() + "/";//上传路径
strFileExe = FilePath.PostedFile.FileName.Substring(FilePath.PostedFile.FileName.LastIndexOf(".")); //文件类型
strBigClassName =BigClass.SelectedItem.Text.ToString();//所属大类
strSmallClassName = SmallClass.SelectedItem.Text.ToString(); // this.SmallClass.DataValueField; //.Attributes("load(this.options[this.selectedIndex].value)"); //SmallClass.SelectedValue; // SmallClass.SelectedItem.Text.ToString();//所属小类


//int aaa = System.Math.Round(double(10)/double(2)),2);

if (FilePath.PostedFile.ContentLength / 1024 < 1024)
{
strFileSize = ((FilePath.PostedFile.ContentLength) / 1024).ToString() + "K";//文件大小
}
else
{
strFileSize = (FilePath.PostedFile.ContentLength / 1024 / 1024).ToString() + "M";//文件大小
}

strUpDate = System.DateTime.Now.ToString();


string SqlStr = "";
string ConnectionStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr"].ToString());//从Web.config文件中获取数据库连接字符串
OleDbConnection AccessConnection = new OleDbConnection(ConnectionStr);
AccessConnection.Open();//打开数据库

SqlStr = "Select * From Files Where FileName='" + strFileName + "' And BigClass = '" + strBigClassName + "' And SmallClass = '" + strSmallClassName + "' And FileExe='" + strFileExe + "' ";
OleDbCommand AccessCommand2 = new OleDbCommand(SqlStr, AccessConnection);
OleDbDataReader AccessReader = AccessCommand2.ExecuteReader();
if (AccessReader.Read())
{//本目录下有同名同类型文件
AccessReader.Close();
AccessCommand2.Dispose();

Response.Write("<SCRIPT language='JavaScript'>alert('目录" + strFilePath + "下已经存在文件:" + strFileName + "');window.location.href='upload.aspx'; </SCRIPT>");
}
else
{
AccessReader.Close();
AccessCommand2.Dispose();

FilePath.PostedFile.SaveAs(Server.MapPath(strFilePath + strFileName));//上传文件


//写入数据库
SqlStr = "Insert Into Files(FileName,FilePath,FileExe,FileSize,FileDate,BigClass,SmallClass) values('" + strFileName + "','" + strFilePath + "','" + strFileExe + "','" + strFileSize + "','" + strUpDate + "','" + strBigClassName + "','" + strSmallClassName + "') ";
OleDbCommand AccessCommand = new OleDbCommand(SqlStr, AccessConnection);
AccessCommand.ExecuteNonQuery();
AccessCommand.Dispose();
AccessConnection.Close();

Response.Write("<SCRIPT language='JavaScript'>alert('成功上传到" + strFilePath + ",文件名为:" + strFileName + "');window.location.href='upload.aspx'; </SCRIPT>");
}
}
}
}
wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
upload.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="_Default" enableEventValidation="false" %>

<!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">
<title>文件上传</title>

<script language="javascript" type="text/javascript">
  <!--
// ACLOUD 常用JS函数
function getBid(s){
return document.getElementById(s);
}
function getBmc(s){
return document.getElementByName(s);
}

//显示分类列表
function showNext(sid,obj)
{
if(sid==null || sid=="" || sid.length<1)
return;
var slt =getBid(obj);
var v = _Default.getNextClass(sid).value; // 类的名称
// alert(v);
//return;
if (v != null)
{
if(v != null && typeof(v) == "object" && v.Tables != null)
{
slt.length = 0;
// slt.options.add(new Option("请选择",0));
//加了个“请选择”主要为了触发onchange事件
// if(obj=="ddl2")
// {
// getBid("ddl3").options.length=0;
// getBid("ddl3").options.add(new Option("请选择",0));
// }
for(var i=0; i<v.Tables[0].Rows.length; i++)
     {
     var txt = v.Tables[0].Rows[i].SmallClassName; //这个地方需要注意区分大小写
       var vol = v.Tables[0].Rows[i].SmallClassName; //跟dataset表的列名称要一致
       slt.options.add(new Option(txt,vol));
      
       var o_option = document.createElement("OPTION") ;
o_option.value = v.Tables[0].Rows[i].SmallClassName;
o_option.text = v.Tables[0].Rows[i].SmallClassName;
document.all("select1").add(o_option) ;
      
       //document.getElementById("select1").optiona.add(new Option(txt,vol));
     }
}
}
return;
}
-->


  </script>

<style>
TABLE {
FONT-SIZE: 12px; COLOR: #333333;
}
TD {
FONT-SIZE: 12px; COLOR: black
}
.TextInput{
border: 1px solid #C0C0C0
}
</style>
</head>
<body>
<body style="padding-top:0px;" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<form id="form1" runat="server">
<div style="margin-top:0px">

<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
<TBODY>
<TR>
<TD style="BORDER-TOP: white 1px solid; BACKGROUND: #f0f0f0; BORDER-LEFT: white 1px solid; CURSOR: default; BORDER-BOTTOM: gray 1px solid" height=24>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR bgColor=#f0f0f0>
<TD style="height: 20px">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD style="style: ; height: 20px;" #d4d0c8? solid border-right: colspan="3"1px>·<B>文件资料列表</B></TD>
</TR></TBODY></TABLE></TD></TR>
<TR>
<TD id=thTdTbl1
style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; BORDER-BOTTOM: 0px; width: 76%;"
vAlign=top colSpan=2 height=450>
<DIV id=thMainDiv style="BACKGROUND: white; HEIGHT: 400px">
<DIV id=divTbl1>
</DIV>
<DIV id=divTbl2>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="90%">
<tr>
<td align="left" height="7" style="font-size: 12px" width="120">
选择文件本地路径:
</td>
<td align="left" height="7" style="font-size: 12px">
<input id="FilePath" runat="server" class="textibput" type="file" />
<font face="宋体">支持上传WORD、PDF、EXCEL、DWG、JPG类型文件 </font>
</td>
</tr>
<tr>
<td align="left" style="font-size: 12px; height: 7px;" width="100">
  文件类别1:
</td>
<td align="left" style="font-size: 12px; height: 7px;">
<asp:DropDownList ID="BigClass" runat="server" Font-Size="12px" Rows="1" Width="112px">

</asp:DropDownList></td>
</tr>
<tr>
<td align="left" style="font-size: 12px; height: 17px; text-align: left;" width="100">
  文件类别2:
</td>
<td align="left" style="font-size: 12px; height: 17px">
<asp:DropDownList ID="SmallClass" runat="server" Font-Size="12px" Rows="1" Width="112px" AppendDataBoundItems="True">



</asp:DropDownList></td>
</tr>
<tr>
<td height="5">
</td>
</tr>
<tr>
<td align="left" style="font-size: 12px; height: 7px;" width="100">
</td>
<td align="left" style="font-size: 12px; height: 7px;">
 
<asp:Button ID="bt_upfile" runat="server" Text="上 传" OnClick="UpFile_Click"/></td>
</tr>
</table>
</DIV>
 
</DIV></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD height=2></TD></TR></TBODY></TABLE>

</div>
</form>
</body>
</body>
</html>
--------------------------------
wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
我是要做两个LISTBOX的联动效果,因为要设置其中一个LISTBOX的autopostback=ture ,这样设置会刷新整个页面,其他的信息都没有了。

所以想到用JS或者AJAX,然后就遇到了这个问题。

求好的解决方案,最好能有CODE(⊙o⊙)哦
txg92 2009-08-19
  • 打赏
  • 举报
回复
你用JS写入值,服务器端执行事件时页面post刷新后又清空了,你后台就取不到了....,这也是执行先后问题
cceon 2009-08-19
  • 打赏
  • 举报
回复
注意页面文件的编码方式,什么GB2312啊,UTF-8啊、ANSI啊、还有服务器版本,都很重要的。
一步步排查
xierfly 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jinglecat 的回复:]
如果你用 js 调价 listbox 的 item ,服务器端从 ListBox 是取不到的!!!

说你的需求,再找方案
[/Quote]

可以变换方式去实现。
zjb211434 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jinglecat 的回复:]
如果你用 js 调价 listbox 的 item ,服务器端从 ListBox 是取不到的!!!

说你的需求,再找方案
[/Quote]

同意
wulechang 2009-08-19
  • 打赏
  • 举报
回复
把管件代码贴上来
Jinglecat 2009-08-19
  • 打赏
  • 举报
回复
如果你用 js 调价 listbox 的 item ,服务器端从 ListBox 是取不到的!!!

说你的需求,再找方案
mngzilin 2009-08-19
  • 打赏
  • 举报
回复
不会吧,你把代码贴上来看看
Pig23 2009-08-19
  • 打赏
  • 举报
回复
get页面时,cs是先于js执行的
但如果是post:首先listbox 要用服务器控件,或者html控件后加runat="server",js写进是可以获取的
Lovely_baby 2009-08-19
  • 打赏
  • 举报
回复
是不是执行的顺序有关系
是不是先执行了后代的代码
你可以后台输出一下 看看 能有结果吗
shankaipingo 2009-08-19
  • 打赏
  • 举报
回复
没明白,可以帖点代码上来吗?我用服务器端控件textbox是可以改的.后台也能认.
wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
选中前面一项,后面的刷新到相应的数据啊~
zhouyanfss 2009-08-19
  • 打赏
  • 举报
回复
这个到没试过,要么你就用HTML控件好了,JS写到INPUT里,后台用request.form("keyname")获取
shankaipingo 2009-08-19
  • 打赏
  • 举报
回复
用javascript写入到LISTBOX的值
你是新增值到listbox还是更改值?
wang_jf411 2009-08-19
  • 打赏
  • 举报
回复
前台:upload.aspx 用javascript写入到LISTBOX的值
后台:upload.aspx.cs 获取不到


加载更多回复(2)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧