Sorry for reposting this but I haven't figure out how to do it.
string str = string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
str += item.Value + "<br><br>";
TextBox txt1 = new TextBox();
form1.Controls.Add(txt1);
}
}
lbl1.Text = str;
This brings up a textbox each time I check a value but I want to bring the textbox near each selected value that I retain in the label. So value1 textbox1, value2 textbox2....It's asp.net web application.
Why do you want to use the same label?
U could do:
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
Label label = new Label();
label.Text = item.Value;
TextBox txt1 = new TextBox();
form1.Controls.Add(label);
form1.Controls.Add(txt1);
}
}
you also might have to assign id to the textboxes if you are planning to retrieve user input from them.
But if you only have limited number of fields u could use static fields, and change its visibility as Tim stated
I don't really know what you plan to do but why dont you define a CSS class for both the label object and the textbox object using the same y- but alternate x-coordinates.
Related
I have 10 text boxes. Like textBox1.Text , textBox2.Text and so on. I need to use textBox2.Text if i=2, textBox2.Text if i =3 and so on.
I did:
string t = "textBox"+i+".Text";
But outcome coming as "textBox1.Text".
How to insert 'i' Value into textBox name instead of 1. And get the outcome as textBox1.Text
So I can use it as name and pass value from textbox to my program.
Based on what I think you are asking, you could store a reference to each TextBox in an array and then use your int to reference it.
TextBox[] boxes = new TextBox[10];
boxes[0] = textBox1;
// ... follow the pattern
boxes[9] = textBox10;
string value = boxes[i-1].Text; // Gets the value of the textBoxi.Text
You can do it like so:
private TextBox GetTB(int i) {
string name = "textBox" + i.ToString();
foreach (var ctrl in Controls) {
var tbox = ctrl as TextBox;
if (tbox?.Name == name) return tbox;
}
return null
}
The easiest way, if you need to access them in C# code this way, would be to maintain an array or List of these textboxes, indexed in order of their numeric value. Then you could simply reference textBoxes[i-1] and get the textbox numbered "i". How you get that array depends on exactly what you're developing. For WinForms, you can use a little Linq on the Form.Controls property:
public static IEnumerable<Control> Flatten(this IEnumerable<Control> controls)
{
var results = new List<Control>();
foreach (var control in controls)
{
results.Add(control);
control.Controls.OfType<Control>().Flatten(results);
}
return results;
}
private static void Flatten(this IEnumerable<Control> controls, List<Control> results)
{
foreach (var control in controls)
{
results.Add(control);
control.Controls.OfType<Control>().Flatten(results);
}
}
...
var textboxes = Form.Controls.Flatten()
.OfType<TextBox>()
.Where(t=>t.Name.StartsWith("textBox"))
.OrderBy(t=>t.Name)
.ToArray();
I have written the code in ASP.NET application like
protected void chkbox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkbox = sender as CheckBox;
GridViewRow currentRow = chkbox.NamingContainer as GridViewRow;
Label lblJurisdiction = grdView.Rows[currentRow.RowIndex].FindControl("lblJurisdiction") as Label;
RequiredFieldValidator rfv = grdView.Rows[currentRow.RowIndex].FindControl("ValReqED") as RequiredFieldValidator;
RequiredFieldValidator rfvExpD = grdView.Rows[currentRow.RowIndex].FindControl("ValReqExpD") as RequiredFieldValidator;
RequiredFieldValidator rfvLN = grdView.Rows[currentRow.RowIndex].FindControl("ValReqLN") as RequiredFieldValidator;
if (!chkbox.Checked)
{
rfv.Enabled = false;
rfvExpD.Enabled = false;
rfvLN.Enabled = false;
string JurisdictionRemoved = lblJurisdiction.Text + ",";
string Jurisdiction = txtJurisdiction.Text;
string ReplacedJurisdiction = Jurisdiction.Replace(JurisdictionRemoved, "");
txtJurisdiction.Text = ReplacedJurisdiction;
}
else
{
string Jurisdiction = txtJurisdiction.Text;
txtJurisdiction.Text = Jurisdiction + "," + lblJurisdiction.Text.ToString();
}
}
Note that string Jurisdictions contains set of Jurisdictions like
Delaware,Georgia,Illinois,Kansas,Maine
And string JurisdictionRemoved contains only one jurisdiction like (where user will uncheck the checkbox)
Georgia
Then txtJurisdicton must contain
Delaware,Illinois,Kansas,Maine
if string JurisdictionRemoved contains "Maine" then above code doesnot works, it is not removed.
Now I want that whenever user will uncheck the checkbox that Jurisdiction should be removed from txtJurisdiction textbox and whenever user will check the checkbox the corresponding jurisdiction should be appended to the txtJurisdiction. Let us suppose that user has checked "Georgia" again
then the textbox should contain
Delaware,Georgia,Illinois,Kansas,Maine
The above code doesn't work like this. Also I want it append checked jurisdiction in the textbox alphabetically and should eliminate the redundant state. Please help me!!!
To overcome this issue you can do like this
split conman separated list to an array using split function
Remove the text from the array using condition
User String.Join method to create new string.
Assign that value to your textbox.
Example
List<String> Items = Jurisdiction.Split(",");
Items.Remove( lblJurisdiction.Text);
string NewX = String.Join(",", Items.ToArray());
I want to capture all the textboxes values on button click in string without explicitly writing each of this line for each textbox in c# like
string atv1 = TextBox1.Text;
string atv2 = TextBox2.Text;
It should find all textboxes and make a string of it (join) .
can anyone help out !!
(window form when making some asp.net website (c#))
concise way :
String.Join(",",Form.Controls.OfType<TextBox>().Select(c => c.Text))
You can use a StringBuilder to "join" (most software types use the term concatenate) the strings. To get all the TextBoxes you can select any control on the form that is a text box. Simple solution is
var s = new StringBuilder();
foreach (var textbox in this.Controls.OfType<TextBox>())
{
s.AppendLine(textbox.Text)
}
Console.WriteLine(s.ToString());
However, a TextBox can be inside of a Control on the Form. So to handle this case you need recursion. I'll leave you to search StackOverflow to figure out how to do this.
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
var txt = (TextBox)c;
//Do Something
}
}
More elegant way:
var objTextboxValues = this.Controls.OfType<TextBox>().Select(obj => obj.Text).ToList();
var varJoinedText = String.Join(", ", objTextboxValues);
Original Answer:
var varAllTextBoxValues = "";
foreach (Control objControl in this.Controls)
if (objControl is TextBox)
varAllTextBoxValues += ((TextBox)objControl).Text;
}
MessageBox.Show(varAllTextBoxValues);
in c# winform entity framework
im serching like this
var search = dataBase.tbl_Employee.First(i=>i.code == 1);
i have many column in database and many textBox in Form
how to fill textbox.text like this
foreach(textbox tb in this.control.oftype<textbox>())
{
tb.text = search.tb.name;
}
You can set name of textboxes equals to property name of model:
and like this set Text property:
foreach(var tb in this.Controls.OfType<Textbox>())
{
tb.Text=
search.GetType().GetProperty(tb.Name).GetValue(search,null).ToString();
}
I have a checkboxlist.When I check one the value will appear in a table.Now I want that value and each value I check to make it a link.This is my code for getting the checked values:
foreach (ListItem item in check.Items)
{
if (item.Selected)
{
TableRow row = new TableRow();
TableCell celula = new TableCell();
celula.Style.Add("width", "200px");
celula.Style.Add("background-color", "red");
//celula.RowSpan = 2;
celula.Text = item.Value.ToString();
row.Cells.Add(celula);
this.tabel.Rows.Add(row);
Now I want the item.value to make it a link..I'm using c# in asp.net application
Add a Hyperlink control to the celula.Controls collection instead of setting the TableCell's Text property.
// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink h = new HyperLink();
h.Text = item.Value;
string url = "~/Default.aspx?Item=" + Server.UrlEncode(item.Value);
h.NavigateUrl = url;
celula.Controls.Add(h);
Note that you need to recreate this table on every postback, so you might want to add this to Page_PreRender instead.