aspx file has 10 labels. Their ID's are like lbl0, lbl1, lbl2...,lbl10.
In the cs file, they will be assigned the values of a list called lst.
lbl0.Text=lst[0];
lbl1.Text=lst[1];
How can I accomplish that in a for loop.
I need sth like this:
for(i=0;i<10;i++)
{
(lbl+i).Text = lst[i];
}
Inside your loop, you can build a string with the label's ID.
string s = String.Format("lbl{0}", i);
And then using something like FindControl() to get the label with that ID.
I think this should work, just use FindControl to get each Label in your loop:
for (int i = 0; i < 10; i++)
{
((Label)(this.FindControl("lbl" + i.ToString()))).Text = lst[i];
}
Related
On drop down list selected Index Change i want bind Drop Down List according the condition.
I am doing this and data is showing if i am using break point but On Front End Drop Down List is not Bind. it shows blank.
My code is given below:
SqlParameter[] param6 = new SqlParameter[]
{ new SqlParameter("#status","drivershow"),
new SqlParameter("#FromDateWithStartTime",driverdate),
new SqlParameter("#dayvalue",dayvalue),
};
List<clsDropDown> assigneddriver = comnFunctionObj.getDropDownList(clsConstant.SP_GET_CAR_BOOKING_STATUS, param6);
ddldriver.Items.Clear();
for (int i = 0; i < assigneddriver.Count; i++)
{
if (!ddldriver.Items.Contains(new ListItem(assigneddriver[i].DisplayFieldText.Trim(), assigneddriver[i].ValueFieldText.Trim())))
{
ddldriver.Items.Add(new ListItem(assigneddriver[i].DisplayFieldText.Trim(), assigneddriver[i].ValueFieldText.Trim()));
}
}
Make sure you bind the data after adding items to DropDownlist as:ddldriver.DataBind();
ddldriver.Items.Clear();
for (int i = 0; i < assigneddriver.Count; i++)
{
// add items
}
// now bind the Data to the Dropdownlist
ddldriver.DataBind();
Check also if assigneddriver list is not null.
I am trying to re define the select query and make it user defined by first listing out all the fields in one checkbox list and grouping them in another and providing a where condition for selected check boxed fields. I am returning a view model that contains the list of all my columns.
I need to know how to populate the list from my view model onto a asp.net checkbox list.
Thanks in advance,
Hrg
As per my understanding try this
#for (int i = 0; i < Model.PropertyName.Count(); i++)
{
<p>
#Html.CheckBox("model.PropertyName[" + i + "]", !Model.PropertyName[i])
</p>
}
You van use this also
#for (int i = 0; i < Model.PropertyName.Count(); i++)
{
#Html.CheckBoxFor(model => Model.Tags[i])
}
I have one drop down list in my pages that its source comes of below code. Now I like to put 1 text box adjusted on my drop down list and when I type on that, source of drop down list (DocumentNo) depend on what I type in the text box and when text box is null drop downs list shows all the (DocumentNo) , please help how I have to change my code,
protected void ddlProjectDocument_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var query = from p in _DataContext.tblDocuments
orderby p.DocumentNo
select p;
int maxs = 0;
foreach (tblDocument v in query)
{
if (v.DocumentNo.Length > maxs)
maxs = v.DocumentNo.Length;
}
foreach (tblDocument vv in query)
{
string doctitle = vv.DocumentNo;
for (int i = vv.DocumentNo.Length; i < maxs; i++)
{
doctitle += " ";
}
doctitle += " | ";
doctitle += vv.TITLE;
// Use HtmlDecode to correctly show the spaces
doctitle = HttpUtility.HtmlDecode(doctitle);
ddlProjectDocument.Items.Add(new ListItem(doctitle, vv.DocId.ToString()));
}
}
First, I would highly recommend storing the result of that query at the beginning of the method into something like a session variable so that you don't have to continually query the database every time you hit this page.
Second, you should use the OnTextChanged event in ASP.NET to solve this problem. Put in the OnTextChanged attribute to point to a method in your code behind that will grab the query result values (now found in your session variable) and will reset what is contained in ddlProjectDocument.Items to anything that matched what was being written by using String.StartsWith():
var newListOfThings = queryResults.Where(q => q.DocumentNo.StartsWith(MyTextBox.Value));
At this point all you need to do is do that same loop that you did at the end of the method above to introduce the correct formatting.
I would like to use the IEnumerable function Intersect() to combine a few list and get the similar integers from each list. The problem I'm faced with is that I don't know how many list I will need to compare.
Here is an example:
A{1,2,3,4}
B{1,2,3}
C{1,2}
results = A.Intersect(B).Intersect(C)
This works great, but the next time around I may have a D{1,2} next time I come across the function.
I'd like to use the Intersect method, but I'm open to new ideas as well.
If you are receivng the collections in a list, you could do this:
List<List<int>> lists = new List<List<int>>();
var result = lists[0].AsEnumerable();
for (int i = 0; i < lists.Count - 1; i++)
{
result = result.Intersect(lists[i + 1]);
}
i have an application in which i have around 100 textinputs all are numbers
i want to simplify the addition ie. any other way than saying txt1.text+txt2.text.....
that would increase my code a lot
is it possible to have (n+=txt*.text) or some thing like that
any help would be appreciated have to get the application done in two days thank you
If txt1, txt2 etc are public properties of the class representing this, you can use the following code to get the sum of the numbers in the text inputs.
var n:Number = 0;
for(i = 1; i <= total; i++)
n += Number(this["txt" + i].text);
To get a concatenated string:
var s:String = "";
for(i = 1; i <= total; i++)
s += this["txt" + i].text;
If the text inputs are properties of a different class, use the instance name of the object instead of this. For example:
instanceName["txt" + i].text;
Another solution that is more clean is to store them in an array and loop through them. But that might require changes in other parts of your code.