i am kinda new to asp.net, and also my english not really good, anyway i hope you still can get my point. nah i have a question here, basically i trying to integrate my app with LinkedIn, so i am using REST API, when i click a button it redirect my page to request data from LinkedIn, it returned XML data, and it contain user's education data, the count of user's education data is uncertain, so i decided to generate text box control from behind ("C#"). i do this :
TextBox txt;
int i;
foreach (var element in person)
{
if ((element.Name == "first-name") || (element.Name == "last-name"))
{
tbName.Text = tbName.Text + " " + element.Value;
}
else if (element.Name == "skills")
{
i = 1;
foreach (var child in element.Elements())
{
if (child.Name == "skill")
{
txt = new TextBox();
txt.ID = "tbSkills" + i;
txt.Width = 200;
txt.Visible = true;
txt.ReadOnly = true;
txt.Text = child.Element("skill").Element("name").Value;
form1.FindControl("divMoreSkills").Controls.Add(txt);
i++;
}
}
}
else if (element.Name == "industry")
{
tbIndustry.Text = element.Value;
}
else if (element.Name == "educations")
{
i = 1;
foreach (var child in element.Elements())
{
if (child.Name == "education")
{
txt = new TextBox();
txt.ID = "tbEducations" + i;
txt.Width = 200;
txt.Visible = true;
txt.ReadOnly = true;
txt.Text = child.Element("school-name").Value;
form1.FindControl("divMoreEducations").Controls.Add(txt);
i++;
}
}
}
}
}
my question is, if i want to use text box i generated previously later, does C# will recognize it? because the control i generated did not have runat server property.
thank you.
if i want to use text box i generated previously later, does C# will
recognize it? because the control i generated did not have runat
server property.
You don't have to specify runat="server", since from the code behind you will be creating server side controls. runat="server" is used on the aspx pages to identify server side controls.
To find it, you need to make sure that these controls are available on the post back. you can find them like you are finding your div in form1. Use Page.FindControl
Yes it will.. You can use added control like
TextBox txt = (TextBox )Page.FindControl("tbSkills0");
Related
I have got many buttons in my Application Form. And I would like to check every buttons text (compare). How can I achive that ?
for (i = 1; i < 30; i++)
{
if (this.button1.Text == "Hello") //here is PROBLEM
{
//..some statement
}
}
So next time this.button1.Text must change to this.button2.Text and so on...
this.button[i].Text not working.
Buttons are not arrays. Each one is a discreet object, and a child of its container.
Ideally, you need to build a collection (array, list, whatever) of the buttons and iterate through that collection, rather than using an index variable (i).
Here's a good approach: https://stackoverflow.com/a/3426721/820068
This is a correct syntax:
foreach (Control button in this.Controls)
{
if (button.GetType() == typeof(Button) && button.Text == "Hello")
{
//..some statement
}
}
I'm quite sure that this is a windows form.
And in windows form you can iterate the controls like this.
foreach (Control c in panel.Controls)
{
string cType = c.GetType().ToString();
// check all buttons
if (cType == "System.Web.UI.WebControls.Button")
{
if(((Button)c).Text == "Hello")
{
}
}
}
So what the code does is to iterate all the controls inside a panel and check each control if it's type is a button.
Update:
As Wesley said, much better approach for the condition is to implement it like this
if (c is Button && c.Text.Equals("Hello")) {
for (int i = 1; i < 3; i++)
{
var buttonName = "button" + i;
Button button = this.Controls.Find(buttonName, true).FirstOrDefault() as Button;
string text = button.Text;
}
try this code.
When I want to add a new tab to the control I want a browser to be added to it.
Each tab/browser should have an array of string address's that the user can cycle through. Different tabs have different addresses to cycle through with left and right arrows.
I can do all this manually with one tab that uses an arraylist of addresses to navigate to but I cant do it with more than one. SO far i am able to add new tabs, and even add a browser to them but cannot navigate the browser to the address since I cannot access the browser name of the new tab. I also do not know how to assign an arraylist of string addresses dynamically. I have read in address from a text file I just dont know what to do with the arraylist.
I want to be able to make the browser of the currently selected tab navigate to the first address on the arraylist assigned to that tab. If you can point out how to do this, not even codewise but pesudo code would be helpful as I can not figure it out after spending a week trying different ways
Some snippets:
tabControl1.SelectedTab
Manual way of going forward and backwards on a tab:
private void Next()
{
if (current < list.Count-1)
{
current++;
String x = (String)list[current];
webBrowser1.Navigate(x);
addressField.Text = x;
}
else
{
Console.WriteLine("AT THE END");
}
Enabler();
}
private void Previous()
{
if (current > 0)
{
current--;
String x = (String)list[current];
webBrowser1.Navigate(x);
addressField.Text = x;
}
else
{
Console.WriteLine("AT THE START");
}
Enabler();
}
private void Enabler()
{
String x = (String)list[current];
if (current == list.Count - 1)
{
toolStripButton2.Enabled = false;
}
else
{
toolStripButton2.Enabled = true;
}
if (current == 0)
{
toolStripButton1.Enabled = false;
}
else
{
toolStripButton1.Enabled = true;
}
UpdateSeperator();
}
private void UpdateSeperator()
{
int g = list.Count;
SeperateButtons.Text = (current+1) + "/" + g;
}
Adding a tab:
private void OpenTab()
{
bookmarks.Add("http://www.beer.com");
bookmarks.Add("http://www.sky.com");
bookmarks.Add("http://www.taffatech.com");
String title = "List " + (tabControl1.TabCount + 1).ToString();
TabPage addedTabPage = new TabPage(title); //create the new tab
tabControl1.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser();
addedWebBrowser.Parent = addedTabPage; //add the new webBrowser to the new tab
addedWebBrowser.Dock = DockStyle.Fill;
String add = (String) bookmarks[bookmarks.Count];
addedWebBrowser.Navigate(add);
}
Any advice would be nice, even if you do not know how to do it idea's would be great and I will try them!
I have some Textboxes that are created dynamically --
int i = 1;
while (reader.Read())
{
System.Web.UI.WebControls.TextBox textBox = new System.Web.UI.WebControls.TextBox();
textBox.ID = reader["field_id"].ToString();
textBox.Enabled = false;
HtmlGenericControl div = new HtmlGenericControl("div");
if(i%2 != 0)
div.Attributes.Add("style", "margin-right:120px;padding-bottom:20px;");
if (i % 2 == 0)
div.Attributes.Add("style", "padding-bottom:20px;");
div.Attributes.Add("class", "inline fourcol");
div.InnerHtml = "<label>" + reader["field"] + "</label>";
div.Controls.Add(textBox);
panelId.Controls.Add(div);
textBox.Text = reader["field_value"].ToString();
++i;
}
That works fine (at least i'm sure -they show up how they should). But when i try to loop through them to enable them, or get their values, i get an "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'. " error.
This is how i've been trying to do it --
public void EditPanel(System.Web.UI.WebControls.Panel panel)
{
foreach (System.Web.UI.WebControls.TextBox t in panel.Controls)
{
t.Enabled = true;
}
}
Thanks!
You're looping over panel.Controls, which will loop over every control in the panel. This is not necessarily the same thing as looping over everything you've added. If there was something else inside the panel that existed when you started, you will end up getting that too.
What you probably wanted was this:
foreach (var t in panel.Controls.OfType<System.Web.UI.WebControls.TextBox>())
{
t.Enabled = true;
}
You are putting each textbox inside a "div" control which is HtmlGenericControl, then inside the panel control. So first you must search for the HtmlGenericControl inside panelId.Controls
A sample code that might help you:
public void EditPanel(System.Web.UI.WebControls.Panel panel)
{
foreach (Control c in panelId.Controls)
{
if (c is HtmlGenericControl)
{
foreach (var textbox in c.Controls.OfType<TextBox>()) //ofType returns IEnumerable<TextBox>
textbox.Enabled = true;
}
}
}
There is a control inside your Panel, that is not a TextBox and could not be cast to it. You should place a breakpoint before the loop and check the panel.Control collection contents in debug mode.
You can avoid the issue if you don't specify a type in the foreach loop and do the safe cast yourself.
foreach (var t in panel.Controls)
{
var textbox = t as System.Web.UI.WebControls.TextBox;
if(textbox != null)
{
textbox.Enabled = true;
}
}
You should check if control is TextBox
public void EditPanel(System.Web.UI.WebControls.Panel panel)
{
foreach (var t in panel.Controls)
{
if (t is System.Web.UI.WebControls.TextBox)
((System.Web.UI.WebControls.TextBox)t).Enabled = true;
}
}
The Controls collection will contain a collection of all the controls in the panel - not just TextBoxes. You can iterate through all of the controls and use the as operator to perform a type cast. If the type cast succeeds then you may enable the textbox.
public void EditPanel(System.Web.UI.WebControls.Panel panel)
{
foreach (var control t in panel.Controls)
{
System.Web.UI.WebControls.TextBox textBox = control as System.Web.UI.WebControls.TextBox;
if (textBox != null)
{
control.Enabled = true;
}
}
}
you add the textbox to the div element and the div element to the panel. therefore you need to select the controls in the panel and then find the textbox.
foreach (var t in panel.Controls.Cast<Control>().SelectMany(c => c.Controls))
{
if (t is TextBox == false) continue;
((TextBox)t).Enabled = true;
}
Let's say I have 30 controls, all lbls, all called "lblA" with a number after.
I also have 30 textboxes, same thing - called "txtB" with a number after.
How exactly would I formated this.
for (i = 1; i < this.controls.count;i++)
{
if ("lblA"+i=null)
{
break;
}
string A = string A + ("lblA" + i).Text
string B = string B + ("txtB" + i).Text
}
I've tried a few different things like calling the object with this.controls[i] but it's not exactly what I want. What I am doing is I have a lot of labels and text boxes in a form that are added at run time. I need to loop through the form to get them all. I'm writting it as a for each with quite a few ifs, but I'm curious if there's a dynamic way to do it.
I've looked for about 1-1:30 hours online and found nothing close to, thanks for the help all.
var labels = new Dictionary<int, string>();
for (i = 1; i < this.controls.count;i++)
{
var label = FindControl("lblA" + i) as Label;
if (label == null)
{
break;
}
labels.Add(i, label.Text);
}
What you want to use is the FindControl method.
Example in VB:
Dim txtMileage As TextBox = CType(cphLeft.FindControl("txtMileage" & iControlCountDays.ToString()), TextBox)
Maybe this will solve what you're after:
void GetSpecialControls() {
const string TXT_B = "txtB";
const string LBL_A = "lblA";
List<TextBox> textBoxList = new List<TextBox>();
List<Label> labelList = new List<Label>();
foreach (Control ctrl in this.Controls) {
Label lbl = ctrl as Label;
if (lbl != null) {
if (lbl.Text.IndexOf(LBL_A) == 0) {
labelList.Add(lbl);
}
} else {
TextBox txt = ctrl as TextBox;
if (txt != null) {
if (txt.Text.IndexOf(TXT_B) == 0) {
textBoxList.Add(txt);
}
}
}
}
Console.WriteLine("Found {0} TextBox Controls.", textBoxList.Count);
Console.WriteLine("Found {0} Label Controls.", labelList.Count);
}
I have a asp.net textbox(textarea which is in a repeater have single texarea for each record which are read only) I have button (open in new window) when i click on it the content of the text area needs to be rendered in a new window using javascript .
similar to experts exchange.
Sorry I misread the question the first time.
Here is a javascript solution:
function DisplayTextFromRepeater()
{
var text = '';
var repeater = document.getElementById('MyRepeater');
var inputs = repeater.getElementsById('input');
var txtId = 'MyTextBox' //ID of textbox in repeater template
for(var i = 0; i < inputs.length; i++)
{
if(inputs[i].type == 'text')
{
if(inputs[i].id.indexOf(txtId) != -1)
{
text = text + inputs[i].value;
}
}
}
OpenNewWindow(text);
}
function OpenNewWindow(message)
{
var OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars=yes,menubar=no");
OpenWindow.document.write("<html>");
OpenWindow.document.write("<title>Title Goes Here</title>");
OpenWindow.document.write("<body>");
OpenWindow.document.write(message);
OpenWindow.document.write("</body>");
OpenWindow.document.write("</html>");
}