Get HTML Table ID in C# function? - c#

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.

Related

Function to copy row of table to another table using LINQ

I've some data (row) of some table in a db temp that I want to copy on another db (not temp !) but with the same tablename that has the same number, type and order of columns !
So my idea was to create a function to avoid to repeat myself :
public int copyDB(string **tableName**, int[] **id**)
{
for(int i = 0; i < **id**.Count(); i++)
{
var temp = from t in dbtemp.+(**tableName**)+ where t.student_id == **id**[i]);
}
(...)
}
Is it possible to write i function like this? I will just need to call the function where i want to with the correct args.
I'm still looking also how to copy data, I've found some interesting link like this. I still need to try them.
Thanks for your help

how to get values from dynamically generated text boxes?

I am trying to get the values of text box which i generated dynamically on page load and cloned them using jquery....
every text box has a unique id in form of a matrix for eg textboxes of row one have ids textbox11,textbox12,textbox13,textbox14 etc
for row two textbox21,textbox22,textbox23........
is there any way to get the values..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class product_entry : System.Web.UI.Page
{
int count;
protected void Page_Load(object sender, EventArgs e)
{
int i, j;
for (i = 0; i < 1; i++)
{
TableRow tr = new TableRow();
tr.Attributes.Add("class", "tabrow");
for (j = 0; j <= 8; j++)
{
TableCell tc = new TableCell();
if (j == 0)
{
tc.Controls.Add(new LiteralControl("<Button class=remove type=button>-</button>"));
}
if (j == 1)
{
tc.Attributes.Add("class", "sno");
}
if (j == 2 || j == 3 || j == 4 || j == 5 || j == 6 || j == 7 || j == 8)
{
TextBox tb = new TextBox();
tb.Style["width"] = "98%";
tc.Controls.Add(tb);
}
tr.Controls.Add(tc);
}
Table1.Controls.Add(tr);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = TextBox1.Text;
for (int i = 1; i <= count; i++)
{
for (int j = 1; j <= 7; j++)
{
TextBox aa = (TextBox)Pnl.FindControl("textbox" + i + j);
Response.Write(aa.Text);
}
}
}
}
i want to fetch the values of hundreds of text boxes geerated using jquery by using the loop designed above is there any way to do that
you can add a specific css class to all dynamically generated text boxes on page load and by class selector using Jquery you can access all of them:
Use each: i is the postion in the array, obj is the DOM object that you are iterating (can be accessed with the jQuery wrapper $(this) as well).
$('input.SomeClass').each(function(i,obj){
var textboxid = $(this).id;
var textboxValue = $(this).val(); // get text inside text box
});
If an element has a unique ID, you can query that element using the selector format
$('#theExactId').val()
In HTML 4.01, IDs cannot start with a number. If your IDs really are 11, 12, etc you might want/need to prepend at least one alpha character.
If you were trying to get the data on the server side, the variables would be part of the HTTP POST.
To actually get the value you use
$('#theExactId').val()
An ASP.NET Literal doesn't add any markup to the page. Therefore you have to wrap your content in some container so that you can edit it via JavaScript:
ou should also consider refactoring all "hard" references to control ids from within your JavaScript code. In ASP.NET, there's the concept of naming containers that will ensure unique ids throughout the page. So if you have a control called
txtName
in your Content section, the real name of the client (as seen from JavaScript) will be e.g.
ct00_txtName
if using masterpages. The prefix is automatically added by the naming container to make the id unique.
Fortunately, every control has a property (server-side) called ClientID which will reveal the actual id that is available on the client. So if you need to access a control from client-side, make sure you always use ClientID property to get the name.
Like this:
var name = document.getElementById('<% =txtName.ClientID %>');
Make sure that your C# code that initially creates the text boxes and your jQuery that duplicates the tags assign a unique name to each tag. So you have tags like this:
<input name="textbox1" ... />
<input name="textbox2" ... />
<input name="textbox99" ... />
After you submitted the form via postback or Ajax, you can use the Request.Form collection to access the values of the text boxes like so:
foreach (string key in Request.Form.Keys)
{
if(key.StartsWith("textbox"))
{
string currentValue = Request.Form[key];
}
}
If you submit the form using GET, you need to access Request.QueryString

How to read column names of a multicolumn ListView control?

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 ;)

Jquery/Javascript Access Table Row Attribute

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)

separate datatable for each row in db. .NEt

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.

Categories