我想获取dropdownlist中选中项的索引值  并把此索引值存入数据库 
我是这样写的
  try
        {
            string toolid = Request.QueryString["toolInfoid"].ToString();
            int developid = 0;
            if (ddlDeveloper.SelectedIndex == 0)
            {
                developid = 1;
            }
            if (ddlDeveloper.SelectedIndex == 1)
            {
                developid = 2;
            }
            if (ddlDeveloper.SelectedIndex == 2)
            {
                developid = 3;
            }
            if (ddlDeveloper.SelectedIndex == 3)
            {
                developid = 4;
            }
            string toolshiBie = this.txtToolsShiBie.Text;
            string developcode = this.txtKaiFaDaiHao.Text;
            string skill = this.txtSkillID.Text;
            DateTime yujitime = Convert.ToDateTime(this.txtYuJi.Text);
            string num = this.txtBanBen.Text; 
            toolInfoDAL td = new toolInfoDAL();
            //td.NewToolInfoUpdate(toolid, i, toolshiBie, developcode, skill, yujitime, num);
            td.NewToolInfoUpdate(toolid, developid, toolshiBie, developcode, skill, yujitime, num);
            Response.Write("SUCCESSFUL!");
        }
        catch (Exception ex)
        {
            LogManager.Error(ex);
            JSHelper.Alert("失败!", Page);
        }感觉这样不太灵活 如果dropdownlist中的项多于4个 就出错了 如果让多于4个项不出错那样改怎么做呢

解决方案 »

  1.   

    你存SelectedIndex做什么?
    真想不明白,
    直接存SelectedValue不就得了.根本不用写if,if,if..了
      

  2.   

    developid = ddlDeveloper.SelectedIndex + 1
    这样不就完了?
      

  3.   

    dropdownlist中的项是从数据库中的类别表中取出的类别名称 而这在信息表中要存储这个类别表的类别编号
      

  4.   

    我就是这个意思,看下边例子吧,哎
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default19.aspx.cs" Inherits="Default19" %><!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            <br />
            <br />
            <br />
           
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
        </form>
    </body>
    </html>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.Xml;public partial class Default19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(int));
                dt.Columns.Add("disp", typeof(string));
                for (int i = 0; i < 10; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = "test" + i.ToString();
                    dt.Rows.Add(dr);
                }
                //实际情况dt从库里取            this.DropDownList1.DataSource = dt;
                this.DropDownList1.DataTextField = "disp";//这个就当是你的类别名了
                this.DropDownList1.DataValueField = "id";//这个就当是你的编号了
                this.DropDownList1.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(this.DropDownList1.SelectedValue);
        }
    }
      

  5.   


    你直接从数据库里把你的数据取出来.然后给dropdownlist的DataTextField属性设置为类别名
    DataValueField属性设置为类别编号,然后直接dropdownlist.SelectedValue取出的就是你当前选中的那个编号
      

  6.   

    晕似乎lovehongyun都说了,我就不重复了。说也差不多。分给他吧
      

  7.   

    dropdownlist.SelectedValue数据库存要存这个值。
      

  8.   

    绑定dropdownlist后,取this.DropDownList1.SelectedValue就可