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
Related
I created a checkbox using the method below:
for( i = 1; i<7; i++)
{
for (j = 1; j < 33; j++)
{
CheckBox a = new CheckBox();
a.Name = "SAT_ID_" + i.ToString() + "_" + j.ToString();
this.Sat_ID_Grid.Children.Add(a);
a.Style = (Style)Application.Current.FindResource("ReadOnlyCheckBox");
Grid.SetRow(a, i );
Grid.SetColumn(a, j );
}
}
Is it possible to reference the checkboxes later using "SAT_ID_X_Y"? I cant seem to find the solution. If not how can I reference them? I need to change the .ischecked state.
Thanks
Just store the references to the controls somewhere instead of their names, e.g. in a List<CheckBox>, then you can access them by index.
Also you actually should not do any of that, use data-templating and data-binding to create controls for data. If done right you just need to change a boolean on your data and the check-box will be checked/unchecked.
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 ;)
i want the code to disable default selection in dropdownlist in asp.net and also on selection of a particular data field the values are displayed in the txtbox below.the dropdown should be filled with system related data like eg c:drive, d ... etc dynamically at run time.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
ok then use this function and cal this function on the databound event of dropdownlist.
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
and call this function in dropdownlist_databound() event.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
Last night when I asked about screen scraping I was given an excellent article link and has got me to this point. I have a few questions however. I will post my code as well as the html source below. I am trying to grab the data between the data tables, and then send the data to an sql table. I have found success in grabbing Description Widget 3.5 ect... Last Modified By Joe however because the 1st 2 /tr also contains img src=/......" alt="00721408" the numbers do not get grabbed. I am stuck as to how to alter the code so that all the data in the table is grabbed. 2nd, What do I need to do next in order to prepare the data to be sent to a sql table. My code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Windows.Forms;
namespace ConsoleApplication1
{
}
class Program
{
static void Main(string[] args)
{
// Load the html document
var webGet = new HtmlWeb();
var doc = webGet.Load("http://localhost");
// Get all tables in the document
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
// Iterate all rows in the first table
HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
// Iterate all columns in this row
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
for (int j = 0; j < cols.Count; ++j)
{
// Get the value of the column and print it
string value = cols[j].InnerText;
Console.WriteLine(value);
}
}
}
}
<table class="data">
<tr><td>Part-Num</td><td width="50"></td><td><img src="/partcode/number/072140" alt="072140"/></td></tr>
<tr><td>Manu-Number</td><td width="50"></td><td><img src="/partcode/manu/00721408" alt="00721408" /></td></tr>
<tr><td>Description</td><td></td><td>Widget 3.5</td></tr>
<tr><td>Manu-Country</td><td></td><td>United States</td></tr>
<tr><td>Last Modified</td><td></td><td>26 Jan 2011, 8:08 PM</td></tr>
<tr><td>Last Modified By</td><td></td><td>
Manu
</td></tr>
</table>
<p>
</body></html>
While fragile something like this would work in your case - basically just including the text content of all image alt attributes:
// Iterate all rows in the first table
HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
// Iterate all columns in this row
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
for (int j = 0; j < cols.Count; ++j)
{
var images = cols[j].SelectNodes("img");
if(images!=null)
foreach (var image in images)
{
if(image.Attributes["alt"]!=null)
Console.WriteLine(image.Attributes["alt"].Value);
}
// Get the value of the column and print it
string value = cols[j].InnerText;
Console.WriteLine(value);
}
}
I'm a litte confused as to what data you're trying to obtain however...
you could try:
SelectNodes("//td[text()='Description']/../child::*[3]")
whose inner text should be "Widget 3.5"
SelectNodes("//td[text()='Manu-Country']/../child::*[3]")
whose inner text should be "United States"
etc. etc.
Btw just as a shameless plug, you should check out : systemhtml.codeplex.com
It's yet another html parser.