Hi RichardSa,
Referring the below articles i have created the sample.
Global.asax
static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Default", "Default", "~/Default.aspx");
routes.MapPageRoute("DisplayBlog", "Blogs/{BlogId}/{Slug}", "~/DisplayBlog.aspx");
}
HTML
Default
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:TextBox ID="txtBlog" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="ae1" runat="server" ServiceMethod="SearchBlogs" ServicePath="Default.aspx"
MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtBlog" FirstRowSelected="false" OnClientItemSelected="OnClientItemSelected">
</cc1:AutoCompleteExtender>
<script type="text/javascript">
function OnClientItemSelected(sender, args) {
var title = args.get_text().replace(".", "");
var blogId = args.get_value();
window.location.href = "/Blogs/" + blogId + "/" + encodeURI(title);
}
</script>
DisplayBlog
<h2><asp:Label ID="lblTitle" runat="server" /></h2>
<hr />
<asp:Label ID="lblBody" runat="server" />
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Code
Default
[WebMethod]
public static List<string> SearchBlogs(string prefixText, int count)
{
List<string> blogs = new List<string>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT [BlogId], [Title], REPLACE([Title], ' ', '-') [SLUG], [Body] FROM [Blogs] WHERE [Title] LIKE @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
blogs.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(
sdr["Title"].ToString(),
sdr["BlogId"].ToString()));
}
}
conn.Close();
return blogs;
}
}
}
DisplayBlog
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (this.Page.RouteData.Values.Count > 0)
{
string articleId = this.Page.RouteData.Values["BlogId"].ToString();
string articleTitle = this.Page.RouteData.Values["Slug"].ToString();
this.PopulateBlog(articleId);
}
}
}
private void PopulateBlog(string blogId)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT [Title], [Body] FROM [Blogs] WHERE [BlogId] = @BlogId", con))
{
cmd.Parameters.AddWithValue("@BlogId", blogId);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
lblTitle.Text = dt.Rows[0]["Title"].ToString();
lblBody.Text = dt.Rows[0]["Body"].ToString();
}
}
}
}
}
Screenshot