从HttpRequest获取数据
在使用用户自定义控件(UserControl)时,经常需要在主页面(page)的Page_Load里面获取UserControl控件中填写的数据,这个时候的UserControl是没有执行Page_Load的,所以不能通过常用的方式获得数据。但是由于用户提交的数据都存放在了HttpRequest中,所以可以从其中取得你想要的数据。
下面给出遍历Request中数据的方法。(from msdn)
int loop1, loop2;
System.Collections.Specialized.NameValueCollection coll;
coll=Request.Form;//或者 coll=Request.Params
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
string s = arr2[loop2];
Response.Write("Value " + loop2 + ": " + s + "<br>");
}
}
从这个方法你可以查看到你的page和UserControl中提交数据,以及他们的变化。
UserControl中的TextBox类型,在Request的Key中一定出现,是这样存储的:UserControl_ID:TextBox1。它的Value不一定存在。
但是UserControl中的Button和ImageButton只有当他们被点击触发事件的时候才会在Key中出现。其中Button的形式和TextBox一样,Value则是其文本信息。而ImageButton的形式则是UserControl_ID:ImageButton1.x和UserControl_ID:ImageButton1.y两个,他们的值是数字,应该是记录其位置的。
通过这些信息我们可以在UserControl的Page_Load之前,取得UserControl的数据。
下面给出遍历Request中数据的方法。(from msdn)
int loop1, loop2;
System.Collections.Specialized.NameValueCollection coll;
coll=Request.Form;//或者 coll=Request.Params
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
string s = arr2[loop2];
Response.Write("Value " + loop2 + ": " + s + "<br>");
}
}
从这个方法你可以查看到你的page和UserControl中提交数据,以及他们的变化。
UserControl中的TextBox类型,在Request的Key中一定出现,是这样存储的:UserControl_ID:TextBox1。它的Value不一定存在。
但是UserControl中的Button和ImageButton只有当他们被点击触发事件的时候才会在Key中出现。其中Button的形式和TextBox一样,Value则是其文本信息。而ImageButton的形式则是UserControl_ID:ImageButton1.x和UserControl_ID:ImageButton1.y两个,他们的值是数字,应该是记录其位置的。
通过这些信息我们可以在UserControl的Page_Load之前,取得UserControl的数据。
浙公网安备 33010602011771号