dynamically create check box and insert its text to database - c#

I am trying To create check boxes list dynamically using c# and insert the check box text values to databse.But I dnt Understand How Can I Create and access its values to insert into db.Is there any one help me in this regard coz i have no strong idea except this.
public void CreateCheckBox(int n)
{
for (int i = 0; i <= n; i++)
{
Response.Write(#"<input type=""checkbox"" name=""link"" value=" + "CA0" + i +">"+"CA0"+ i +"<br>") ;
}
}
How Can I access its value to be inserted to db.Plz Help

you can assign an id attribute to the checkboxes like:
for (int i = 0; i <= n; i++)
{
Response.Write(#"<input id='check'" + i.ToString() + " type=""checkbox"" name=""link"" value=" + "CA0" + i +">"+"CA0"+ i +"<br>") ;
}
then on the event that you want to retrieve the values simply you can use the following:
for(int i=0;i<=n;i++)
{
var data=Request.Form["check" + i.ToString()]; // this should hold the value of the checkbox as string
}
hope that it will help
regards

Related

Why is my logic not adding the quotes back to the CSV File for the columns after the First one?

I am using hashmaps to store CSVFile1 and compare it to CSVFile2. CSVFile1 has a column of customers to remove while CSVFile2 has all of the completed data (which has more then 1 column and that is why I am only comparing dataArray2[0]). Once I compare I only rewrite the files that are NOT in the CSVFile1 to a file called CSVFileFinal.
The issue is that some of the data has commas inside of fields and when I read the file it take them out once I try and rewrite. My goal is to rewrite the data with the quotes back around the commas in the fields which I "split" at.
I have managed to get the First column (the most important one) to split and replace the quotes perfectly! But for whatever reason my second loop is messing up and not checking the columns after that correctly. I believe it is simply the placing of my code.
This is the logical statement/placement in question:
if (dataArray2[i].Contains(","))
{
string x = "\"" + dataArray2[i] + "\"";
record = x;
}
I know the logic works because if you look at my entire source code; the code posted works for the first column and works in the 2nd IF statement for If i == 0 meaning if it's the 1st columns. But how or where do I place that to work for the others afterward?
Can anyone help figure out where to place this for the following columns?
It should be a simple copy and pasting of the code I have but I've been unable to figure it out. Thank you!
int count = 0;
while (!CSVFile2.EndOfData)
{
if (!badCustomers.ContainsKey(dataArray2[0]))
{
String record = null;
for(int i = 0; i < dataArray2.Length; i++)
{
if(i == 0)
{
if (dataArray2[i].Contains(","))
{
string x = "\"" + dataArray2[i] + "\"";
record = x;
}
else
record = dataArray2[i];
} else
{
record = record + "," + dataArray2[i];
}
}
count++;
if( count % 50 == 0)
{
Console.WriteLine(count);
}
output.WriteLine(record);
}
dataArray2 = CSVFile2.ReadFields();
}
The above code works but does not consider the later columns after the first one to replace this quotes- this code is what should fix the ones after the first column but for whatever reason it does not. Why?
if (!badCustomers.ContainsKey(dataArray2[0]))
{
String record = null;
for(int i = 0; i < dataArray2.Length; i++)
{
if(i == 0)
{
if (dataArray2[i].Contains(","))
{
string x = "\"" + dataArray2[i] + "\"";
record = x;
}
else
record = dataArray2[i];
} else
{
if (dataArray2[i].Contains(","))
{
string x = "\"" + dataArray2[i] + "\"";
record = x;
}
else
record = record + "," + dataArray2[i];
}
}
What I believe you are asking is for this:
int count = 0;
while (!CSVFile2.EndOfData)
{
if (!badCustomers.ContainsKey(dataArray2[0]))
{
String record = String.Empty;
for(int i = 0; i < dataArray2.Length; i++)
{
if (dataArray2[i].Contains(","))
{
string x = "\"" + dataArray2[i] + "\"";
record += x;
}
else
{
record += dataArray2[i];
}
if (i < dataArray.Length -1)
{
record += ",";
}
}
count++;
if( count % 50 == 0)
{
Console.WriteLine(count);
}
output.WriteLine(record);
}
dataArray2 = CSVFile2.ReadFields();
}
This loops through each line from the input, adding quotes around commas. You may have to tweak it to meet your final needs, but this should be a start.
The reason it was only ever doing it for your first column is the if(i == 0). This means the code would only ever execute for the first iteration (your first column).

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).

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

Can I use an array in findcontrol?

Hello I have a DataList that has four lables and they all end with a number 1-4. In my code behind I have a for loop and an array that I want to set the labels of the Datalist with.
for (int x = 0; x< cartreciept.Items.Count; x++)
{
DataListItem item = cartreceipt.Items[x];
string catalogtype = ("select CatalogType From SC Where OrNum=" + OrNum)
if (catalogtype="TC")
{
((Panel)item.FindControl("pnlIprintInfo")).Visible = true;
string scRID = ("Select SCRID From SC Where OrNum =" + OrNum
for(int y = 1; y<5; y++)
{
string lT[y] = (Select LineText From table Where SCartRD =" + scRID + " AND LN=" + y)
((Label)item.FindControl("lbl[y]")).text = lT[y];
}
}
}
So would the ((Label)item.FindControl("lbl[y]")) work? most of this code is just pseudo code until I figure the details out. If you need to know anything else I can provide what needs to be known, I am open to other suggestions as well. Thank you to any one who can offer some help.
You can use this code - based on string.Format("....{0}",arguments)
var control = (Label)item.FindControl(string.Format("lbl{0}",y));
I suggest you to use DataList.ItemDataBound
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx

Categories