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

本文关键字:搜索 net 何使用 存储过程 提交 asp | 更新日期: 2023-09-27 18:26:58

我有一个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

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

I您可以重定向任何单击的人。b使用一些查询字符串搜索到当前页面。然后在Page_Load中调用Search函数,该函数读取Query Strings并绑定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. 像这样为asp:Button添加OnClick处理程序

<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)

早期的lbSearch_Click事件绑定到linkbutton,我建议将其重命名为建议的事件,这样在提交按钮单击时,那些为linkbutton执行的代码块现在将为Search按钮单击执行。