如何通过Arraylist进行迭代 [英] how to iterate through through Arraylist
本文介绍了如何通过Arraylist进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的WebMethod会像这样返回arraylist:
[WebMethod]
public static ArrayList ProcessIT( string strpid )
{
DataTable dt = new DataTable();
ArrayList list = new ArrayList();
string queryString = select * from Registration其中RegNo =' + strpid + '和Deleted = 1;
使用(SqlConnection con = new SqlConnection( Server = op- maurya; Database = DishaEyeHospitals; User ID = sa; Password = Password123; Trusted_Connection = False;))
{
使用(SqlCommand cmd = new SqlCommand(queryString,con))
{
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
list.Add(DR);
}
}
}
return list;
}
它工作正常,但我想在 javascript $中迭代此列表 b $ b < script >
function getpid()
{
var _pid = document.getElementById(' < ;%= txtpid.ClientID%>')。 value ;
alert(_pid);
PageMethods.ProcessIT(_pid,onSucess,onError);
函数onSucess(结果)
{
for ( var i 结果)
{
alert(response [i]);
}
返回 true ;
}
函数onError(结果)
{
alert( 错误跨度>);
for ( var i in 结果)
{
alert(response [i]);
}
返回 false ;
}
}
< / script >
解决方案
这看起来像C#,而不是JavaScript。 :-)
如果是这样,你永远不应该使用ArrayList
。早在第2版引入泛型时,这种类型就已经过时了。形式上,该类没有标记[Obsolete]
属性,因为将它留在某些遗留代码中没有任何问题,但在新开发中它从未有任何意义。改为使用System.Collections.Generic.List<>
。
迭代完全按照你的方式完成与您的数据行:
System.Collections.Generic.List< SomeType> list = new System.Collections.Generic.List< SomeType>();
// ...
foreach (SomeType item in list){ / * ... * / }
此代码可以通过类型推断缩短:
var list = new System.Collections.Generic.List< SOMETYPE>();
// ...
foreach ( var item in list){ / * ... * / }
-SA
Hi,
My WebMethod returns arraylist like this:
[WebMethod]
public static ArrayList ProcessIT(string strpid)
{
DataTable dt = new DataTable();
ArrayList list = new ArrayList();
string queryString = "select * from Registration where RegNo='"+strpid+"' and Deleted=1";
using (SqlConnection con = new SqlConnection("Server=op- maurya;Database=DishaEyeHospitals;User ID=sa;Password=Password123;Trusted_Connection=False;"))
{
using(SqlCommand cmd = new SqlCommand(queryString, con))
{
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
list.Add(dr);
}
}
}
return list;
}
it work fine but i want to iterate this list in javascript
<script>
function getpid()
{
var _pid=document.getElementById('<%=txtpid.ClientID %>').value;
alert(_pid);
PageMethods.ProcessIT(_pid,onSucess,onError);
function onSucess(result)
{
for (var i in result)
{
alert(response[i]);
}
return true;
}
function onError(result)
{
alert("Error");
for (var i in result)
{
alert(response[i]);
}
return false;
}
}
</script>
解决方案
This looks like C#, not JavaScript. :-)
If so, you should never useArrayList
. This type was rendered obsolete as early as by v.2.0, when generics were introduced. Formally, the class wasn't marked with[Obsolete]
attribute only because there is nothing wrong with leaving it in some legacy code, but in new development it never makes any sense. UseSystem.Collections.Generic.List<>
instead.
The iteration is done exactly as you did with your data rows:
System.Collections.Generic.List<SomeType> list = new System.Collections.Generic.List<SomeType>(); //... foreach (SomeType item in list) { /* ... */ }
This code can be shortened with type inference:
var list = new System.Collections.Generic.List<SomeType>(); //... foreach (var item in list) { /* ... */ }
—SA
这篇关于如何通过Arraylist进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文