pass value from aspx to javascript - c#

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(",");

Related

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
}
}

Loop c# - use variable in textbox

I made a loop:
for (int i = 0; i < sumUSERS; i++ )
{
principal.UserPrincipalName = "bn" + txtbox_cusnumber.Text + "." + txt_user1.Text;
}
In my Form i have Text boxes with the following names :
txt_user1
txt_user2
txt_user3
How can I set the value i in txt_user(i).text?
I hope my question is understandable.
Make an array of the boxes, and use an index, like this:
var txt_user = new [] {txt_user1, txt_user2, txt_user3};
for (int i = 0; i < txt_user.Length ; i++ ) {
principal.UserPrincipalName += "bn" + txtbox_cusnumber.Text + "." + txt_user[i].Text;
}
Note that I replaced = with +=, otherwise the text would be the same as if you used txt_user3 by itself (i.e. only the last assignment would stay).

Auto Incrementing file names?

I have a list of files like so
abc.txt
pas.txt
tempr.txt
What I would like to do is to append english alphabets to theese file names ..
the result should look like this
abc_a.txt
pas_b.txt
tempr_c.txt
This process should continue till the last character (i.e 'z'). if there are more files then the file names would become
abc_a.txt
pas_b.txt
tempr_c.txt
.................
filename_z.txt
anotherfilename_a001.txt
Notice that the counter was again reset to the first character except an integer was attached to it.
This is the code that i have right now. Please note that it is NOT working ..
string alphabets= "abcdefghijklmnopqrstuvwxyz";
List<string> filenames = new List<string>();
filenames.Add("test1.txt");
filenames.Add("newfile.cs");
filenames.Add("test2.txt");
filenames.Add("newfile2.cs");
string currentFileNmae = string.Empty;
foreach(string s in filenames) {
char usedAlphabet = new char();
for(int i = 0;i<=alphabets.Length-1;i+=11) {
usedAlphabet.Dump();
alphabets[i].Dump();
if(usedAlphabet != alphabets[i] )
{
if(currentFileNmae!= s)
{
string.Format("{0}--{1}",s,alphabets[i]).Dump();
usedAlphabet = alphabets[i];
currentFileNmae = s;
}
}
break;
}
}
I am part of a team that's building a file renamer tool for our internal purposes and hence i need this code. This is part of the our enumertation functionality that we have planned.
Please suggest.
thanks
Try starting here:
using System.Diagnostics;
using System.IO;
string filename = #"C:\Foo\Bar.txt";
for (int count = 0; count < 100; count++)
{
char letter = (char)((int)'a' + count % 26);
string numeric = (count / 26) == 0 ? "" : (count / 26).ToString("000");
Debug.Print(Path.GetFileNameWithoutExtension(filename) + "_" + letter + numeric + Path.GetExtension(filename));
}
Substitute your own loop to go through the filenames and use Path to manipulate the pieces/parts of the names.
The renaming, IIRC, can be handled by File.Move. Surround it with a try/catch to implement the name collision logic.
Had no coffee yet, but this should do.
List<string> files = new List<string>();
int charIndex = 0;
int numericIndex = -1;
foreach (var file in files.Select(path => new FileInfo(path)))
{
// Create new Filename - This may needs some tuning
// to really remove only the extension ad the end
// It doesnt take care of things like
// file.bmp.bmp.bmp ...
string newFileName = String.Format("{0}_{1}{2}.{3}",
file.FullName.Replace(file.Extension,String.Empty),
(char)(charIndex++ + 97),
(numericIndex > -1 ? String.Format("{0:D4}", numericIndex) : String.Empty),
file.Extension);
// Rename the File
file.MoveTo(newFileName);
// Increment Counters.
if (charIndex > 25)
{
charIndex = 0;
numericIndex++;
}
}
You can try something like this
const string directory = #"C:\\wherever";
string[] fiNames = new string[]{ "abc", "pas", "etc",};
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
int x = 0;
string ending = "";
for(int i = fiNames.Count()-1; i>=0; i--)
{
if(x%26==0)
{
x=0
if( ending=="")
ending="1";
else
ending=(System.Convert.ToInt32(ending)+1).ToString();
}
System.IO.File.Move(directory+fiNames[i], fiNames[i]+alphabet[x].ToString()+ending);
x++;
}

ASP.NET gives multiple control ID error for one control

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);
}

C# For Loop with LIST using LINQ

I'm using LINQ and returning a list to my Business Logic Layer. I'mtrying to change one of the values in the list (changing the 'star' rating to an image with the number of stars).
Although the counter (i) appears to be working, the FOR loop is not working correctly. The first time through it stops at the correct IF but then it pops out at the ELSE statement for everything and all values end up with "star0.png." It appears as though I'm not cycling through the list??? Thanks in advance!
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
if (serviceCode == "*")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star1.png";
}
else if (serviceCode == "**")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star2.png";
}
else if (serviceCode == "***")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star3.png";
}
else if (serviceCode == "****")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star4.png";
}
else
{
ReviewList[i].SERVICE.SERVICE_DESC = "star0.png";
}
}
If all values end up at star0.png, then you are cycling through the list. The fact that the else statement is the only code being executed for each element suggests a logical error -- did you perhaps mean to do something like this?
string serviceCode = ReviewList[i].SERVICE.SERVICE_CODE;
I dont think its an issue of the for loop working properly... your syntax is good and as written will iterate ReviewList.Count # of times.
I would step through and verify the contents of ReviewList first.
Let me know what you find
If you know each item will consist of a number of stars, why not do this?:
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
ReviewList[i].SERVICE.SERVICE_DESC = "star" + serviceCode.Length + ".png";
}
Protection on double pass and with else condition
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
if(!serviceCode.Contains(".png")) { // once name set should not be modified
if(serviceCode.Contains("*"))
ReviewList[i].SERVICE.SERVICE_DESC = "star" + serviceCode.Length + ".png";
else
ReviewList[i].SERVICE.SERVICE_DESC = "star0.png";
}
}
alternate LINQ approach
ReviewList.ForEach(rs=>if(!rs.SERVICE.SERVICE_DESC.Contains(".png"))
{ rs.SERVICE.SERVICE_DESC =
"star" + rs.SERVICE.SERVICE_DESC.Length + ".png"});

Categories