How can I highlight a word within gridview - c#

How can I highlight the text of a query in the gridview control?

if you want do this client side please follow this steps:
add jQuery reference to your page.add a text input calles txt_Search.
and then use this script:
$(document).ready(function () {
$('#txt_Search').keyup(function () {
searchTable($(this).val());
});
function searchTable(inputVal) {
var table = $('#GridView1');
table.find('tr').each(function (index, row) {
var allCells = $(row).find('td');
if (allCells.length > 0) {
var found = false;
allCells.each(
function (index, td) {
var regExp = new RegExp(inputVal, 'i');
if (regExp.test($(td).text())) {
found = true;
return false;
}});
if (found == true) $(row).show(); else $(row).hide();
}
});
}
});

var gv = document.getElementById("#GridView1");
for (var i = 0; i < gv.all.length; i++) {
var cellValue = grid.rows[i].cells[0].elements[0];
cellValuestyle.background = '#DD00DD';
}

search the text, dress them with a tag like <label>, and don't forget to add highlight style for the labels.

Related

How to get the row index of gridview when a textbox in that row gets focus

Hi I have a gridview wherein I have added textbox dynamically as shown in the code below:
protected void getDateControls()
{
foreach (GridViewRow grow in gdView.Rows)
{
for (int i = 7; i <= gdView.HeaderRow.Cells.Count - 1; i++)
{
string txtName = gdView.HeaderRow.Cells[i].Text;
System.Web.UI.WebControls.TextBox txt = new System.Web.UI.WebControls.TextBox();
txt.ID = txtName;
txt.Width = 25;
txt.Font.Size = 9;
txt.Style.Add("text-align", "Center");
txt.BackColor = Color.Black;
txt.ForeColor = Color.White;
txt.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
txt.AutoPostBack = true;
txt.TextChanged += new System.EventHandler(this.txtName_Changed);
grow.Cells[i].Controls.Add(txt);
}
}
}
Now I have to get the row index when any of these textbox gets focus. Can anyone help me complete the following code. I want to take the row index in a textbox(TextBox1) which is added to the page during design time.
<script type="text/javascript">
$(document).ready(function() {
$(document).on("focus", "input[id*='txtName']", function() {
////help required to get the row index....
});
});
</script>
I am not sure whether this is the right approach.
Thanking you all in anticipation.
You can do this by adding an attribute to the TextBox and getting it in the javascript function. So first change the foreach in order to add the row number.
int rowNumber = 0;
foreach (GridViewRow grow in GridView1.Rows)
{
for (int i = 7; i <= GridView1.HeaderRow.Cells.Count - 1; i++)
{
//rest of code
txt.Attributes.Add("rowNumber", rowNumber.ToString());
}
rowNumber++;
}
And then the javascript function. You may need to adjust it to your exact specifications.
<script type="text/javascript">
$(document).ready(function () {
$('#<%= GridView1.ClientID %> input[type="text"]').focus(function () {
var rowNumber = $(this).attr("rowNumber");
alert(rowNumber);
});
});
</script>

why listbox selected index changed is not firing?

I have written a javascript method to sortlistbox items and it works well in a sense that the item that I type in text box gets highlighted.
But when I click on the highlighted item it dosen't gets selected. Why?
The selectedIndexchanged is not working.
Here is my javascript code:
function SearchList() {
var l = document.getElementById("<%= LBox.ClientID %>");
var tb = document.getElementById("<%= txtDepartments.ClientID %>");
if (tb.value == "") {
ClearSelection(l);
}
else {
for (var i = 0; i < l.options.length; i++) {
if (l.options[i].value.toLowerCase().match(tb.value.toLowerCase())) {
l.options[i].selected = true;
return false;
}
else {
ClearSelection(l);
}
}
}
}
function ClearSelection(l) {
l.selectedIndex = -1;
}
Do this, then call your function inside, you need to reference jquery library :
$(document).ready(function() {
$('#' + '<%= DropdownName.ClientID %>').change(function() {
// Call function here...
});
});

how to display validation summary of an asp.net page using Javascript

I have one asp.net form.
In it i have many fields which are required.
I want to display validation summary of all fields at the end of the form.
I have already checked for valid values of input controls.
Now i only want is Summary.
Here is my code for valid data input
<script language="javascript" type="text/javascript">
function userCheck(uName) {
var uName = document.getElementById(uName);
var letters = /^[A-Za-z0-9]+$/;
if (uName.value.match(letters) && uName.value.length != 0) {
uName.style.border = "2px solid #008A2E";
return true;
}
else {
uName.value = uName.value.replace(/[\W]/g, '');
uName.style.border = "2px solid #ff0000";
uName.focus();
return false;
}
}
</script>
This is just one function for username check.
I have many more to deal with.
Is there any way to display summary from all fields at the last when submit button is pressed ?
below solution is not working.
function ddlcheck(ddlclg) {
var clg = document.getElementById(ddlclg.id);
var clgname = document.getElementById('<%= LblCollegeName.ClientID %>');
clgname.style.display = "block";
clgname.innerHTML = "Selected College : " + clg.options[clg.selectedIndex].value;
clg.style.border = "1px solid #008A2E";
if (clg.options[clg.selectedIndex].value == "Select") {
clgname.style.display = "none";
clg.style.border = "1px solid #ff0000";
validationhtml = validationhtml + "<b>*</b> College" + "</br>";
}
}
above code is for dropdownlist.
function select() {
var count = 0;
var chkSelectAll = document.getElementById('<%= ChkSelectAll.ClientID %>');
var chkList = document.getElementById('<%= chk.ClientID %>').getElementsByTagName("input");
for (var i = 0; i < chkList.length; i++) {
if (chkList[i].checked == true) {
count++;
}
}
if (count == chkList.length)
chkSelectAll.checked = true;
else {
chkSelectAll.checked = false;
}
}
above code is for selected check boxes.
create a div ( errorreport) at required location validation summary give it style as you needed
After that
<script language="javascript" type="text/javascript">
var validationhtml="";
function userCheck(uName) {
var uName = document.getElementById(uName);
var letters = /^[A-Za-z0-9]+$/;
if (uName.value.match(letters) && uName.value.length != 0) {
uName.style.border = "2px solid #008A2E";
return true;
} else {
uName.value = uName.value.replace(/[\W]/g, '');
uName.style.border = "2px solid #ff0000";
uName.focus();
validationhtml=validationhtml +"uname is not correct" ;
return false;
}
}
function validationsummary() {
// if using jquery
$(".errorreport").html(validationhtml);
//for javascript
document.getelementbyid("errorreport").innerHTML = validationhtml;
if(validationhtml == "") {
return true;
} else {
return false;
}
}
</script>
and call validationsummary() on submit button click

CheckBox inside ListView control using ASP.Net 3.5

When CheckBox is unchecked in a ListView i need to get a popup window?
I have make a JS function and just pass id of your list like as
OnClientClick="return GetSelectedCheckBoxInGrid('grdCustomer');"
function GetSelectedCheckBoxInGrid(obj)
{
var con = 'ctl00_ContentPlaceHolder1_' + obj
var Parent = document.getElementById(con);
var TargetChildControl = "chk";
if (Parent==null)
{
return false;
}
var items = Parent.getElementsByTagName("input");
for(var n = 0; n < items.length; ++n)
if(items[n].type == 'checkbox' &&
items[n].id.indexOf(TargetChildControl,0) >= 0 &&
items[n].checked == false)
alert('Hi');return false;)
}
I think this is that
Not too sure about this, but hypothetically, you could give each checkbox a class, eg chkbox, and then have some jquery code to handle a click event:
$('chkbox').click(function() {
alert("here is where you put your popup code");
});
You could use window.open here
$('chkbox').click(function() {
if (! $('#chkbox').is(':checked'))
{
window.open ("http://www.javascript-coder.com","mywindow","status=1");
}
});
or
$('chkbox').click(function() {
if(! $('#chkbox').attr('checked'))
{
window.open ("http://www.javascript-coder.com","mywindow","status=1");
}
});
How to check whether a checkbox is checked in jQuery?

Text box content rendering in a new window using javascript

I have a asp.net textbox(textarea which is in a repeater have single texarea for each record which are read only) I have button (open in new window) when i click on it the content of the text area needs to be rendered in a new window using javascript .
similar to experts exchange.
Sorry I misread the question the first time.
Here is a javascript solution:
function DisplayTextFromRepeater()
{
var text = '';
var repeater = document.getElementById('MyRepeater');
var inputs = repeater.getElementsById('input');
var txtId = 'MyTextBox' //ID of textbox in repeater template
for(var i = 0; i < inputs.length; i++)
{
if(inputs[i].type == 'text')
{
if(inputs[i].id.indexOf(txtId) != -1)
{
text = text + inputs[i].value;
}
}
}
OpenNewWindow(text);
}
function OpenNewWindow(message)
{
var OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars=yes,menubar=no");
OpenWindow.document.write("<html>");
OpenWindow.document.write("<title>Title Goes Here</title>");
OpenWindow.document.write("<body>");
OpenWindow.document.write(message);
OpenWindow.document.write("</body>");
OpenWindow.document.write("</html>");
}

Categories