ASP.NET gives multiple control ID error for one control - c#

I want to add a control to a placeholder dynamically, like this:
int fileCount = Convert.ToInt32(lblCount.Text);
for (int i = 0; i<fileCount ; i++)
{
FileUpload fu = new FileUpload();
if(PlaceHolder1.HasControls())
PlaceHolder1.Controls.AddAt(i,fu);
else
PlaceHolder1.Controls.Add(fu);
PlaceHolder1.Controls[i].ID = "123456abcdef" + i;
}
But I get the error
Multiple controls with the same ID '123456abcdef0' were found. FindControl requires that controls have unique IDs.
Why? Only one control should get that ID on each iteration of the loop.
EDIT: Should mention that I haven't actually been able to test the loop, I get the error even when fileCount is 1.
SOLUTION: I called this function from a "foreach" loop in page load when I thought it was outside of it. Still, having the clear() method in mind will remove the necessity of the addat part of the function.

Just do a clear before you start adding:
PlaceHolder1.Controls.Clear();
And your add statements can be simplified as follows:
FileUpload fu = new FileUpload();
fu.Id = "123456abcdef" + i;
PlaceHolder1.Controls.Add(fu);

As per my knowledge, you can try something like below.
int fileCount = Convert.ToInt32(lblCount.Text);
for (int i = 0; i<fileCount ; i++)
{
FileUpload fu = new FileUpload();
fu.ID = "123456abcdef" + i;
PlaceHolder1.Controls.Add(fu);
}
Hope this Helps!!

Change to this:
int fileCount = Convert.ToInt32(lblCount.Text);
for (int i = 0; i<fileCount ; i++)
{
FileUpload fu = new FileUpload();
fu.ID = string.Format("fu_{0}", i);
PlaceHolder1.Controls.Add(fu);
}

Related

How to do a clickable file path in asp.net c#

I am trying to get a clickable file path using asp.net c# visual studios web form, meaning to say that it is like windows file explorer, allowing the person to navigate through the different levels of folders etc, can anyone provide any links to help get me started? [1]: https://i.stack.imgur.com/WyyLq.png
You could try to get the path string and divide it in multiple pieces. Then store them in multiple textboxes, labels, buttons or whatever you want. My form looks like this: Picture Form
Secondly, you will have to update those (in my case) textboxes to save the path. See my code and decide what you want to use, and if you have to modify it.
private void changePath()
{
String path = webBrowser1.Url.AbsolutePath;
if (path.Contains(#"/")) { path = path.Replace(#"/", #"\"); }
string[] directories = path.Split(Path.DirectorySeparatorChar);
int count = directories.Count();
if (count <= 6)
{
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = "";
for (int i = 0; i < count; i++)
{
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[i];
}
}
else
{
int p = count / 6;
int z = count - (p * 6);
for (int i = 0; i < count; i++)
{
int g = i - 1;
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[z];
z++;
if (i == 5)
{
break;
}
}
}
}
Second step is to make click functions on the (in my case) textboxes. Here is one sample of how you can do this. See for yourself what you can do.
private void textBox5_Click(object sender, EventArgs e)
{
if(!textBox5.Text.Equals(String.Empty))
{
String p = webBrowser1.Url.AbsolutePath;
if(!textBox6.Text.Equals(String.Empty))
{
webBrowser1.Url = new Uri(p.Replace(#"/" + textBox6.Text, ""));
}
}
}
This code will remove the last piece, leaving you with a new path. Example:
Before: C:\Users\USERNAME\Desktop\C#
After:
After: C:\Users\USERNAME\Desktop
Again, you have to look what works for you. There are multiple ways to resolve your issue.
Good Luck!
Twan.

Fill an array with a loop

I want to get a shorter code than now but I don't know how.
What I do now is like the code below.
arrPictureBox[0] = picChair0;
arrPictureBox[1] = picChair1;
arrPictureBox[2] = picChair2;
arrPictureBox[3] = picChair3;
arrPictureBox[4] = picChair4;
arrPictureBox[5] = picChair5;
arrPictureBox[6] = picChair6;
arrPictureBox[7] = picChair7;
arrPictureBox[8] = picChair8;
arrPictureBox[9] = picChair9;
arrPictureBox[10] = picChair10;
arrPictureBox[11] = picChair11;
(pic) is a picturebox.
But I want less code but I don't know if it possible to do this with a loop (for loop).
for (int i = 0 ; i < arrPictureBox.Length; i++)
{
arrPictureBox[i] = picChair + i;
}
If picChairN is a local variable then there's nothing you can do to simplify it as much as you'd like. The best you can do is
arrPictureBox = new [] { picChair0, picChair1, picChair2, picChair3,
picChair4, picChair5, picChair6, picChair7,
picChair8, picChair9, picChair10, picChair11};
If picChairN is a class member (e.g. a field created by the designer) then you could use reflection, but considering you already have the array method typed out I don't see much benefit.
Let's predict you're on WinForms and the pictureBoxes already exist, then you can use the following:
for (int i = 0; i < arrPictureBox.Length; i++)
{
arrPictureBox[i] = this.Controls["picChair" + i];
}
Which actually does this:
get the first Control (a PictureBox for example) with the given name
add the found control to the array of pictureboxes
EDIT:
It might be useful to check for non existing pictureBoxes:
for (int i = 0 ; i < arrPictureBox.Length; i++)
{
var pb = this.Controls["picChair" + i] as PictureBox;
if (pb != null)
{
arrPictureBox[i] = pb;
}
}
You can use al List like below.
List<string> arrPictureBox = new List<string>();
for (int i = 0; i < 20; i++)
{
arrPictureBox.Add("picChair" + i);
}
var result = arrPictureBox.ToArray();
Hope it helps.
If all the picture boxes are on the same form and are the ONLY picture boxes on the form, you can loop through them with something like the following:
int x = 0;
foreach(Control c in this.Controls)
{
if(c is PictureBox)
{
arrPictureBox[x++] = c
}
}

Can I loop through TextBox id's to check / change values in asp.net c#?

I'm working on a project and I'm a beginner and I'm having a little trouble with this. I'm trying to check if a textbox is empty and if it is to change the value to N/A so that I can input n/a into a database instead of it not working.
Here is the code that I thought would work but didn't because the .Text property isn't near the ID anymore:
for(int i = 1; i<=17; i++)
{
if(!("tb" + i).Text)
"tb" + i.Text = "n/a";
}
I wasn't sure if the true/false would work but I never got to find out because it doesn't compile to begin with. I have 17 textboxes on my design page all with ID 'tb + i' e.g tb1, tb2
thx
If you want to loop through the textbox ids, you have to find the control on the page first using the FindControl method.
Then you can create your loop like this:
TextBox txt;
for(int i = 1; i<=17; i++)
{
txt = (TextBox)Page.FindControl("tb" + i);
if(string.IsNullOrEmpty(txt.Text))
txt.Text = "n/a";
}
You can use FindControl method
for(int i = 1; i<=17; i++)
{
var textBox = Page.FindControl("tb" + i) as TextBox;
if(textBox != null && textBox.Text == "") { ... }
}

pass value from aspx to javascript

i have set of addresses, which i use to load on the map,
im loading those addresses from cs file to javascript using HiddenField like
// In code behind
string []arr=new string[]
{
"51.482238,0.001581",
"51.473364,0.011966","51.471974,-0.000651",
"51.472108,-0.002196","51.474995,-0.003827",
"51.476492,-0.005629","51.477855,-0.006058",
"51.478443,-0.007045","51.479298,-0.007861",
"51.481202,-0.002136","51.481577,-0.0022"
};
for ( int j = 0; j < arr.Length ; j++ )
{
HiddenField1.Value += arr[j] + ":";
}
}
//in javascript
var hidValue= document.getElementById("<%=HiddenField1.ClientID%>").value;
var latlonArr = hidValue.split(':');
for (var i = 0; i < latlonArr.length - 1; i++)
{
//alert("item :"+ i + " : "+latlonArr[i]);
var latlonA = latlonArr[i].split(',');
var latlon = new google.maps.LatLng(latlonA[0],latlonA[1]);
arrCoords.push(latlon);
}
so, now if i have more no. of address, will it be okay to go with HiddenField for addresses ranging from 100 to 1000 or more..
Check the performance with the maximum number of addresses you might have, if the performance is OK then you should be fine.
If the performance is not OK then consider fetching the addresses asynchronusly, e.g. 100 at a time.
In code-behind you can use:
string []arr=new string[]
{
"51.482238,0.001581",
"51.473364,0.011966","51.471974,-0.000651",
"51.472108,-0.002196","51.474995,-0.003827",
"51.476492,-0.005629","51.477855,-0.006058",
"51.478443,-0.007045","51.479298,-0.007861",
"51.481202,-0.002136","51.481577,-0.0022"
};
HiddenField1.Value = arr.join(",");

Concatenate strings to make Picturebox name

i have lot of pictureboxes named this way: PBr1_1, PBr1_2, ... PBr1_9
I'd like to make loop
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1"){ "PBr1_"+"i".Tag = "cb.png";}
}
so for i=0 => PBr1_0, i=10 => Pbr1_10.
Example i have value in textbox: 0001011101 - then if value in textbox is "1" then i'd like to change picturebox tag.
How to automate this process, using for example loop "for"?
I suppose your controls are on a WinForm (this) and the ones with that name are all pictureboxes.
If so, that's the way ----
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1")
{
Control[] c = this.Controls.Find("PBr1_" + i.ToString(), true);
if(c != null && c.Length > 0) c[0].Tag = "cb.png";
}
}
You can put the picture boxes in to a List<PicutreBox> and iterate over the list.
var pictures = new List<PictureBox>();
pictures.Add(pic1);
pictures.Add(pic2);
//...
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1")
pictures[i].Tag = "cb.png";
}
Dynamic variable names as in your example are not supported.
Create an array (or list) that contain the picture boxes and use those within the for loop.
You can also use reflection but in my opinion it is best not to use that in this case.
If you are using WinForm you can use Control.Find method to locate a control by name
Once you have got the control you can easily change any property

Categories