C#中的Datalist控件遍历问题
aspx页面中有以下控件:<asp:TextBoxID="MyBox1"runat="server"></asp:TextBox><asp:TextBoxID="MyBo...
aspx页面中有以下控件:
<asp:TextBox ID="MyBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="MyBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label2"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100">
<asp:ListItem Value="0">小学</asp:ListItem>
<asp:ListItem Value="1">中学</asp:ListItem>
<asp:ListItem Value="2">大学</asp:ListItem>
</asp:DropDownList>
<asp:DataList ID="dlNews" runat="server" Width="100%">
<ItemTemplate>
<div id="Div1" class="more_line1" runat="server" >联 系 人:<asp:TextBox ID="MyBox5"
runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "username")%>'></asp:TextBox></div>
<div id="Div2" class="more_line1" runat="server" >公司名称:<asp:TextBox ID="MyBox6"
runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"useradd")%>'></asp:TextBox></div>
</ItemTemplate>
</asp:DataList>
通过下面的代码实现了对TextBox,Label,DropDownList中的text或value值的保存。
Session["Allcontrolvalue"] = null;
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc.ClientID.IndexOf("MyBox") != -1)
{
if (((TextBox)childc).Text!="")
{ Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + ((TextBox)
childc).Text + ","; }
else
{ Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "这个为空" + ",";
}
}
if (childc.ClientID.IndexOf("Label") != -1)
{
Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "标签的值" + ",";
}
if (childc.ClientID.IndexOf("MyButton") != -1)
{
Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "我的按钮" + ",";
}
if (childc.ClientID.IndexOf("DropDownList") != -1)
{
if (((DropDownList)childc).SelectedValue!=null)
{Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + ((DropDownList)
childc).SelectedItem.Value + ",";}
}
}
}
那么在这段代码中,填写怎样的代码,能实现对DataList中的TextBox的text赋值给Session
["Allcontrolvalue"]?? 展开
<asp:TextBox ID="MyBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="MyBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label2"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100">
<asp:ListItem Value="0">小学</asp:ListItem>
<asp:ListItem Value="1">中学</asp:ListItem>
<asp:ListItem Value="2">大学</asp:ListItem>
</asp:DropDownList>
<asp:DataList ID="dlNews" runat="server" Width="100%">
<ItemTemplate>
<div id="Div1" class="more_line1" runat="server" >联 系 人:<asp:TextBox ID="MyBox5"
runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "username")%>'></asp:TextBox></div>
<div id="Div2" class="more_line1" runat="server" >公司名称:<asp:TextBox ID="MyBox6"
runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"useradd")%>'></asp:TextBox></div>
</ItemTemplate>
</asp:DataList>
通过下面的代码实现了对TextBox,Label,DropDownList中的text或value值的保存。
Session["Allcontrolvalue"] = null;
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc.ClientID.IndexOf("MyBox") != -1)
{
if (((TextBox)childc).Text!="")
{ Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + ((TextBox)
childc).Text + ","; }
else
{ Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "这个为空" + ",";
}
}
if (childc.ClientID.IndexOf("Label") != -1)
{
Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "标签的值" + ",";
}
if (childc.ClientID.IndexOf("MyButton") != -1)
{
Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + "我的按钮" + ",";
}
if (childc.ClientID.IndexOf("DropDownList") != -1)
{
if (((DropDownList)childc).SelectedValue!=null)
{Session["Allcontrolvalue"] = Session["Allcontrolvalue"] + ((DropDownList)
childc).SelectedItem.Value + ",";}
}
}
}
那么在这段代码中,填写怎样的代码,能实现对DataList中的TextBox的text赋值给Session
["Allcontrolvalue"]?? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询