I need to access two attributes I've assigned to my row in code-behind.
userId and eventId
here is what I have.. and have tried.
function ExportToPbuse()
{
var rowCount = GeneralReport.rows.length;
for (var i = 0; i < rowCount; i++) {
var userId= GeneralTbl.tbodies.row[i]$(this).attr("userid")
var eventId= GeneralTbl.tbodies.row[i]$(this).attr("eventid")
//exportArray add userId and eventId
}
}
this doesn't work... and I've tried a few other ways, but I'm really not all that familiar with jquery so any suggestions would help greatly..thanks :)
In jQuery, $ is an ordinary function (but with an unusual name) that can take a selector or a DOM element.
Therefore, you can write $(GeneralTbl.tbodies.row[i]).attr("userid"). (Assuming that GeneralTbl.tbodies.row[i] is a <tr> DOM element)
Related
Hi I have a record called Tags in a table called Knowledgebase (KB) in my DB. The words in Tags are separated by commas. Also in the KB table is a field called Title.
I need to match the tags field to the Title so I have replaced all the commas in the tags with spaces like so string removeCommas = commas.Replace(",", " ");.
I think I now need to loop around every record in the KB table to find records? I would like to store the number of matches it finds in a variable called suggestions.
Question is what is the best way to implement the for each loop for a query like this? Is the for each loop the best method for this task?
One way is to store the space seperated strings in a List. Now, use Foreach loop for the whole table and in turn a foreach loop for each record to find the match of Title.
I will correct my answer if this is going the wrong direction, but would something like this complete the operation you are attempting?
System.Data.DataTable KB = new System.Data.DataTable();
string[] tags = "some,foo,bar".Split(',');
System.Collections.Generic.List<string> suggestions = new System.Collections.Generic.List<string>();
for (int i = 0; i < tags.Length; i++)
{
for (int j = 0; j < KB.Rows.Count; j++)
{
string row = KB.Rows[j]["Title"].ToString();
if ((row.Contains(tags[i]))
{
suggestions.Add(row);
}
}
}
Also, avoid foreach whenever humanly possible. Check out THIS post to see why.
Yes Linq can help you to retreive the data from your database and then replacing it after a foreach loop
Code Added
Let say you are using EDM and you have a context kb:
public void ReplaceCommaInTag(KbContext kb)
{
var tags = from t in kb.Titles
Select t.Tags;
foreach(Tag tag in tags)
{
tag = tag.Replace(","," ");
}
try
{
kb.SubmitChanges();
}
catch(Exception e)
{
//Display the error.
}
}
I hope it will help you
In HTML page I've table with ID = "week1" and in C# function I'm using this as
week1.Rows.Add(TableCell);
I want to cast string in table ID. Suppose I've string
for(int i=0; i<5; i++)
{
String abc = "week" + i;
/*
How to do cast this string in table ID
like above to add rows and cell in table but above is hardcoded and I want
to make this dynamic.
*/
}
How to cast above string in HTML Table ID ?????
If your tables reside in a panel you can look them up like this. Please note that ofc you will need runat=server for them. I assume you use HtmlTable in your form ()
for (int i = 0; i < 5; i++)
{
var table = (HtmlTable)pnlTables.FindControl("week" + i);
if (table != null)
{
//do stuff with your table
}
}
Make sure your table in your .aspx has runat="server" (<table id="week1" runat="server">), then, in your code behind, you can simply do
week1.ID
or week1.ClientID (for the full ID in your DOM) - whichever one you're wanting.
Your table should have runat="server" attribute.Only then u can access it from code behind.
What is the best way to find the names of the columns of a ListView?
I converted a DataTable to a List using a procedure I found on this forum, but I cannot make it to put the Id column first, especially because not all of my DataTables have a column "Id".
I can search in collection listView.Columns.ToString() but the format I am seeing is:
"ColumnHeader: Text: Id"
which I have to parse to find the proper name "Id".
This does not look like the spirit of C#.
I also tried: listView.SelectedItems[0].SubItems["Id"]
but that does not compile.
Ok Here is the complete code.
The exact problem is that the user selects a row in the listView with Courier Names and Ids, but it could also be Ids and Names, in that order. The fastest way to find the Id of the selected courier would be:
ListViewItem si = listCouriers.SelectedItems[0];
CourierId = si.SubItems["Id"].Text;
but that does not work. The hardcoded way would be this, but I cannot guarantee that some day the wrong column will be used:
ListViewItem si = listCouriers.SelectedItems[0];
CourierId = si.SubItems[1].Text;
Using #HuorSwords method leads to this not-so-simple solution, which works for me, but depends on the reasonable assumption that the order of columns in the ColumnHeaderCollection corresponds to the display on the form:
ListViewItem si = listCouriers.SelectedItems[0];
string CourierId = null;
int icol = 0;
foreach (ColumnHeader header in listCouriers.Columns)
{
if (header.Text == "Id")
{
CourierId = si.SubItems[icol].Text;
break;
}
icol++;
}
As listView.Columns is of type ListView.ColumnHeaderCollection, then it contains ColumnHeader objects.
The ColumnHeader.Text contains the column title, so you can check for concrete column with:
foreach (ColumnHeader header in listView.Columns)
{
if (header.Text == "Id")
{
// Do something...
}
}
I don't know if is the best approach, but you don't need to parse the results to find "Id" value...
UPDATE
Also, have you tried to reference it with the String indexer? > listView.Columns["Id"]
use this code:
private ColumnHeader GetColumn(string Text)
{
for (int i = 0; i < listView1.Columns.Count; i++)
for (int j = 0; j < listView1.Items.Count; j++)
if (listView1.Items[j].SubItems.Count - 1 >= i)
if (listView1.Items[j].SubItems[i].Text == Text)
return listView1.Columns[i];
return null;
}
just give item text to this code and get everything you want of a column.
Enjoy ;)
In my last question I was having problems looping through a list with jQuery. Then we figured this out and it worked perfectly:
public List<Sale> AllSales { get; set; }
for (var i = 0; i < <%= AllSales.Count %>; i++) {
}
I now need to use the values inside the loop so I thought it would be as simple as this :
for (var i = 0; i < <%= AllSales.Count %>; i++) {
var date = <%= AllSales[i].Date %>;
alert(date);
}
When I first tried this, it said "The name 'i' does not exist in the current context
", so I just put 0 instead of i instead of AllSales[0]. Then nothing happens.
What am I missing?
You have javascript loop which you want to iterate on server side list this is not possible. You can use ajax to send data to client side. This is a nice article for using jQuery ajax with csharp.
Assigning the values of your list separated with comma to some hidden field and accessing that hidden field in javascript could be a possible solution. But if you want to use more attributes of your list object then it would be very messy solution. Using ajax is best option.
I have long tables generated by datagrid control that go beyond the page width. I would like to convert that into separate table for each row or definition list where each field name is followed by field value.
How would I do that.
Uses jquery. If you have more than one table you'll need to change it to accommodate that. Also, just appends to the end of the document. If you want it elsewhere, find the element you want to place it after and insert it into the DOM at that point.
$(document).ready(
function() {
var headers = $('tr:first').children();
$('tr:not(:first)').each(
function(i,row) {
var cols = jQuery(row).children();
var dl = jQuery('<dl></dl>');
for (var i=0, len = headers.length; i < len; ++i) {
var dt = jQuery('<dt>');
dt.text( jQuery(headers[i]).text() );
var dd = jQuery('<dd>');
dd.text( jQuery(cols[i]).text() );
dl.append(dt).append(dd);
}
$('body').append(dl);
}
);
$('table').remove();
}
);
Here's a reference:
http://www.mail-archive.com/flexcoders#yahoogroups.com/msg15534.html
The google terms I think you want are "invert datagrid". You'll get lots of hits.