How to store an array and make it available after postback - asp.net

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

Related

Retrieving images from Database into ArrayList and Displaying it one by one from ArrayList in ASP.NET

using following code i store images into ArrayList.
public ArrayList getImagesToArray(string qry)
{
ArrayList arr;
if (myConn.State == System.Data.ConnectionState.Closed)
myConn.Open();
if (myDR != null)
myDR.Close();
myComm = new SqlCommand(qry, myConn);
using (myComm)
{
arr = new ArrayList();
myDR = myComm.ExecuteReader();
while (myDR.Read())
{
arr.Add((byte[])myDR[0]);
}
return arr;
}
}
Now i retreive images to an ArrayList using Following Code
ArrayList arrImgs_Main = Q.getImagesToArray("Select FieldImg from InfoTagQA I INNER JOIN PensionScannedDocs P on p.ScanImageID=i.ScanImageID Where P.PersonalNumber='" + hid_PersonalNo.Value + "'");
Now I display image one by one from arrayList to ImageUrl by clicking Next and Previous button
protected void lnk_BackImage_Click(object sender, EventArgs e)
{
try
{
if (arrImgs_Main.Count > 0)
{
if (i > 0)
{
i--;
img_mainImage.ImageUrl = "data:image;base64," + Convert.ToBase64String((byte[])arrImgs_Main[i]);
lab_TotalRecords.Text = "(" + (i + 1) + "/" + arrImgs_Main.Count + ")";
if (i == 0)
{
lnk_ForwardImage.Visible = true;
lnk_BackImage.Visible = false;
}
}
}
}
catch(Exception ee)
{
ShowErrorMsg(ee.Message);
}
}
**All above code works fine and quick on my local machine, but after deployment on server , it take much time in displaying image one by one from array.
Am i doing doing right in above code or going on wrong way. Please guide me **

Devexpress MultiSelect Row Field Name .?

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

ASPxGridView.GetChildDataRow is always null

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

Multiple Textbox values into database

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";

Dynamically add checkboxlist into placeholder and get the checked value of the checkboxlist

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

Resources