如何在下拉列表中保存所选项目的ID? [英] How can I save the id of selected item in dropdownlist ?
本文介绍了如何在下拉列表中保存所选项目的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个新闻网站,我需要添加新闻文章。如果我添加新闻文章,我需要选择类别。问题是当我保存新闻时,我想保存所选类别的ID
我尝试过:
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
LoadCategory();
}
}
//添加文章按钮
protected void btn_AddArticles_Click(object sender,EventArgs e)
{
SqlConnection cn = new SqlConnection(cs);
cn.Open();
SqlCommand cm = new SqlCommand(insert into tbl_AddNews(title,category_id,details,photo,date)values(@Title,@ Category,@ Details,@ photo,@ Date),cn);
cm.Parameters.AddWithValue(@ Title,txtbox_Title.Text);
cm.Parameters.AddWithValue(@ Category,Select1.Value);
cm.Parameters.AddWithValue(@ Details,txtbox_details.Text);
string strImg = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
cm.Parameters.AddWithValue(@ photo,strImg);
cm.Parameters.AddWithValue(@ Date,DateTime.Now.ToString());
cm.ExecuteNonQuery();
cn.Close();
}
//下拉列表的绑定
private void LoadCategory()
{
try
{
using(SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand (select * from tbl_Category,con);
con.Open();
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(table);
Select1.DataSource = table;
Select1.DataValueField =Category_id;
Select1.DataTextField =Category_Name;
Select1.DataBind();
Select1.Items.Insert(0,new ListItem( - Select Category--,0));
}
}
catch(例外情况)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text =出了点问题! + ex.Message +;
}
解决方案
使用SelectedValue [ ^ ],DropdownList
没有名为的财产价值
Select1.SelectedValue;
Im creating a news website that i need to add news articles. If i add a news article i need to select category. The problem is when i save the news, I want to save the id of the selected category
What I have tried:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadCategory(); } }
//add articles button
protected void btn_AddArticles_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(cs); cn.Open(); SqlCommand cm = new SqlCommand("Insert into tbl_AddNews (title, category_id, details, photo, date) values(@Title, @Category, @Details, @photo, @Date)", cn); cm.Parameters.AddWithValue("@Title", txtbox_Title.Text); cm.Parameters.AddWithValue("@Category", Select1.Value); cm.Parameters.AddWithValue("@Details", txtbox_details.Text); string strImg = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); cm.Parameters.AddWithValue("@photo", strImg); cm.Parameters.AddWithValue("@Date", DateTime.Now.ToString()); cm.ExecuteNonQuery(); cn.Close(); }
//binding of dropdownlist
private void LoadCategory() { try { using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("select * from tbl_Category", con); con.Open(); DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(table); Select1.DataSource = table; Select1.DataValueField = "Category_id"; Select1.DataTextField = "Category_Name"; Select1.DataBind(); Select1.Items.Insert(0, new ListItem("--Select Category--", "0")); } } catch (Exception ex) { Label1.ForeColor = System.Drawing.Color.Red; Label1.Text = "Something went wrong!." + ex.Message + ""; }
解决方案
use SelectedValue [^],DropdownList
doesnt have a property calledValue
Select1.SelectedValue;
这篇关于如何在下拉列表中保存所选项目的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文