filtering the data with textbox input - c#

How we can filter the results according with the input of of a textbox like Google search.
i.e, If i enter "alaska airlines", then it filtered and showed result according with our input. How it possible. Please help me. thanks in advnce..

If I understand correctly you want some form of autocomplete as the user types in your input box.
To achieve this you should use ajax, and the ASP.Net Ajax Toolkit might be what you are looking for. Check out the sample and docs at http://www.asp.net/ajax/ajaxcontroltoolkit/samples/autocomplete/autocomplete.aspx.
Here's a sample for VS2010 and using ASP.Net Toolkit 4.
Markup
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox runat="server" ID="myTextBox" autocomplete="off" />
<asp:autocompleteextender runat="server" behaviorid="AutoCompleteEx" id="autoComplete1"
targetcontrolid="myTextBox" servicepath="AutoComplete.asmx" servicemethod="GetCompletionList"
minimumprefixlength="2" completioninterval="1000" enablecaching="true" completionsetcount="20"></asp:autocompleteextender>
</div>
</form>
</body>
</html>
AutoComplete.asmx.cs
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
public AutoComplete()
{
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
if (count == 0)
{
count = 10;
}
if (prefixText.Equals("xyz"))
{
return new string[0];
}
Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65, 90);
char c2 = (char)random.Next(97, 122);
char c3 = (char)random.Next(97, 122);
items.Add(prefixText + c1 + c2 + c3);
}
return items.ToArray();
}
}

On search click event bind the grid or any controls where you want to populate the result through the database query using like keyword by passing the textbox value as input parameter

Related

asp.net how render html input radio to postback

I'm using the code below to create radio buttons dynamically to a webpage as each set of data is retrieved from a database by iteration. It works well. The only problem is I need these to PostBack so further data can be retrieved depending on which radio button is clicked. I tried doing this with RadioButton controls but these wouldn't position inside of a table cell like the <span> technique does and they wouldn't create through a <span>.
int count = 0;
if (dataReader.HasRows)
{
testLabel1.Text = "dataReader.HasRows: " + dataReader.HasRows;
while (dataReader.Read())
{
count += 1;
htmlString.Append("<table border = '1'>");
htmlString.Append("<tr>");
htmlString.Append("<td>");
htmlString.Append(dataReader["dateTime"] + "<br />" + "<span><input type='radio' id='rd1'/>SOMTEXT </span>" + dataReader["statistics"]");
htmlString.Append("</td>");
htmlString.Append("</tr>");
htmlString.Append("</table>");
htmlString.Append("<br />");
}
test_populatePlaceHolder.Controls.Add(new Literal { Text = htmlString.ToString() });
dataReader.Close();
dataReader.Dispose();
}
}
}
}
I tried adding runat="server" in <span><input type='radio' id='rd1'runat='server'/>SOMTEXT </span> but it didn't create the radio button. Thanks in advance.
Don't know, how to format code in comment. One more again - if you write runat="Server" in your hand-made html, it does nothing, because this code will not be compiled, you just write html answer manually. On the other hand compilation of declarative language (asp.net, razor or any other) makes javascript, you can do it manually too, but B.Gates kindly agreed to do it for you :-)
Asp.Net design
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<table border ="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:200px">
<asp:Label runat="server" ID="Label1"
/>
</td>
<td style="width:200px" >
<asp:RadioButton ID="RadioButton1" runat="server" Checked="false" Text = '<%# Eval("Text") %>' AutoPostBack ="true" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
C# CodeBehind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int num = 10;
if (Page.IsPostBack)
{
for (int i = 1; i < this.Repeater1.Controls.Count-1; i++)
{
RadioButton r = (RadioButton)this.Repeater1.Controls[i].Controls[3];
if (r.Checked)
{
num=int.Parse(r.Text.Substring(0, r.Text.IndexOf(' ')));
break;
}
}
}
this.Repeater1.DataSource = GetAll(num);
this.Repeater1.DataBind();
}
DataTable GetAll(int count)
{
Random r = new Random();
DataTable dt = new DataTable();
dt.Columns.Add("Text", typeof(string));
DateTime bs = DateTime.Now;
for (int i = 0; i < count; i++)
{
int value = r.Next(3, 10);
dt.Rows.Add(value.ToString() + " rows will be shown");
}
return dt;
}
}
Method GetAll() returns table, having count lines. In each line is text "%i rows will be shown" with random number. Table is bind to data repeater on page load event. If it is postback, number of rows is parsed from text of radiobutton inside repeater row (you can't get it from data table, it is another page, data table is already in GC)

Populating RadComboBox using for loop

Hi I have RadComboBox where I am trying to populate the dropdown options using for loop...
int last=20;
int lastitem;
for (int i=1; i < last; i++)
{
lastitem = i + 1;
categoriesCombo.Items.Add(new RadComboBoxItem(i.ToString()+ "-"+lastitem.ToString()));
}
Problem with this code is that it only displays one value the first one. Please let me know how to fix it so it can populate all the items in drop down in for loop. Thanks
I have tried with your code but not able to reproduce your issue.
Please check my code snippet.
ASPX
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
</telerik:RadAjaxManager>
<telerik:RadComboBox ID="categoriesCombo" runat="server"></telerik:RadComboBox>
</form>
</body>
</html>
ASPX.CS
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int last = 20;
int lastitem;
for (int i = 1; i < last; i++)
{
lastitem = i + 1;
categoriesCombo.Items.Add(new RadComboBoxItem(i.ToString() + "-" + lastitem.ToString()));
}
}
}
OutPut:
Let me if any concern.

How to Update the value of textbox in asp.net using c#?

I have this program in asp.net using C#
.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="first" runat="server">
Text Boxes
<asp:TextBox ID="txtnr" runat="server"></asp:TextBox><br />
<asp:Button ID="btnxt1" runat="server" Text="Next" onclick="btnxt1_Click"/><br />
</div>
<div id="second" runat="server">
<asp:Table ID="tbl" runat="server"></asp:Table>
<asp:Button ID="btnx2" runat="server" Text="Next" onclick="btnx2_Click"/><br />
</div>
<div id="third" runat="server">
<asp:Label ID="lblxx" runat="server" Text=""></asp:Label><br />
</div>
</form>
</body>
</html>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
static int numr = 0;
static TableHeaderCell[] tc2;
static TextBox[] txtb;
protected void Page_Load(object sender, EventArgs e){}
protected void btnxt1_Click(object sender, EventArgs e)
{
numr = int.Parse(txtnr.Text);
TableHeaderRow tr;
tc2 = new TableHeaderCell[numr];
txtb = new TextBox[numr];
for (int i = 0; i < numr; i++)
{
tr = new TableHeaderRow();
tc2[i] = new TableHeaderCell();
txtb[i] = new TextBox();
txtb[i].Text = "w";
tc2[i].Controls.Add(txtb[i]);
tr.Controls.Add(tc2[i]);
tbl.Controls.Add(tr);
}
}
protected void btnx2_Click(object sender, EventArgs e)
{
for (int i = 0; i < numr; i++)
lblxx.Text += txtb[i].Text+"<br/>";
}
}
Program steps:
enter number of text-boxes to appear (say = 4) and click 'Next'
four text-boxes will appear (the program sets the value of textbox = "w",to understand the problem)
user can set other value for the text for the four text boxes (say: 1 , 2 , 3 , 4) then click next
finally, the program print the values of text boxes, but the problem is the program
will print: wwww not "1234" :( ??
How to fix this problem??
The problem is due to the fact that dynamic controls (like yours) do not automatically maintain their state after a post back and you should handle it on your own.
Please follow this link for a very similar question and then this link for the corresponding solution.

string array from c# to javascript example not working

i'm using asp.net web form fx3.5 and i'm trying to get my server-side string array into my javascript. i found a simple example that claims to work but it doesn't for me. temp variable is not recognized in ().Serialize(temp);
Here's the reference article
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ShelterExpress.UserInterface.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="c#">
string[] temp;
int lengthOfTemp;
public string tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp);
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<script runat="server" language="c#">
string[] temp;
int lengthOfTemp;
public string tempJSON;
protected override void OnLoad(EventArgs e)//you have to initialize your temp and tempJSON in a method
{
base.OnLoad(e);
temp = new string[] { "Hi", ",", "ojlovecd" };//Initialize your temp here
tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp);
}
</script>

javascript is not working in mozilla but working in other browers

Technology Used:- Asp.Net 2.0
Code:- See Below
Description:- hello code given below is working fine in i.e. and other but not working in all mozila version.javascript is simple to devide two textbox's value. you can easily understand.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="javascript_test.aspx.cs" Inherits="javascript_test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script type ="text/jscript">
var _txtamount;
var _txtins;
var _txtinsamount;
function test()
{
var temp;
_txtamount = document.getElementById("txtamount");
_txtins = document.getElementById("txtins");
_txtinsamount = document.getElementById("txtinsamount");
if (_txtinsamount.value !='')
{
temp = parseFloat(_txtamount.value) / parseFloat(_txtinsamount.value);
}
else
{
temp = 0
}
_txtins.value = temp;
}
</script>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="myform" runat="server" name="myform">
<div>
<asp:TextBox ID="txtamount" runat="server" ></asp:TextBox>
<asp:TextBox ID="txtinsamount" runat="server" onblur="test();"></asp:TextBox>
<asp:TextBox ID="txtins" runat="server"></asp:TextBox></div>
</form>
</body>
</html>
You're using text/jscript as the <script> type. Use text/javascript instead:
<script type ="text/javascript">
JScript is Microsoft's own version of ECMAScript -- no wonder it works on IE.
Your <script> tag needs to be wrapped in the <head> element or in the <body> element; it can't just be a direct child of <html>.
[edit]more important is your "type" value, as the other answer here mentions.

Categories