怎么在前台(aspx页)获取datalist中的控件值,在线等。。

解决方案 »

  1.   

    asp.net夜话之八:数据绑定控件
    在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。
      

  2.   

    我一般的做法是现看HTML源代码,看生成的INPUT的ID,一般是你的datalist的ID加上数字来,所以看到规律你就可以用document.getElementById来做了
      

  3.   

    //当绑定DataList1中的每一项时的处理方法 
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
        { 
            //如果要绑定的项是交替项或者是普通项 
            //注意此外还有脚模板和脚模版 
            if (e.Item.ItemType == ListItemType.Item || 
                 e.Item.ItemType == ListItemType.AlternatingItem) 
            { 
                //e.Item表示当前绑定的那一行 
                //利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件 
                Label lbSex = (Label)(e.Item.FindControl("Label1")); 
                //利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件 
                DataList dl2 = (DataList)(e.Item.FindControl("DataList2")); 
                bool male = bool.Parse(lbSex.Text); 
                dl2.DataSource = GetDataTable(male); 
                dl2.DataBind(); 
            } 
        } 
    DataList有Items属性,这个属性表示获取表示控件内单独项的 DataListItem 对象的集合。
      

  4.   

    void Button_Click(Object sender, EventArgs e)
          { 
             if (DataList1.Items.Count > 0)
             {
                Label1.Text = "The Items collection contains: <br />";            foreach(DataListItem item in DataList1.Items)//对DataList的项进行遍历
                {               Label1.Text += ((DataBoundLiteralControl)item.Controls[0]).Text +
                                  "<br />";
                }
             }
          }
      

  5.   

    document.getElementById具体怎么弄啊 。。
      

  6.   

    我想实现的功能是   
    在datalist的项模板里添加了一个表格 
    表格为一行两列 
    第一列是绑定数据 
    第二列的数据是根据第一列的值去查找一个表的某列值
      

  7.   

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DataList ID="dl" runat="server">
        <AlternatingItemTemplate>
        <div>Customers ||    CustomerID:  <%#Eval("CustomerID")%>&nbsp;&nbsp;&nbsp;&nbsp;CompanyName:  <%#Eval("CompanyName") %>
        </div>
        <div>
        Orders ||   ShipName:  <%# GetShipNameByCustomerID(Eval("CustomerID"))%>
        </div>
        </AlternatingItemTemplate>
        <SeparatorTemplate>
        </SeparatorTemplate>
        <ItemTemplate>
        <div>Customers ||    CustomerID:  <%#Eval("CustomerID")%>&nbsp;&nbsp;&nbsp;&nbsp;CompanyName:  <%#Eval("CompanyName") %>
        </div>
        <div>
        Orders ||   ShipName:  <%# GetShipNameByCustomerID(Eval("CustomerID"))%>
        </div>
        </ItemTemplate>
        </asp:DataList>
        </form>
    </body>
    </html>
      

  8.   


    protected object GetShipNameByCustomerID(object CustomerID)
        {
            object result = null;
            using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind"))
            {
                SqlDataAdapter sda = new SqlDataAdapter("select top 1 ShipName from Orders where CustomerID=@CustomerID", con);
                sda.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", CustomerID));            DataSet ds = new DataSet();
                sda.Fill(ds, "ShipName");
                if (ds.Tables.Contains("ShipName") && ds.Tables["ShipName"].Rows.Count > 0)
                {
                    result = ds.Tables["ShipName"].Rows[0][0];
                }
            }
            return result;
        }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind"))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("select top 10 * from Customers", con);
                    DataSet ds = new DataSet();
                    sda.Fill(ds, "Customers");
                    if (ds.Tables.Contains("Customers"))
                    {
                        dl.DataSource = ds.Tables["Customers"].DefaultView;
                        dl.DataBind();
                    }
                }        }
        }
      

  9.   

    使用的是 Northwind 数据库
      

  10.   

    <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" BorderColor="Gray"
                            BorderStyle="Double" BorderWidth="1px">
                            <ItemTemplate>
                                <table>
                                    <tr>
                                        <td style="width: 100px; height: 244px" valign="top">
                                            &nbsp;<table>
                                                <tr>
                                                    <td style="width: 100px" valign="top">
                                                        <div class="lt-zl_2">
                                                            <asp:Image ID="Image1" runat="server" Height="78px" Width="80px" />&nbsp;<br />
                                                            <asp:LinkButton ID="lkbtnRname" runat="server" ForeColor="Black" Text='<%# Eval("reply_username") %>'></asp:LinkButton><br />
                                                            <asp:Label ID="lbRurl" runat="server" Text='<%# GetShipNameByCustomerID(Eval("reply_id")) %>' Visible="false"></asp:Label>
                                                            <br />
                                                            <asp:Label ID="lbid" runat="server" Text='<%# Eval("reply_id") %>'></asp:Label></div>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td style="width: 100px; height: 244px" valign="top">
                                            <div class="lt-zr_2">
                                                <%--<%# GetShipNameByCustomerID(Eval("reply_id")) %>--%>
                                                <% 
                                                    string Rurl = Convert.ToString(Eval("reply_content"));
                                                    Response.ContentType = "text/HTML";
                                                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                                                    Response.WriteFile(Rurl);
                                                    Response.Write(Rurl);
                                                     %>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:DataList>&nbsp;
    我想在<% 
                                                    string Rurl = Convert.ToString(Eval("reply_content"));
                                                    Response.ContentType = "text/HTML";
                                                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                                                    Response.WriteFile(Rurl);
                                                    Response.Write(Rurl);
                                                     %>
    这里输出文件  但是string Rurl = Convert.ToString(Eval("reply_content"));要出错
    那应该怎么做啊。。?
      

  11.   

    就是输出文件到指定位置 我的文件 是html文件 我知道用<%%>可以实现 
    输出文件到datalist的指定位置
    但问题是每一行都要输出不同的文件  该文件是根据每一行绑定的一个id来输出不同的文件那位 帮帮忙..
      

  12.   

    就是输出文件到指定位置 我的文件 是html文件 我知道用 <%%>可以实现
    输出文件到datalist的指定位置
     我只能在每一行输出相同的文件我想 在每一行都要输出不同的文件  该文件是根据每一行绑定的一个id来输出不同的文件那位 帮帮忙..
      

  13.   

    进来看看, 我是在 aspx.cs中获取的   foreach(...)
    前台用javascript遍历 datalist 吧
    o(∩_∩)o...哈哈
      

  14.   


    <%--<script type="text/javascript">
        //获取GridView2中TextBox的值
        onload = function ForeachGridView()
        {
            var gridview = document.getElementById("<%=GridView2.ClientID %>");
            for(int i=0;i<girdview.rows.length;i++)
            {
               var sortID = gridview.rows[i].cell[0].getElementsByTagName("CP_SortID")[4];
               alert(sortID.value);
            }
        }
    </script>--%>
    ...
      

  15.   

    lakeq  你好 如果是datalist怎么获取TextBox的值