如何使用存储过程提交用于搜索的asp.net页面

时间:2014-10-17 16:06:31

标签: c# sql asp.net sql-server repeater

我有一个ASP.net(www.mypage.com/search.aspx)页面,其中包含一些下拉列表(从代码隐藏中填充),一个提交按钮和一个转发器控件:

<asp:Label runat="server" ID="lblCount"></asp:Label>
<asp:DropDownList ClientIDMode="Static" ID="ddlProvider" CssClass="setProvDDStyle" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
<asp:DropDownList ClientIDMode="Static" ID="ddlSpecialty" CssClass="chosen-select setProvDDStyle" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
<asp:DropDownList ClientIDMode="Static" ID="ddlLocation" CssClass="chosen-select setProvDDStyle" runat="server" AppendDataBoundItems="true"></asp:DropDownList>

<asp:Button ID="btnSubmit" runat="server" Text="Search" />

<~- was using the below button to handle the search
<asp:LinkButton ID="lbSearch" ClientIDMode="Static" CssClass="defaultLinks" runat="server" OnClick="lbSearch_Click">Search</asp:LinkButton> -->

<asp:Repeater runat="server" ID="rptSearchResult" ClientIDMode="Static">
    <HeaderTemplate>
        <div style="padding-left: 5%; overflow: hidden; width: 95%;">
            <div style="float: left; width: 20%; overflow: hidden; text-align: center;">
                <img src="<%# Eval("Image").ToString() %>" />
            </div>
            <div style="float: left; width: 78%; overflow: hidden; text-align: left; vertical-align: top;">
                <span><asp:Label ID="lblName" runat="server" ClientIDMode="Static"><%# Eval("Physician Name").ToString() %></asp:Label></span>
                <hr />
                Specialty: <asp:Label id="lblSpec1" runat="server"><%# Eval("Specialty1").ToString() %></asp:Label>, 
            </div>
        </div>
    </HeaderTemplate>
</asp:Repeater>

以下代码:

string cString;
SqlConnection Conn;
string sqlCode = "";
string strGender = "";
int rowCount = 0;
string strProv, strSpec, strLoca, strGend, strInsu, strLang;

protected void Page_Load(object sender, EventArgs e)
{
    cString = ""; //my connection string
    if (!Page.IsPostBack)
    {
        PopulatePhysician();
        PopulateLocation();
        PopulateSpecialty();
        PopulateInsurance();
        PopulateLanguage();
    }
}
public void lbSearch_Click(object sender, EventArgs e)
{
    Conn = new SqlConnection(cString);
    Conn.Open();

    strGender = ddlGender.SelectedItem.Text;

    if (ddlProvider.SelectedItem.Text == "Any Provider")
    {
        strProv = "%";
    }
    else
    {
        strProv = ddlProvider.SelectedItem.Text;
    }
    if (ddlSpecialty.SelectedItem.Text == "Any Specialty")
    {
        strSpec = "%";
    }
    else
    {
        strSpec = ddlSpecialty.SelectedItem.Text;
    }
    if (ddlLocation.SelectedItem.Text == "Any Location")
    {
        strLoca = "%";
    }
    else
    {
        strLoca = ddlLocation.SelectedItem.Text;
    }
    if (ddlGender.SelectedItem.Text == "Any Gender")
    {
        strGend = "%";
    }
    else
    {
        strGend = ddlGender.SelectedItem.Text;
    }
    if (ddlInsurance.SelectedItem.Text == "Any Insurance")
    {
        strInsu = "%";
    }
    else
    {
        strInsu = ddlInsurance.SelectedItem.Text;
    }
    if (ddlLanguage.SelectedItem.Text == "Any Language")
    {
        strLang = "%";
    }
    else
    {
        strLang = ddlLanguage.SelectedItem.Text;
    }

    using (SqlConnection scCon = new SqlConnection(cString))
    {
        using (SqlCommand scCmd = new SqlCommand("searchPhysician", scCon))
        {
            scCmd.CommandType = CommandType.StoredProcedure;

            scCmd.Parameters.Add("@strProvider", SqlDbType.VarChar).Value = strProv;
            scCmd.Parameters.Add("@strSpecialty", SqlDbType.VarChar).Value = strSpec;
            scCmd.Parameters.Add("@strLocation", SqlDbType.VarChar).Value = strLoca;
            scCmd.Parameters.Add("@strGender", SqlDbType.VarChar).Value = strGend;
            scCmd.Parameters.Add("@strInsurance", SqlDbType.VarChar).Value = strInsu;
            scCmd.Parameters.Add("@strLanguage", SqlDbType.VarChar).Value = strLang;

            scCon.Open();
            scCmd.ExecuteNonQuery();

            //rptSearchResult datasource = result from the above store procedure query
            //lblCount.Text = rptSearchResult.Items.Count
        }
    }

如果不在页面中提交其他按钮,我该如何实现以下目标:

    //rptSearchResult datasource = result from the above store procedure query
    //lblCount.Text = rptSearchResult.Items.Count

我还希望在显示结果后页面的URL包含查询字符串:

示例:www.mypage.com/search.aspx?name=any provider&specialty=any specialty&location=any location

2 个答案:

答案 0 :(得分:1)

我可以重定向任何点击 btnSearch 到当前页面的人都有一些查询字符串。然后在 Page_Load 中调用搜索功能,该功能读取查询字符串并绑定 rptSearchResult 。我希望你明白我的意思......

string cString;
SqlConnection Conn;
string sqlCode = "";
string strGender = "";
int rowCount = 0;
string strProv, strSpec, strLoca, strGend, strInsu, strLang;

protected void Page_Load(object sender, EventArgs e)
{
    cString = ""; //my connection string
    if (!Page.IsPostBack)
    {
        PopulatePhysician();
        PopulateLocation();
        PopulateSpecialty();
        PopulateInsurance();
        PopulateLanguage();

        Search();
    }
}

public void lbSearch_Click(object sender, EventArgs e)
{
    Response.Redirect(String.Format("~/search.aspx?name={0}&specialty={1}&location={2}", ddlProvider.SelectedItem.Text, ddlSpecialty.SelectedItem.Text, ddlLocation.SelectedItem.Text));
}

public void Search()
{
    Conn = new SqlConnection(cString);
    Conn.Open();

    strGender = Request.QueryString["Gender"];

    if (Request.QueryString["Provider"] == "Any Provider")
    {
        strProv = "%";
    }
    else
    {
        strProv = Request.QueryString["Provider"];
    }
    if (Request.QueryString["Specialty"] == "Any Specialty")
    {
        strSpec = "%";
    }
    else
    {
        strSpec = Request.QueryString["Specialty"];
    }
    if (Request.QueryString["Location"] == "Any Location")
    {
        strLoca = "%";
    }
    else
    {
        strLoca = Request.QueryString["Location"];
    }
    if (Request.QueryString["Gender"] == "Any Gender")
    {
        strGend = "%";
    }
    else
    {
        strGend = Request.QueryString["Gender"];
    }
    if (Request.QueryString["Insurance"] == "Any Insurance")
    {
        strInsu = "%";
    }
    else
    {
        strInsu = Request.QueryString["Insurance"];
    }
    if (Request.QueryString["Language"] == "Any Language")
    {
        strLang = "%";
    }
    else
    {
        strLang = Request.QueryString["Language"];
    }

    using (SqlConnection scCon = new SqlConnection(cString))
    {
        using (SqlCommand scCmd = new SqlCommand("searchPhysician", scCon))
        {
            scCmd.CommandType = CommandType.StoredProcedure;

            scCmd.Parameters.Add("@strProvider", SqlDbType.VarChar).Value = strProv;
            scCmd.Parameters.Add("@strSpecialty", SqlDbType.VarChar).Value = strSpec;
            scCmd.Parameters.Add("@strLocation", SqlDbType.VarChar).Value = strLoca;
            scCmd.Parameters.Add("@strGender", SqlDbType.VarChar).Value = strGend;
            scCmd.Parameters.Add("@strInsurance", SqlDbType.VarChar).Value = strInsu;
            scCmd.Parameters.Add("@strLanguage", SqlDbType.VarChar).Value = strLang;

            scCon.Open();
            scCmd.ExecuteNonQuery();

            //rptSearchResult datasource = result from the above store procedure query
            //lblCount.Text = rptSearchResult.Items.Count
        }
    }
}

答案 1 :(得分:0)

你需要做两件事。

  1. 为此OnClick添加asp:Button处理程序。
  2. <asp:Button ID="btnSubmit" runat="server" Text="Search" OnClick="btnSubmit_Click" />

    1. public void lbSearch_Click(object sender, EventArgs e)重命名为protected void btnSubmit_Click(object sender, EventArgs e)
    2. 早期lbSearch_Click事件已绑定到linkbutton,我建议将其重命名为建议的事件,以便在提交按钮单击时,那些为linkbutton执行的代码块将现在执行Search按钮点击。