本文介绍了以更简化或有效的编码方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想问一下有没有其他好的方法来修改下面的代码? 实际上这是用于过滤3列。 Hi,I would like to ask are there any other good way to revamp the following code?Actually this is for filtering for 3 columns.protected void ddlCat_SelectedIndexChanged(object sender, EventArgs e) { string ddlCat = ((DropDownList)sender).SelectedValue; Session["ddlCat"] = ddlCat; string ddlLoc = (string)Session["ddlLoc"]; string ddlStatus = (string)Session["ddlStatus"]; if (ddlCat == "All") { if (ddlLoc == "All") { if (ddlStatus == "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual"; } if (ddlStatus != "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE status = '" + ddlStatus + "'"; } } if (ddlLoc != "All") { if (ddlStatus == "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE process_loc = '" + ddlLoc + "'"; } if (ddlStatus != "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE process_loc = '" + ddlLoc + "' and status = '" + ddlStatus + "'"; } } } else //ddlCat != "All" { if (ddlLoc != "All") { if (ddlStatus == "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE manual_cat = '" + ddlCat + "' and process_loc = '" + ddlLoc + "' "; } if (ddlStatus != "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE manual_cat = '" + ddlCat + "' and process_loc = '" + ddlLoc + "' and status = '" + ddlStatus + "' "; } } if (ddlLoc == "All") { if (ddlStatus == "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE manual_cat = '" + ddlCat + "'"; } if (ddlStatus != "All") { SqlDataSource1.SelectCommand = "SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE manual_cat = '" + ddlCat + "' and status = '" + ddlStatus + "'"; } } } GridView1.DataBind();} 推荐答案 protected void ddlCat_SelectedIndexChanged(object sender, EventArgs e){ Session["ddlCat"] = ((DropDownList)sender).SelectedValue; string ddlCat = (string)Session["ddlCat"]; string ddlLoc = (string)Session["ddlLoc"]; string ddlStatus = (string)Session["ddlStatus"]; SqlDataSource1.SelectCommand = @"SELECT manual_cat , process_loc , area , doc_no , rev , title , originator , mrc_no , effective, status FROM dbo.TrainningManual WHERE (manual_cat = @ddlCat OR @ddlCat='All') AND(process_loc = @ddlLoc OR @ddlLoc='All') AND(status = @ddlStatus OR @ddlStatus='All')"; SqlDataSource1.SelectParameters.Add("@ddlCat", SqlDbType.VarChar, 80).Value = ddlCat; SqlDataSource1.SelectParameters.Add("@ddlLoc", SqlDbType.VarChar, 80).Value = ddlLoc; SqlDataSource1.SelectParameters.Add("@ddlStatus", SqlDbType.VarChar, 80).Value = ddlStatus; GridView1.DataBind();} 请注意,我还没有测试过,你可能需要改进。 你也可以用声明的方式绑定你的参数,如下所示: Be aware, that I have not tested it, you might need to refine. You can also bind your parameters in declarative way, something like this:<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<% switch (ddlCat)case "All":{ switch (ddlLoc) case "All" { //query+ WHERE status = ''" + ddlStatus + "''"; break; } default: { break; } break;}default:{ break;} 老实说,我认为如果你的情况更容易调试。Honestly though, I think If else in your case is easier to debug. 这篇关于以更简化或有效的编码方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 05:36