当前位置:编程学习 > C#/ASP.NET >>

.net如何获取一组TextBox的值啊,

    <asp:TextBox ID="MainProducts1" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts2" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts3" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts4" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts5" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts6" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts7" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts8" runat="server" Height="20px" Width="100px"></asp:TextBox>
怎么样才能获取这此textbox的值,并且存到一个字段里呀?
--------------------编程问答-------------------- 遍历 页面上的 textbox呗   --------------------编程问答--------------------

        string id = "";
        string strStr="";
        for (int i = 1; i <= 8; i++)
        {
            id = "MainProducts" + i;
            TextBox tbbb= this.FindControl(id) as TextBox;

             strStr += tbbb.Text;
        }
--------------------编程问答-------------------- MainProducts1.text+MainProducts2.text+...

直接+不行吗? --------------------编程问答-------------------- 2楼正解 --------------------编程问答--------------------
引用 2 楼  的回复:
C# code

        string id = "";
        string strStr="";
        for (int i = 1; i <= 8; i++)
        {
            id = "MainProducts" + i;
            TextBox tbbb= this.FindControl(id) as TextB……
这个可以用 --------------------编程问答--------------------
引用 2 楼  的回复:
C# code

        string id = "";
        string strStr="";
        for (int i = 1; i <= 8; i++)
        {
            id = "MainProducts" + i;
            TextBox tbbb= this.FindControl(id) as TextB……

+1 --------------------编程问答-------------------- 遍历指定区域的控件,按类型进行组合处理,打个比方上述的TextBox都在一个Panel里(panel1),即如下处理:

string dynObject=string.Empty;

foreach(Control c in panel1.Controls)
{
    if(c.GetType()==typeof(TextBox))
    {
      dynObject+=string.Format("{0}/",((TextBox)c).Text);
    }
}

dynObject即为值集合,格式为(TextBox1/TextBox2....) --------------------编程问答-------------------- 恩  同意2楼得  这个方法可行 --------------------编程问答--------------------
引用楼主  的回复:
    <asp:TextBox ID="MainProducts1" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:TextBox ID="MainProducts2" runat="server" Height="20px" Width="100px"></asp:TextBox>
    <asp:T……

TextBox[] txarray;
然后用Page.FindControl(),取出来放到txarray里 --------------------编程问答--------------------
Page.FindControl()方法可以。。。

遍历控件,判断是否是textbox可以

也可以用JS或Jquery

给TextBox相同的Name,比如:name="txtAbc"


$("[name='txtAbc']").each(function(){
    alert($(this).val());  //$(this).val()是每个值
});

--------------------编程问答-------------------- 2楼的靠谱 --------------------编程问答-------------------- 用控件遍历!如果TextBox是动态增加的用2楼的方法不可取!
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,