1

我有一个下拉列表,我希望如果用户未选择任何内容,则 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.....!";

   }

}
4

1 回答 1

2

您应该在创建列表项时插入“无”值

new ListItem("---none---", "0"));
于 2012-12-21T12:15:09.757 回答