我有一个下拉列表,我希望如果用户没有选择任何id应该在数据库中存储为0,否则如果用户选择了某些内容,则存储该项的id。 代码在这里:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Query = @"SELECT [category_id] FROM [Category_Master]";
ds1 = dl.fetchrecord(Query);
ddl_parent.Items.Insert(0, new ListItem("---none---"));
int i = 0;
while (i < ds1.Tables[0].Rows.Count)
{
flag = 1;
catname = "";
index = 1;
ListItem li = new ListItem();
li.Text = catmapping(ds1.Tables[0].Rows[i]["category_id"].ToString());
li.Value = ds1.Tables[0].Rows[i]["category_id"].ToString();
ddl_parent.Items.Add(li);
i = i + 1;
}
}
lbl_mess.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
Query = @"INSERT INTO [RbmDatabase].[dbo].[Category_Master]
([Category_ParentId]
,[Category_Name]
,[Category_MetaTag]
,[Category_MetaTagKeywords]
,[Category_Description]
,[Category_SortOrder]
,[Category_Status]
,[Category_UpdateDate])
VALUES ('" + dl.QoutesHandel(ddl_parent.SelectedValue.ToString()) + "','" + dl.QoutesHandel(txt_category.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTdesc.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTkey.Text.Trim()) + "','" + dl.QoutesHandel(txt_Desc.Text.Trim()) + "','" + dl.QoutesHandel(txt_order.Text.Trim()) + "','" + dl.QoutesHandel(ddl_status.SelectedIndex.ToString()) + "','" + String.Format("{0:yyyy-MM-dd}", DateTime.Now) + "')";
dl.insertrecord(Query, lbl_mess);
lbl_mess.Text = "Record Inserted Successfull.....!";
}
}
答案 0 :(得分:2)
创建列表项
时应插入“none”值new ListItem("---none---", "0"));