vs c# dropdownlist 和textbox 绑定后 对dropdownlist进行选择 为什么textbox出现相对应的??
autopostback=true设置了dropdownlist_SelectedIndexChanged好像没有起反应??代码:publicpartialclassGo...
autopostback =true 设置了 dropdownlist _SelectedIndexChanged 好像没有起反应??
代码:
public partial class GoodsReserve : System.Web.UI.Page
{string strcon = ConfigurationManager.AppSettings["dsn"];
public void Page_Load(object sender, EventArgs e)
{if (!IsPostBack)
{ SqlConnection con = new SqlConnection(strcon);
SqlDataAdapter sda = new SqlDataAdapter("select * from Goods,Store where SGID=GID and SWID=" + Session["uwid"].ToString() + "", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataRow[] rows = ds.Tables[0].Select();
foreach(DataRow row in rows)
{ ListItem newList = new ListItem();
newList.Text=row["GName"].ToString();
newList.Value = row["GID"].ToString();
gname.Items.Add(newList); }
con.Close();
if(gname.Text!="")
{showlist(); }
}
}
public void showlist()
{
//string str = gname.SelectedItem.Value.ToString();
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand com1 = new SqlCommand("select SQuantity from Store where SGID='" +gname.SelectedItem.Value.ToString()+ "'", con);
SqlDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{ quantity.Text = dr1["SQuantity"].ToString(); }
con.Close();
}
protected void gname_SelectedIndexChanged(object sender, EventArgs e)
{ if (gname.Text != "")
{ showlist(); }
}
} 展开
代码:
public partial class GoodsReserve : System.Web.UI.Page
{string strcon = ConfigurationManager.AppSettings["dsn"];
public void Page_Load(object sender, EventArgs e)
{if (!IsPostBack)
{ SqlConnection con = new SqlConnection(strcon);
SqlDataAdapter sda = new SqlDataAdapter("select * from Goods,Store where SGID=GID and SWID=" + Session["uwid"].ToString() + "", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataRow[] rows = ds.Tables[0].Select();
foreach(DataRow row in rows)
{ ListItem newList = new ListItem();
newList.Text=row["GName"].ToString();
newList.Value = row["GID"].ToString();
gname.Items.Add(newList); }
con.Close();
if(gname.Text!="")
{showlist(); }
}
}
public void showlist()
{
//string str = gname.SelectedItem.Value.ToString();
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand com1 = new SqlCommand("select SQuantity from Store where SGID='" +gname.SelectedItem.Value.ToString()+ "'", con);
SqlDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{ quantity.Text = dr1["SQuantity"].ToString(); }
con.Close();
}
protected void gname_SelectedIndexChanged(object sender, EventArgs e)
{ if (gname.Text != "")
{ showlist(); }
}
} 展开
2个回答
展开全部
你的问题:为什么textbox出现相对应的??
这句我是没太看懂。
从代码来看,你是想DropDownList切换之后,TextBox显示数据库中最后一条读取到的数据?如果是的话,我觉得你的代码没有问题。
这句我是没太看懂。
从代码来看,你是想DropDownList切换之后,TextBox显示数据库中最后一条读取到的数据?如果是的话,我觉得你的代码没有问题。
更多追问追答
追问
dropdownlist(gname) 车架 轮胎 刹车
textbox(quantity) 5 10 8
假如 dropdownlist 选择 “车架 ”,对应的textbox 自动显示“5”
dropdownlist 选择 “刹车 ”,对应的textbox 自动显示“8 ”
追答
protected void gname_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
quantity.Text = ddl.SelectedValue;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询