Populate asp.net dropdownlist with number - asp.net

A simple query , i want to populate the dropdownlist with number starting from 17 to 90 , and the last number should be a string like 90+ instead of 90. I guess the logic will be using a for loop something like:
for (int a = 17; a <= 90; a++)
{
ddlAge.Items.Add(a.ToString());
}
Also I want to populate the text and value of each list item with the same numbers.
Any ideas?

for (int i = 17; i < 90; i++)
{
ddlAge.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlAge.Items.Add(new ListItem("90+", "90"));

Try this:
for (int a = 17; a <= 90; a++)
{
var i = (a == 90 ? a.ToString() + '+': a.ToString());
ddlAge.Items.Add(new ListItem(i, i));
}

This is easy enough. You need to instantiate the ListItem class and populate its properties and then add it to your DropDownList.
private void GenerateNumbers()
{
// This would create 1 - 10
for (int i = 1; i < 11; i++)
{
ListItem li = new ListItem();
li.Text = i.ToString();
li.Value = i.ToString();
ddlAge.Items.Add(li);
}
}

for (int a = 17; a <= 90; a++)
{
ddlAge.Items.Add(new ListItem(a.ToString(), a.ToString()));
}

for (int i = 17; i <= 90; i++)
{
ddlAge.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlAge.Items.Insert(0, new ListItem("Select Age", "0")); //First Item
ddlAge.Items.Insert(ddlAge.Items.Count, new ListItem("90+", "90+")); //Last Item

for (int i = 0; i <=91; i++)
{
if (i == 0)
{
ddlAge.Items.Add("Select Age");
}
else if(i<=90)
{
ddlAge.Items.Add(i.ToString());
i++;
}
else
{
ddlAge.Items.Add("90+");
}
}

Related

Update GridPane with Slider in JavaFx

I'm new at this, I´m creating a field that solves N x N Matrixes, and I want the spinner to add more TextFields every time I change its value, so the user can select the size of the matrix and then solve it...However I don't know how to do this.
I've tried to attach a listener JavaFX spinner but it didn't work, and it has created some errors in the code.
The code in question...
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage primaryStage) throws Exception{
Double[][] A = new Double[6][6];
Label tamanoML = new Label("Tamaño Matriz:");
Spinner tamanoMS =new Spinner(1,5,0,1);
int tamano=(int) tamanoMS.getValue();
int r;
Label[] campoL = new Label[6];
TextField campo [][] = new TextField[6][6];
Button resolverB = new Button("Resolver matrix");
resolverB.setOnAction((ActionEvent t) -> {
int n=(int) tamanoMS.getValue();
int cont=0;
for(int i=0; i<n; i++) {
for(int j=0; j<n+1; j++) {
A[i][j]=Double.parseDouble(campo[i][j].getText());
}
}
for (int a = 0; a < n; a++) {
Double temp = 0.0;
temp = A[cont][cont];
for (int y = 0; y < (n + 1); y++) {
A[cont][y] = A[cont][y] / temp;
}
for (int x = 0; x < n; x++) {
if (x!=cont) {
Double c = A[x][cont];
for (int z = 0; z < (n + 1); z++) {
A[x][z] = ((-1 * c) * A[cont][z]) + A[x][z];
}
}
}
cont++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n+1; j++) {
campo[i][j].setText(""+A[i][j]);
}
}
});
Button limpiarb = new Button("Limpiar");
limpiarb.setOnAction((ActionEvent t) -> {
for(int i=0; i<tamano+1; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j].setText("0");
}
}
});
GridPane mainPane = new GridPane();
mainPane.setMinSize(650,350);
mainPane.setPadding(new Insets(10,10,10,10));
mainPane.setVgap(5);
mainPane.setHgap(5);
mainPane.setAlignment(Pos.CENTER);
mainPane.add(tamanoML, 0, 0);
mainPane.add(tamanoMS, 1, 0);
mainPane.add(resolverB,0,1);
mainPane.add(limpiarb,1,1);
for(int i=0; i<tamano+1; i++) {
r=i+1;
if(r<tamano+1) {
r=i+1;
campoL[i]=new Label("X"+r);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<tamano; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
tamanoMS.valueProperty().addListener((newValue) -> {
int s;
for(int i=0; i<tamano+1; i++) {
s=i+1;
if(s<tamano+1) {
s=i+1;
campoL[i]=new Label("X"+s);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<tamano; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
s=0;
});
Scene scene = new Scene(mainPane);
primaryStage.setScene(scene);
primaryStage.setTitle("Ventas del Dia");
primaryStage.show();
}
Ok so the first thing you want to do is rewrite your listener like this
tamanoMS.valueProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("heard");
int s;
for(int i=0; i<newValue+1; i++) {
s=i+1;
if(s<newValue+1) {
s=i+1;
campoL[i]=new Label("X"+s);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<newValue; i++) {
for(int j=0; j<newValue+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
s=0;
});
I'm gonna be honest I don't quite understand why you need oldValue and obs but you do. However, the reason your matrix wasn't expanding was because you weren't updating tamano to the most recent number.
You also need to change your spinner to this so that is know the value that is coming out is an integer.
Spinner<Integer> tamanoMS =new Spinner<Integer>(1,5,0,1);

Auto Update DropDown items in Datalist in Asp.net

I am Working on a Shop page where I want the Dropdownlist show the available Quantity . (Eg: if stock contains 2 items then Dropdownlist contains Items 1 followed by 2..)
Here's the code I attempted , I have no idea in which event i should put it.
`
Code:
connect cu = new connect();
System.Web.UI.WebControls.Label Label8 = (System.Web.UI.WebControls.Label)DataList1.FindControl("modelnoLabel");
//Modelnolabel contains model no
cu.cmd.CommandText = "select qty from stock where modelno=#mod";
//qty is retrived based on modelnolabel text of datalist
cu.cmd.Parameters.Clear();
cu.cmd.Parameters.AddWithValue("#mod", Label8.Text);
int qty = Convert.ToInt16(cu.cmd.ExecuteScalar());
if (qty < 5 && qty > 0)
{
(DataList1.FindControl("DropDownList1") as DropDownList).Items.Clear();
for (int i = 1; i < qty + 1; i++)
{
(DataList1.FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
}
}
else
{
(DataList1.FindControl("DropDownList1") as DropDownList).Visible = false;
}`
Thanks in advance
Thanks for the silence ,It really helped me figure out on my own :)
protected void DataList1_Load(object sender, EventArgs e)
{
for(int j=0;j<DataList1.Items.Count;j++)
{
System.Web.UI.WebControls.Label Label8 = (System.Web.UI.WebControls.Label)DataList1.Items[j].FindControl("modelnoLabel");
connect cu = new connect();
cu.cmd.CommandText = "select qty from stock where model=#mod";
cu.cmd.Parameters.Clear();
cu.cmd.Parameters.AddWithValue("#mod", Label8.Text);
int qty = Convert.ToInt16(cu.cmd.ExecuteScalar());
if (qty <= 5 && qty > 0)
{
(DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Clear();
for (int i = 1; i < qty+1; i++)
{
(DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Only "+qty+" Left";
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.OrangeRed;
(DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible= true;
(DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible= true;
}
}
else if (qty > 5)
{
(DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Clear();
for (int i = 1; i < 6; i++)
{
(DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.GreenYellow;
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Available";
(DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible = true;
(DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible= true;
}
}
else
{
(DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Visible = false;
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Out of Stock";
(DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.Red;
(DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible= false;
(DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible = false;
(DataList1.Items[j].FindControl("Label1") as System.Web.UI.WebControls.Label).Visible = false;
}
}
}

QList to QTableWidget

I'd like to paste all my data I stored in several QList variables into one central QTableWidget.
I have six QList<QString> variables with actually each length of them is 7.
With the help of this routine, I'd like to write each element of my QList into a QTableWidgetItem. What is the easiest and efficient way to solve this?
for (int ridx = 0; ridx < iRowCount; ridx++ )
{
tmptable = resultTable[ridx];
for (int cidx = 0; cidx < iColumnCount; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(tmptable[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
I got it running.
This is my final code to add all elements of each QList into the central QTableWidget. It's not that elegant solution. Might be there another (better) solution?
for (int cidx = 0; cidx < iColumnCount; cidx++)
{
if (cidx==0)
{
// Column LoginName
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListLoginName[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
if (cidx==1)
{
//Column Lastname
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListLastname[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
if (cidx==2)
{
// Column Firstname
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListFirstname[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
if (cidx==3)
{
// COlumn Position
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListPosition[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
if (cidx==4)
{
// Column Email
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListEmail[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
if (cidx==5)
{
// Column Telephone
for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(ListTelephone[ridx]);
ui->tableWidget->setItem(ridx,cidx,item);
}
}
}

How to writte matrice(2d array) into listbox

I have tried following code but it writes in vertical way.How can i add 2d array into my listbox
for (int i = 0; i < test.GetLength(0); i++)
{
for (int j = 0; j < test.GetLength(1); j++)
{
ListBox3.Items.Add("-" + test[i, j].ToString());
}
ListBox3.Items.Add("\n");
}
}
This may help you from what I understand from your query;
string listboxstr = "";
for (int i = 0; i < test.GetLength(0); i++)
{
listboxstr = "" + test[i, 0].ToString();
for (int j = 1; j < test.GetLength(1); j++)
{
listboxstr +="-" + test[i, j].ToString();
}
ListBox3.Items.Add(listboxstr);
ListBox3.Items.Add("\n");
}
}

Create 2 or more text files with ASP.NET

I have created a web app which creates 1 text file. Inside this text file it is created 1000 rows with the same word "TRY AGAIN". After this each 50 rows I put a random code which means in 1000 rows, 20 rows are random.
This is my code:
static Random randNum = new Random();
public static string Random(int ran)
{
string _charachters = "ABCDEFGHIJKMLNOPQRSTUVWXYZ0123456789";
char[] chars = new char[ran];
int allowedCharCount = _charachters.Length;
for (int i = 0; i < ran; i++)
{
chars[i] = _charachters[(int)((_charachters.Length) * randNum.NextDouble())];
}
return new string(chars);
}
protected void Button1_Click(object sender, EventArgs e)
{
string pathCreate = #"C:\" + TextBox3.Text + ".txt";
if (!File.Exists(pathCreate))
{
using (StreamWriter sw = File.CreateText(pathCreate))
{
for (int i = 1; i <= int.Parse(TextBox1.Text); i++)
{
sw.WriteLine("TRY AGAIN.");
}
}
}
string pathRandom = #"C:\" + TextBox3.Text + ".txt";
string[] lines = File.ReadAllLines(pathRandom);
for (int i = 0; i < lines.Length; i += int.Parse(TextBox2.Text))
{
lines[i] = lines[i].Replace("TRY AGAIN.", Random(int.Parse("7")));
}
File.WriteAllLines(pathRandom, lines);
}
Now I want to create 2 ore more text files with one click of a button. And on each text file there will be random codes (not duplicates). Any idea?
Thank You.
I found the solution. It is late in my country and my brain barely works. :P
for(int j = 1; j <= 10; j++)
{
string pathKrijo = #"C:\inetpub\wwwroot\KODET\" + j.ToString() + ".txt";
using (StreamWriter sw = File.CreateText(pathKrijo))
{
for (int i = 1; i <= 100; i++)
{
sw.WriteLine("Provo Përsëri.");
}
}
string pathKodFitues = #"C:\inetpub\wwwroot\KODET\" + j.ToString() + ".txt";
string[] lines = File.ReadAllLines(pathKodFitues);
for (int i = 0; i < lines.Length; i += 10)
{
lines[i] = lines[i].Replace("Provo Përsëri.", Random(int.Parse("7")));
}
File.WriteAllLines(pathKodFitues, lines);
}

Resources