I have an ASP.net (www.mypage.com/search.aspx
) page which has some dropdownlist (populated from code-behind), a submit button and a repeater control:
<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>
The following code behind:
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
}
}
How can I achieve the following without submitting other buttons in the page:
//rptSearchResult datasource = result from the above store procedure query
//lblCount.Text = rptSearchResult.Items.Count
I also would like the URL of the page to have the query string after the result has been shown:
Example: www.mypage.com/search.aspx?name=any provider&specialty=any specialty&location=any location
I you can Redirect any one who click on btnSearch to current page with some Query Strings. and then in Page_Load you call Search function that reads Query Strings and bind rptSearchResult. I hope you get what I mean...
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
}
}
}