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.
Related
I have multiple treeviews in the table layout panel - C# windows application.
Each cell consists of Treeview or dropdown or textbox. I can get the value of textbox and dropdown but I am unable to get the selected node of multiple Treeviews in the table layout panel.
below my code.
int rows;
int column;
rows = tableLayoutPanel1.RowCount;
column = tableLayoutPanel1.ColumnCount;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
Control value = tableLayoutPanel1.GetControlFromPosition(j, i);
//here i got specified treeview but i can't get refernce. getting error
string controlName = value.Controls.Owner.Name;
//here i got error i am unable to get treeview selected text
string seletedvalue = controlName.SelectedNode.Text;
MessageBox.Show(controlName);
}
}
You are getting the control, but you are not checking what type of control it is, and you are not casting it. So it stays as a general control and not a TreeView. And only the TreeView has nodes.
You can also simply the selection of the specific control that you want.
foreach ( TreeView tv in tableLayoutPanel1.Controls.OfType<TreeView>())
{
string seletedvalue = tv.SelectedNode.Text;
MessageBox.Show(tv.Name + " " + seletedvalue);
}
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
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......
Not sure what is the best way to word this, but I am wondering if a dynamic variable name access can be done in C# (3.5).
Here is the code I am currently looking to "smarten up" or make more elegant with a loop.
private void frmFilter_Load(object sender, EventArgs e)
{
chkCategory1.Text = categories[0];
chkCategory2.Text = categories[1];
chkCategory3.Text = categories[2];
chkCategory4.Text = categories[3];
chkCategory5.Text = categories[4];
chkCategory6.Text = categories[5];
chkCategory7.Text = categories[6];
chkCategory8.Text = categories[7];
chkCategory9.Text = categories[8];
chkCategory10.Text = categories[9];
chkCategory11.Text = categories[10];
chkCategory12.Text = categories[11];
}
Is there a way to do something like ("chkCategory" + i.ToString()).Text?
Yes, you can use
Control c = this.Controls.Find("chkCategory" + i.ToString(), true).Single();
(c as textBox).Text = ...;
Add some errorchecking and wrap it in a nice (extension) method.
Edit: It returns Control[] so either a [0] or a .Single() are needed at the end. Added.
for(...)
{
CheckBox c = this.Controls["chkCategory" + i.ToString()] as CheckBox ;
c.Text = categories[i];
}
You can do that with reflection. But don't.
It's more proper to instantiate a list of contols, add them programmatically to your form, and index that.
Sometimes it can help to put your controls into an array or collection as such:
Checkbox[] chkCataegories = new Checkbox[] { chkCategory1, chkCategory2 ... };
for(int i = 0; i < chkCategories.Length; i++)
chkCategories[i].Text = categories[i];
As another approach, you can dynamically create your checkboxes at runtime instead of design time:
for(int i = 0; i < categories.Length; i++)
{
Checkbox chkCategory = new chkCategory { Text = categories[i] };
someContainer.Controls.Add(chkCategory);
}
At least with dynamically created controls, you don't need to modify your GUI or your form code whenever you add new categories.
You don't need dynamic for that. Put chkCategory1 - 12 in an array, and loop through it with a for loop. I would suggest you keep it around in a field and initialize it at form construction time, because chkCategory seems to be related. But if you want a simple example of how to do it in that simple method, then it would be something like this:
private void frmFilter_Load(object sender, EventArgs e)
{
var chkCategories = new [] { chkCategory1, chkCategory2, chkCategory3, .......... };
for(int i = 0 ; i < chkCategories.Length ; i++ )
chkCategoies[i].Text = categories[i];
}
You know more about the application, so you could perhaps avoid writing out all the control names - for instance, if they are placed on a common parent control, then you could find them by going through it's children.
No, but you could do something like this (untested, beware of syntax errors):
private readonly CheckBox[] allMyCheckboxes = new CheckBox[] { chkCategory1, chkCategory2, ... }
Then you just need to do
for (i = 0; i < 12; i++) allMyCheckboxes[i].Text = categories[i];
The "this.Controls["chkCategory" + i.ToString()]" and "this.Controls.Find("chkCategory" + i.ToString(), true)" both do not work... the former informs you that the contents of the [] are not an int and the latter that ControlCollection does not contain a definition for Find.
Use "Control myControl1 = FindControl("TextBox2");" instead.
I needed this form as I was looping through another array, extracting values and using them to populate form fields. Much easier to look for label1, label2, label3, etc.
How can table controls be copied?
table2=table1?
ASP.NET C# Visual Studio 2008 table control.
The reason is that it works for strings. Assume the below strings to be tables.
string full;
string userinput;
full = full + userinput;
Edit: Since answering this question back in June 2010, I have spent a lot of time using jQuery to do this kind of thing on the client. If interested in this approach, look into the jQuery clone() and append(), after(), before() and related methods.
End Edit
No, you can't. Table, like any other control, is a reference type. It means that copying it just copies a reference to the real object instance. Because it doesn't implement System.ICloneable you have to create a new one and then copy properties manually one by one.
I would be curious to know why you are trying to do this, because it doesn't seem to follow any of the best practices that I am familiar with. Could you describe what you are trying to do?
One thing you could do is copy the contents of a table, although this won't copy the other properties such as styles and cell-padding etc:
protected void CopyTable()
{
var clontable= new HtmlTable();
var mytbl = form1.FindControl("mytable") as HtmlTable;
if (mytbl != null)
{
HtmlTableRow myrow;
HtmlTableCell mycell;
for (int i = 0; i < mytbl.Rows.Count - 1; i++)
{
myrow = new HtmlTableRow();
for (int j = 0; j < mytbl.Rows[i].Cells.Count - 1; j++)
{
mycell = new HtmlTableCell();
mycell.InnerHtml = mytbl.Rows[i].Cells[j].InnerHtml;
myrow.Cells.Add(mycell);
}
clontable.Rows.Add(myrow);
}
form1.Controls.Add(clontable);
}
}