为什么下拉列表插入“SELECT”将项目列入SQL? [英] Why the dropdown list insert "SELECT" list item into SQL?
问题描述
我已经在asp.net c#web表单中编写了以下代码:
i have written the following code in asp.net c# web form:
<asp:DropDownList ID="ddltype" runat="server" CssClass="dropspace" Style="width: 95%; float: left">
<asp:ListItem >SELECT YOUR GADGET</asp:ListItem>
<asp:ListItem >Laptop</asp:ListItem>
<asp:ListItem >Desktop</asp:ListItem>
<asp:ListItem >Printer</asp:ListItem>
</asp:DropDownList>
问题是当我点击提交按钮时没有从下拉列表中选择任何项目,它将SELECT YOUR GADGET插入到sql数据库中。请帮我解决这个问题。请编写c#代码,以便它不能将第一个列表项值插入数据库。
我尝试过:
the problem is that when i click on submit button without selecting any item from dropdown list it insert "SELECT YOUR GADGET" into the sql database. please help me out from this problem. please write the c# code so that it can not insert first list item value to the data base.
What I have tried:
if(ddltype.selectedValue !="")
{ cmd = new SqlCommand("sp_submitquery", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@gadget", SqlDbType.VarChar).Value = ddltype.SelectedValue;
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Your Query Submitted Sucessfully');", true);
}
if (ddltype.SelectedValue == "")
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Please select your gadget');", true);
}
推荐答案
我认为这将是您的解决方案..
I think this would be your solution..
if(ddltype.selectedindex==0)
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Please select your gadget');", true);
return;
}
if(ddltype.selectedValue !="")
{ cmd = new SqlCommand("sp_submitquery", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@gadget", SqlDbType.VarChar).Value = ddltype.SelectedValue;
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Your Query Submitted Sucessfully');", true);
}
> asp:ListItem > 选择你的GADGET < / asp:ListItem >
<asp:ListItem >SELECT YOUR GADGET</asp:ListItem>
put
Value =0
u也可以使用InitialValue =0
put
Value="0"
u can also validate with a requiredfiled validator using InitialValue="0"
如果将index设置为0.那么您将在保存数据之前获得默认索引值,然后将此索引值检查为0,然后根据需要触发弹出窗口。
If you set index as a 0. So you will get the default index value before saving the data and then you check this index value as a 0 or not and fire the popup whatever you want.
这篇关于为什么下拉列表插入“SELECT”将项目列入SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!