我想获取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个项不出错那样改怎么做呢
我是这样写的
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个项不出错那样改怎么做呢
真想不明白,
直接存SelectedValue不就得了.根本不用写if,if,if..了
这样不就完了?
<%@ 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);
}
}
你直接从数据库里把你的数据取出来.然后给dropdownlist的DataTextField属性设置为类别名
DataValueField属性设置为类别编号,然后直接dropdownlist.SelectedValue取出的就是你当前选中的那个编号