I have multiple textboxes in my form like txtTask0, txtTask1... txtTask12.
so I want to pass values of those textboxes into my webservice one by one.
for (int i = 0; i <= 12 ; i++)
{
sOUT = ws_service.InsertAchievement(i,txtTask0.Text,txtAchieve0.Text);
}
Here instead of passing txtTask0.text I need to pass the "i" value one by one like
txtTask[i].text
something similar to this
TextBox tb = (TextBox) Controls["txtTask" + i];
from this link
But that code results in error like
Error 92 The best overloaded method match for 'System.Web.UI.ControlCollection.this[int]' has some invalid arguments
How can I pass multiple textbox values into the loop.?
You cant do like that, because the Controls[index] expects an integer as its parameter, but you are passing a "string concatenated with an integer" it wont work, instead you do like below it will work, hope it will help you...
foreach (Control c in this.Controls)
{
int i = 0;
if (c is TextBox)
{
while(i < 10)
{
if (c.Name == "txtTask" + i)
{
MessageBox.Show("This is textBox" + i);
}
i++;
}
}
}
EDIT :
If the condition if(c is TextBox) is not parsing correctly then do like
foreach (Control c in this.Controls)
{
int i = 0;
while (i < this.Controls.Count)
{
if (c.Name == "txtTask" + i)
{
MessageBox.Show("This is textBox" + i);
}
i++;
}
}
EDIT 2:
Or Simply if you want to loop out in all textbox controls in aspx page use this following code part. It works very perfectly..
int count = 0;
foreach (Control c in this.Page.Controls)
{
foreach (Control c1 in c.Controls)
{
int i = 0;
if (c1 is TextBox)
{
while (i < 10)
{
if (c1.ID == "TextBox" + i)
{
count++;
}
i++;
}
}
}
}
Label1.Text = count + " textbox(es) has been found";
Related
I need some help keeping an array after postback.
On page load I am creating a array that contains three random numbers and sorts them from lowest to highest. These numbers are then used to compare characters stored in another string in the positions of the generated numbers.
When the submit button is hit the numbers are regenerated and the verification fails as now there is a new set of numbers.
here is part of the code ... the array i need to keep is rannumInt.
int[] rannumInt = { 0, 0, 0 };
protected void page_load()
{
if (!IsPostBack)
{
int i, j;
if (rannumInt[0] == 0)
{
for (i = 0; i < 3; i++)
{
Random rannum = new Random();
rannumInt[i] = rannum.Next(1, 9);
if (i > 0)
{
for (j = 0; j < i; j++)
{
if (rannumInt[i] == rannumInt[j])
{
i--;
}
}
}
}
Array.Sort(rannumInt);
Label1.Text = Convert.ToString(rannumInt[0]);
TextBox1.Text = Convert.ToString(rannumInt[0]);
Label2.Text = Convert.ToString(rannumInt[1]);
TextBox2.Text = Convert.ToString(rannumInt[1]);
Label3.Text = Convert.ToString(rannumInt[2]);
TextBox3.Text = Convert.ToString(rannumInt[2]);
}
}
else
{
}
}
You will have to store rannumInt somewhere, like a Session or ViewState. Then when a PostBack occurs, convert the ViewState back to an array.
if (!Page.IsPostBack)
{
if (rannumInt[0] == 0)
{
Array.Sort(rannumInt);
Label1.Text = Convert.ToString(rannumInt[0]);
//save the array into the viewstate
ViewState["rannumInt"] = rannumInt;
}
}
else
{
//check if the viewstate exists
if (ViewState["rannumInt"] != null)
{
//convert it back to an int[]
rannumInt = ViewState["rannumInt"] as int[];
}
}
If you want the number to be the same across multiple pages and/or page reloads, use Session
How can I determine whether Xtragrid in multiselect=True mode check button is selected during the GridviewButton event? See the grid image below:
Solved --
gridView1_SelectionChanged
var view = sender as GridView;
if (view == null) return;
view.BeginSelection();
int[] selectedRows = view.GetSelectedRows();
int[] RowHandle = view.GetSelectedRows();
try
{
for (int i = -1; i < selectedRows.Length; i++)
{
if (selectedRows.Length == 0)
{
clear();
}
else
{
//Clear all value zero
foreach (int value in RowHandle)
{ deger += Convert.ToDouble(gridView1.GetRowCellValue(value, "FieldName")); }
}
}
I need to check whether the checkbox values are ticked or not using 'for' loop.
CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
from searches i got this line. what this tab_patients denote ?`
string chk = "CheckBox";
for (int i = 1; i < 13; i++)
{
for (int k = 1; k < 4; k++)
{
// CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
CheckBox cb = (CheckBox)this.Page.Form.FindControl("chk"+i.ToString());
if(cb.Checked==true)
{
check = i + "0" + k;
}
}}
Make your life easier and create the checkboxes with checkboxlists: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.checkboxlist.aspx
<asp:CheckBoxList ID="SomeElement" runat="server"
AutoPostBack="true"
RepeatColumns="4"
EnableViewState="true">
</asp:CheckBoxList>
With the Repeatcolumns property you are telling the element, that you infact want 4 columns, so the control will devide your 32 elements by 4 and group those checkboxes by the pack of 8.
After this, you can iterate through every list like this:
foreach(var cb in checkboxlistID)
{
if(cb.Checked)
{
//do something
}
}
On top of that, here is some old code showing you how to fill a checkboxlist with data fetched from a SQL-query. You can ignore the if expression, which is checking if there is cached data, from which to fill all the checkboxes.
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
string Query = #"SELECT * from level";
OleDbCommand command = new OleDbCommand(Query, connection);
if (Session["SavedLevelItems"] != null)
{
CheckBoxListLevel.Items.Clear();
List<ListItem> SessionList = (List<ListItem>)Session["SavedLevelItems"];
foreach (var item in SessionList)
{
try
{
CheckBoxListLevel.Items.Add(item);
}
catch { }
}
}
else
{
connection.Open();
CheckBoxListLevel.DataTextField = "bez_level";
CheckBoxListLevel.DataValueField = "id_level";
OleDbDataReader ListReader = command.ExecuteReader();
CheckBoxListLevel.DataSource = ListReader;
CheckBoxListLevel.DataBind();
ListReader.Close(); ListReader.Dispose();
}
}
I'm dynamically binding my grid with a result of linq expression on PageLoad and at HtmlRowPrepared event i'm trying to reach a DataRow with
for (int i = 0; i < grid.GetChildRowCount(visibleGrIndex); i++)
{
var row = grid.GetChildDataRow(visibleGrIndex, i);
}
but its ALWAYS NULL?
HtmlRowPrepared is triggered once for every grid row.
So, you can use this code to fetch data row:
private void Grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e) {
if (e.RowType == GridViewRowType.Group)
{
for (int i = 0; i < GetChildRowCount(e.VisibleIndex); i++)
{
var row = GetChildDataRow(e.VisibleIndex, i);
}
}
}
I am creating an admin Page, in which checkboxlist(User list from DB)is to be created dynamically and its the checked users value is retrieved.
There are different types of users, so it is distinguised groupwise.
Now first a panel is defined, the checkboxlist is created dynamically and placed inside the panel, then the panel is placed inside a Placeholder.
What Iam doing here is placing the checkboxlist inside the panel, then the panel inside the placeholder. So the values of the Checkboxlist is not retrieved, due to the panel it doesnt get the checkboxlist and it doesn't loop through the Checkboxlist.
What I have done is.
private void AddControl(string pUserGrp, int pUserGrp_Id, int pName)
{
CheckBoxList chkList = new CheckBoxList();
CheckBox chk = new CheckBox();
User us = new User();
us.OrderBy = "Order By User_Name";
us.WhereClause = "Where UserRole_Id = " + pUserGrp_Id ;
chkList.ID = "ChkUser" + pName ;
chkList.AutoPostBack = true;
chkList.Attributes.Add("onClick", "getVal(ChkUser" + pName + ");");
chkList.RepeatColumns = 6;
chkList.DataSource = us.GetUserDS();
chkList.DataTextField = "User_Name";
chkList.DataValueField = "User_Id";
chkList.DataBind();
chkList.Attributes.Add("onClick", "getVal(this);");
Panel pUser = new Panel();
if (pUserGrp != "")
{
pUser.GroupingText = pUserGrp ;
chk.Text = pUserGrp;
}
else
{
pUser.GroupingText = "Non Assigned Group";
chk.Text = "Non Assigned group";
}
pUser.Controls.Add(chk);
pUser.Controls.Add(chkList);
Place.Controls.Add(pUser);
}
private void setChecked(int pPageGroupId)
{
ArrayList arr = new ArrayList();
PageMaster obj = new PageMaster();
obj.WhereClause = " Where PageGroup_Id = " + pPageGroupId;
arr = obj.GetPageGroupUserRights(null);
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
if (chkList != null)
{
for (int i = 0; i < chkList.Items.Count; i++)
{
if (arr.Count > 0)
{
int ii = 0;
while (ii < arr.Count)
{
PageMaster oCand = (PageMaster)arr[ii];
if (oCand.User_Name == chkList.Items[i].Text)
{
if (!chkList.Items[i].Selected)
{
chkList.Items[i].Selected = true;
}
}
ii++;
oCand = null;
}
}
}
}
}
public string GetListCheckBoxText()
{
StringBuilder sbtext = new StringBuilder();
foreach (Control c in Place.Controls)
{
if (c.GetType().Name == "CheckBoxList")
{
CheckBoxList cbx1 = (CheckBoxList)c;
foreach (ListItem li in cbx1.Items)
{
if (li.Selected == true)
{
sbtext.Append(",");
sbtext.Append(li.Value);
}
else
{
sbtext.Append(li.Value);
}
}
}
}
return sbtext.ToString(); }
It doesnt get through the Checkboxlist control in the setChecked(), also doesnt loop through the GetListCheckBoxTest().
Anyone can plz help me.
Regards
The problem is that you are trying to find a control (in setChecked) without setting the Name property. You are using this:
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
But where is this in AddControl?
chkList.Name = "ChkUser";
And in GetListCheckBoxText instead of...
if (c.GetType().Name == "CheckBoxList")
...use this:
if (c.GetType()== typeof(CheckBoxList))