首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

dropdownlist绑定数据后,进行插入操作错误

2012-04-26 
dropdownlist绑定数据后,进行插入操作异常高人们求助我有两张表,一张信息表,一张学历表信息表字段为id,nam

dropdownlist绑定数据后,进行插入操作异常
高人们求助

我有两张表,一张信息表,一张学历表
信息表字段为id,name,card,xueli,price
学历表字段为id,code,description

xueli和code相关联

想在网页中实现插入功能,通过dropdownlist进行选择,选择的是description,但是希望插入信息表的是code的信息,但是现在讲dropdownlist绑定后,插入的数据却永远是同一个,求解?

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="add.aspx.cs" Inherits="WebApplication5.add" %>

<!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 id="Head1" runat="server">
  <title>无标题页</title>
  <style type="text/css">
  .style1
  {
  width: 200px;
  height: 76px;
  }
  .style2
  {
  width: 337px;
  height: 76px;
  }
  .style3
  {
  height: 76px;
  }
  .style4
  {
  width: 337px;
  }
  </style>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <div>
  <div style="text-align: center">
  <br />
  <table cellpadding="5" cellspacing="0" style="border-right: silver 1px solid; border-top: silver 1px solid; border-left: silver 1px solid; border-bottom: silver 1px solid; background-color: #ffcc33;" width="800">
  <tr>
  <th align="left" nowrap="nowrap" style="height: 25px;"
  valign="middle">
  &nbsp;</th>
  </tr>
  </table>
  </div>
   
  </div>
  <table border="0" cellpadding="5" style="border-right: silver 1px solid; border-top: silver 1px solid; border-left: silver 1px solid; border-bottom: silver 1px solid;"
  width="800" cellspacing="0">
  <tr>
  <td align="right" nowrap="nowrap" style="width: 200px" valign="middle">
  </td>
  <td align="left" nowrap="nowrap" valign="middle" class="style4">
  &nbsp;</td>
  <td align="left" nowrap="nowrap" valign="middle">
  </td>
  </tr>
  <tr>
  <td nowrap="nowrap" valign="middle" style="width: 200px" align="right">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left" class="style4">
  &nbsp;
  </td>
  <td nowrap="nowrap" valign="middle" align="left">
  <%-- <asp:RequiredFieldValidator ID="rfvUserNameCheck" runat="server" ControlToValidate="txtUserNo"
  ErrorMessage="**请输入用户名" Font-Names="幼圆" Font-Size="10pt" Display="Dynamic" ValidationGroup="UserName"></asp:RequiredFieldValidator> --%>


  </td>
   
  </tr>
  <tr>
  <td nowrap="nowrap" valign="middle" align="right" class="style1">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left" class="style2">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left" class="style3">
  &nbsp;</td>
   
  </tr>
  <tr>
  <td nowrap="nowrap" valign="middle" style="width: 200px" align="right">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left" class="style4">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left">
  &nbsp;</td>
  </tr>
   
  <tr>
  <td nowrap="nowrap" valign="middle" style="width: 200px" align="right">
  <asp:Label ID="Label7" runat="server" Font-Names="幼圆" Font-Size="10pt" Text="故障范围:"></asp:Label></td>
  <td nowrap="nowrap" valign="middle" align="left" id="t" class="style4">
  <asp:DropDownList ID="ddlxueli" runat="server" Height="19px" Width="145px" 
  onselectedindexchanged="ddlxueli_SelectedIndexChanged">
   
   
   
  </asp:DropDownList>
  </td>
  <td nowrap="nowrap" valign="middle" align="left">
  &nbsp;</td>
  </tr>
  <tr>
  <td nowrap="nowrap" valign="middle" style="width: 200px" align="right">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left" class="style4">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left">
  &nbsp;</td>
  </tr>
  <tr>
  <td nowrap="nowrap" valign="middle" style="width: 200px" align="right">
  </td>
  <td nowrap="nowrap" valign="middle" align="left" class="style4">
  &nbsp;</td>
  <td nowrap="nowrap" valign="middle" align="left">
  </td>
  </tr>
  </table>
  <table cellpadding="5" cellspacing="0" style="border-right: silver 1px solid; border-top: silver 1px solid;
  border-left: silver 1px solid; width: 800px; border-bottom: silver 1px solid">


  <tr>
  <td align="center" style="height: 34px" valign="middle">
  <asp:Button ID="btnRegion" runat="server" onclick="btnRegion_Click" 
  Text="Button" />
  </td>
  <td align="center" style="height: 34px" valign="middle">
  &nbsp;</td>
  <td align="center" style="height: 34px" valign="middle">
  &nbsp;</td>
  </tr>
  </table>
  </div>
  </form>
</body>
</html>



后台代码


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

namespace WebApplication5
{
  public partial class add : System.Web.UI.Page
  {

  string strCon = "Data Source=(local);Database=drop;Uid=sa;Pwd=sa123456";


  protected void Page_Load(object sender, EventArgs e)
  {

  string SqlStr = "SELECT * from xueli";
  DataSet ds = new DataSet();

  SqlConnection conn = new SqlConnection(strCon);
  if (conn.State.ToString() == "Closed") conn.Open();
  SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
  da.Fill(ds, "xueli");
  if (conn.State.ToString() == "Open") conn.Close();

  ddlxueli.DataSource = ds.Tables[0].DefaultView;
  ddlxueli.DataTextField = "description";
  ddlxueli.DataValueField = "code";
  ddlxueli.DataBind();

   
  }

  protected void btnRegion_Click(object sender, EventArgs e)
  {
  // 数据库连接
  SqlConnection conn = new SqlConnection(strCon);
  // 数据库命令
  SqlCommand objCommand = new SqlCommand("", conn);
  // 设置Sql语句



  objCommand.CommandText =
  " INSERT INTO test01 " +
  " (xueli) " +
  " VALUES " +
  " (@xueli) ";
  // 设置Sql语句参数
  objCommand.Parameters.Add("xueli", SqlDbType.VarChar);
   
  // Sql语句参数赋值
  objCommand.Parameters["xueli"].Value = ddlxueli.SelectedValue;
   
  try
  {
  // 打开数据库连接
  if (conn.State == ConnectionState.Closed) conn.Open();
  // 插入数据
  objCommand.ExecuteNonQuery();
  }
  catch (SqlException exp)
  {
  Session["Error"] = exp.Message;
  Response.Redirect("Error.aspx");
  }
  finally
  {
  // 关闭数据库连接
  if (conn.State == ConnectionState.Open) conn.Close();


  }

  Response.Redirect("2.aspx");
  }

  protected void ddlxueli_SelectedIndexChanged(object sender, EventArgs e)
  {
  string SqlStr = "SELECT * from xueli";
  DataSet ds = new DataSet();

  SqlConnection conn = new SqlConnection(strCon);
  if (conn.State.ToString() == "Closed") conn.Open();
  SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
  da.Fill(ds, "xueli");
  if (conn.State.ToString() == "Open") conn.Close();

  ddlxueli.DataSource = ds.Tables[0].DefaultView;
  ddlxueli.DataTextField = "description";
  ddlxueli.DataValueField = "code";
  ddlxueli.DataBind();

  }
  }
}


望各位大哥大姐帮忙看下。谢谢

[解决办法]
使用SelectedIndexChanged事件,需要设置DropDownList的AutoPostBack属性为true

<asp:DropDownList ID="ddlxueli" runat="server" Height="19px" Width="145px" 
onselectedindexchanged="ddlxueli_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>

热点排行