2014年7月13日 星期日

[RESOLVED] send only checked items


Hi,


I have 20 checkboxes on my page,


I want tht when I click on the send button, only checked checkboxes send to another page.


they will be send to a grid view


actually I have 20 products on the page, these 20 checkboxes check by user to do compare operation


how can I do this?



Can you show your code?  I have a solution but I have to know the fields you are using.  Please include your aspx and code behind page (vb/cs)



How are your currently handling this?


You can grab all of your Checked CheckBox elements within your code-behind (assuming they are CheckBox controls) as follows :


// Get all of your checked checkboxes
List checkedCheckboxes = Controls.Cast().Where(c => c.Checked).ToList();

I'm not sure how you plan on passing these values across or which sections you need of them (such as the values) but this will get you all of the ones that are checked. If you just wanted to store all of the checked values into an array, you could
probably use something like :


// Get the checked values of all of your checkboxes and store them in an array
var checkedValues = Controls.Cast().Where(c => c.Checked).Select(v => v.Text).ToArray();

Any additional code that you could provide would be greatly appreciated.







Rion Williams



You can grab all of your Checked CheckBox elements within your code-behind (assuming they are CheckBox controls) as follows :


// Get all of your checked checkboxes
List checkedCheckboxes = Controls.Cast().Where(c => c.Checked).ToList();




Yes, that is similar to what I was going to recommend Rion :) 



I thought my code will make u confuse tht's why I asked the question totally,


becoz I also got confused by this problem,


assume tht u have some products on the page,


for these products u put checkbox,ok


for compare operation, user checkes desire checkbox


after tht clicks on the compare button


choosed items ID will be send to gridview to another page


(I've made the poviting gridview, so I can't manipulate it manually, checkboxes ID must be send to a string tht will be used as sqlcommand string)


or any solution u think tht is more right.


one more problem is my sql command must be this structure:


select * from cars_gps where name='gps avl02'

name=checkbox ID

if more checkboxes choosed it have to be like this:


select * from cars_gps where name='gps avl02' or name='gps mvt340'

with grabing all in list box will it work?



Well, I'm sure that your code probably won't confuse Rion or me as we have both been in the .net programming field for years.  Try us.



ok :)


 I declare a static variable to save checkboxes ID on it in a static class


Global.cs


public static class Global
{
public static string strSearch = "";
}

index.aspx


20 of this:



GPS AVL02

add to PostBackUrl="~/compare.aspx">compare page


index.aspx.cs


protected void cb_AVL02_CheckedChanged(object sender, EventArgs e)
{
if(cb_AVL02.Checked==true)
{
Global.strSearch = Global.strSearch + cb_AVL02.ID.ToString();

}
}


I have a simple gridview control in compare.aspx


compare.aspx.cs


public void FillGridView(string StrSearch)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(StrSearch, con);
DataSet ds = new DataSet();
da.Fill(ds, "car_detector");
GridView1.DataSource = FlipDataSet(ds);
GridView1.DataBind();
con.Close();
}

public DataSet FlipDataSet(DataSet my_DataSet)
{
DataSet ds = new DataSet();
DataTable table = new DataTable();
DataRow r = null;
foreach (DataTable dt in my_DataSet.Tables)
{

for (int i = 0; i <= dt.Rows.Count; i++)// add columns as much as rows count
{
table.Columns.Add(Convert.ToString(i));
}
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
r[j] = dt.Rows[j - 1][k];
table.Rows.Add(r);
}
ds.Tables.Add(table);
}

return ds;
}


I think my way is wrong,coz have to make some more things static,1st I want to know how to transmit checkboxes


if u show me another solution totally I will be thankful


list box also works like this way I think


without noticing to my way, I want to know if u want to do like this work, what way will u choose?


thank u for spending time for my question :)



First, don't use a static class to share the checkboxes. The reason, this is shared among each and every user on the system so everyone will see the exact same copy of it. There are three easy ways to do this.


1) Querystring.

you can concatenate the id's into a seperated string and pass them to another page using a querystring variable, read the string on the destination page and use the string.split() function to split your delimitted string into an array.


2) Session

You could do the same thing with a session, but with a strongly typed List which would make it more convenient to iterate through and more flexible if you need to manipulate it or query it (ie, use linq to re-order it etc.). Only problem with session is
it's unreliable unless you are using a database as the session store since it's stored in memory (ends up going away). 


3) Cookies

Another tried and true method is to just store the list into a cookie for easy reference without working about messing up the querystring or the frailty of cookies.



thank u for solutions.


can u show me a working example with any option u think tht is more easy?


as I know cookies and querystrings are client-side operation and session is server-side


so if my data needs to be secure I have to use session, some operation like internet shoping, right?


then for transmiting checkboxes it dosent matter which option I use, right?


and my another question is can I mix multiple choice of these 3 options?will any problem happen?any restriction?


沒有留言:

張貼留言