我有一个下拉列表,它产生以下内容:
SiteId 的可能值:
"Select Site"
"1"
"2"
"3"
"4"
请注意,选择站点是默认值:
<asp:DropDownList ID="ddlSite" DataSourceID = "siteDS" runat="server" OnSelectedIndexChanged="ddlSite_SelectedIndexChanged" AutoPostBack="true" DataTextField="SiteName"
DataValueField="SiteId" AppendDataBoundItems="true">
<asp:ListItem>Select Site</asp:ListItem>
</asp:DropDownList>
我有以下查询,但不确定是否有更好的方法
// this checks to see if the value is Select Site or an actual siteid (1,2,3)
int siteID = 0;
int Site;
bool result = Int32.TryParse(ddlSite.SelectedValue, out Site);
if (result)
{
siteID = Site;
}
if (result)
{
NTDS.SelectCommand = "SELECT * FROM [tbl1] where siteId = " + siteID;
}
else
{
NTDS.SelectCommand = "SELECT * FROM [tbl1]";
}
我之所以有 if else 是因为我们如果用户选择:选择站点,我不想执行 SELECT,因为没有值为 Select Site 的 siteId。
有没有更有效的方法来做到这一点?