我在許多文件中有許多下拉列表。我試圖編寫一個函數,在每次頁面加載時只顯示「---請選擇 - 」作爲默認項目 - 在回發後無法選擇它,因此將其添加到數據庫中,也不會添加到列表中作爲新物品,。我不知道如何實現這一點。而是我使用插入(我知道是錯誤的),但我不知道哪個是正確的方式來實現這一點。請指教在下拉列表中顯示不可選的值
ASPX
<asp:DropDownList ID="DropDownListVType" runat="server" DataSourceID="SqlDataSourceVesselType"
DataTextField="Vessel_Type" DataValueField="VType_ID" Width="160px" AutoPostBack="False"
CausesValidation="True" OnDataBound="ddl_DataBound">
CS ....
PublicFunctions pubvar = new PublicFunctions();
protected void ddl_DataBound(object sender, System.EventArgs e)
{
pubvar.ddl_DB(DropDownListVType);
}
公共職能CS
public void ddl_DB(DropDownList d)
{
try
{
d.Items.Insert(0, new ListItem("--- Please Select ---", String.Empty));
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
UPDATE: 公共職能CS
public object checkNull (Control c)
{
try{
if (c.GetType()==typeof(TextBox))
{
TextBox tb = c as TextBox;
if (!string.IsNullOrEmpty(tb.Text))
{
return tb.Text;
}
}
if(c.GetType()==typeof(DropDownList))
{
DropDownList dl = c as DropDownList;
if ((!string.IsNullOrEmpty(c.??
{
return c.???
}
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
return (DBNull.Value);
CALL頁面CS
command.Parameters.AddWithValue("@VType_ID", DropDownListVType.SelectedValue);
爲什麼會插入方法是不正確的?你究竟在哪裏遇到麻煩? – Vogel612
每次頁面發回時,---請選擇---作爲下拉列表中的新項目添加 – New2This
檢查它是否事先存在然後... – Vogel612