C# 如何使用存储过程提交asp.net页面进行搜索
我有一个ASP.net(C# 如何使用存储过程提交asp.net页面进行搜索,c#,sql,asp.net,sql-server,repeater,C#,Sql,Asp.net,Sql Server,Repeater,我有一个ASP.net(www.mypage.com/search.aspx)页面,其中有一些dropdownlist(通过代码隐藏填充)、提交按钮和转发器控件: <asp:Label runat="server" ID="lblCount"></asp:Label> <asp:DropDownList ClientIDMode="Static" ID="ddlProvider" CssClass="setProvDDStyle" runat="server" Ap
www.mypage.com/search.aspx
)页面,其中有一些dropdownlist(通过代码隐藏填充)、提交按钮和转发器控件:
<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>
如何在不提交页面中的其他按钮的情况下实现以下目标:
//rptSearchResult datasource = result from the above store procedure query
//lblCount.Text = rptSearchResult.Items.Count
我还希望页面的URL在显示结果后具有查询字符串:
示例:
www.mypage.com/search.aspx?name=any provider&speciality=any speciality&location=any location
I您可以将点击btnSearch的任何人重定向到当前页面,并使用一些查询字符串。然后在页面加载中调用搜索函数,读取查询字符串并绑定搜索结果。我希望你明白我的意思
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
}
}
}
你需要做两件事
asp:Button
添加OnClick
处理程序
public void lbSearch\u Click(对象发送者,事件参数e)
为protected void btnSubmit\u Click(对象发送者,事件参数e)
先前的
lbSearch\u Click
事件绑定到linkbutton
,我建议将其重命名为建议的,以便在单击提交按钮时,为linkbutton
执行的代码块现在将为Search
按钮单击执行。谢谢。我会尝试一下。如果是第一次加载,我不想调用Search()
:有一件事,URL会更改,但DropDownList会将我不想要的所有内容都设置回默认值。另外,只有在单击页面中的搜索时,我才想调用SEARCH()
。第一次:您应该检查querystring,如果没有,那么不要调用SEARCH。对于set DropDownLists,您也应该检查页面加载中的查询字符串,并决定根据查询字符串数据选择一个选项,这样做可以吗<代码>如果(Request.QueryString[“provider”!=”)
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
}
}
}