如何在运行时将我的datalist的数据源设置为datatable

时间:2012-12-25 08:52:54

标签: asp.net

我的页面中有两个数据列表,其中一个嵌套在另一个中。 我想在页面加载事件期间在运行时给出他们的数据源,但是我的内部数据列表没有显示数据表的值,我不知道如何将我的内部数据列表数据源设置为数据表运行时间。

我已经搜索了很多关于它但我找不到有用的信息。 请帮帮我。

在页面中,我想显示哪位用户购买了哪些产品,显示每种产品的数量和价格,并显示有关付费索引的一些信息。像索引号或索引库名一样。 问题是,对于一个索引号,我可以有几个产品。为了显示每个索引号的相关产品,我使用了嵌套数据列表。 在我的第一个数据列表中,我的外部(父)数据列表可以显示信息,但应显示产品信息的内部数据列表不显示任何内容。

我真的需要帮助

这是我的代码:

  <asp:DataList ID="outerDataLiat" runat="server" RepeatColumns="1" 
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"    BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both"  DataKeyField="idfromIndex" OnItemDataBound="DataList1_ItemDataBound" />
            <FooterStyle BackColor="#CCCCCC"  />;
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <ItemStyle BackColor="White" />
        <ItemTemplate />           
    <fieldset>

    <legend&gt>customer information&lt</legend&gt>
    <table style="width: 100%;"/>
     <tr>
      <td>
     &nbsp;customer code:
    </td>
     <td>
                    &nbsp;<asp:Label ID="Label1" runat="server" Text='<%# Eval("idFromUsers") %>'</asp:Label>
             </td>;

                 <td>
                    &nbsp;telephone:
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Eval("user_phone") %>'></asp:Label>
                </td>

            </tr>
            <tr>
                <td>
                    &nbsp; customer name :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label3" runat="server" Text='<%# Eval("user_name") %>'></asp:Label>
                </td>
                <td>
                    &nbsp;cellphone number:
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label4" runat="server" Text='<%# Eval("user_cellphone") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp; customer familly name :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label5" runat="server" Text='<%# Eval("user_familly") %>'></asp:Label>
                </td>
                <td>
                    &nbsp;email :
                </td>
                <td>
                   &nbsp;<asp:Label ID="Label6" runat="server" Text='<%# Eval("user_email") %>'></asp:Label>
                </td>
            </tr>

        </table>

    </fieldset>
    <fieldset>
    <legend>Index Information</legend>
      <table style="width: 100%;">
          <asp:Label ID="hiddenLable" runat="server" Text='<%# Eval("idfromIndex") %>' Visible="false"></asp:Label>
            <tr>
                <td>
                    &nbsp;Index number :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label7" runat="server" Text='<%# Eval("IndexNO") %>'></asp:Label>
                </td>
                <td>
                    &nbsp;date of index :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label8" runat="server" ></asp:Label>
                </td>
                <td>
                    &nbsp;bank name:
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label9" runat="server" Text='<%# Eval("bankName") %>'></asp:Label>
                </td>
            </tr>

        </table>

    </fieldset>
    <fieldset>
    <legend>purchased products</legend>
    <asp:DataList ID="innerDataLiat" runat="server" RepeatColumns="1" 
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" OnItemDataBound="DataList2_ItemDataBound"  >
            <FooterStyle BackColor="#CCCCCC"  />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="White" />
        <ItemTemplate >
        <table style="width: 100%;" id="tbl1"></table>
       <table style="width: 100%;">
            <tr>
                <td>
                    &nbsp;product name :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label10" runat="server" Text=""></asp:Label>
                </td>
                <td>
                    &nbsp;quantity:
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label11" runat="server" Text=""></asp:Label>
                </td>
                <td>
                    &nbsp;unit price of each product :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label12" runat="server" Text=""></asp:Label>
                </td>
               <td>
                    &nbsp;Total Amount:
                </td>
                <td>
                &nbsp;<asp:Label ID="Label13" runat="server" Text=""></asp:Label>

                </td>
            </tr>


        </table>
                   </ItemTemplate> 

        </asp:DataList>

    </fieldset>
    <fieldset><legend>shopping cart Information</legend>
        <table style="width: 100%;">
            <tr>
                <td >
                    &nbsp;Total quantity :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label14" runat="server" Text=""></asp:Label>
                </td>
              <td>
                    &nbsp;total amount of shopping cart:
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label15" runat="server" Text=""></asp:Label>
                </td>

            </tr>
            <tr>
            <td> &nbsp;date of shopping :</td>
            <td dir="rtl">&nbsp<asp:Label ID="Label16" runat="server" Text='<%# Eval("orderDate") %>'  /></td>
            </tr>

        </table>
        </fieldset>


          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="confirm">confirm</asp:LinkButton>
          &nbsp;
          <asp:LinkButton ID="LinkButton2" runat="server" CommandName="notconfirm">not confirm</asp:LinkButton>
        </ItemTemplate>

            <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

        </asp:DataList>


    protected void Page_Load(object sender, EventArgs e)
   {
    if (!IsPostBack)
    {
        getDistinctOrderId();
        doPaging();
    }     
 }
private void getDistinctOrderId()
{
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = myConnection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT min(id), [idfromIndex] FROM Orders WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM Orders) GROUP BY [idfromIndex] ";
    DataView MyDv = new DataView();

    try
    {
        myConnection.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        MyDv = dt.DefaultView;
        for (int i = 0; i &lt; MyDv.Count; i++)
        {
            if (i == MyDv.Count - 1)
            {
                distinctList += MyDv[i][0].ToString();
            }
            else
                distinctList += MyDv[i][0].ToString() + ",";
        }

    }
    catch (Exception EXP)
    {
    }
    finally
    { myConnection.Close(); }
}
 public DataTable getTheData()
 {

 DataSet DS = new DataSet();
 SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
        SqlDataAdapter objSQLAdapter = new SqlDataAdapter("select idfromIndex,user_name,user_email,indexNO,bankName,Ispay,user_familly,user_cellphone,orderDate,user_phone,idFromUsers,orderId from factors where orderId IN (" + distinctList + ")", myConnection); objSQLAdapter.Fill(DS, "mobile_store");

    return DS.Tables[0];
 }

  void doPaging()
  {
    //DataTable mydatatable = 
    pagedData.DataSource = getTheData().DefaultView;
    pagedData.AllowPaging = true;
    pagedData.PageSize = 4;

 /* 
 ...
   here I wrote some code to make my outer datalist pagable
 ...
*/


    outerDataLiat.DataSource = pagedData;
    outerDataLiat.DataBind();

}

  protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
 {
    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        Label lbl = (Label)e.Item.FindControl("hiddenLable");
        string idfromindex = lbl.Text;
        DataList lst2 = (DataList)e.Item.FindControl("innerDataLiat");
        SqlDataSource ds = new SqlDataSource();
        DataTable mydt = new DataTable();

        dataset t = setinnerDataList(idfromindex);
        tb2 = t;
        int sumqty = 0;
        double sumprice = 0;
        for (int i = 0; i &lt; t.Rows.Count; i++)
        {
            sumprice += Convert.ToDouble(t.Rows[i]["p_price"]) * Convert.ToInt32(t.Rows[i]["qty"]);
             sumqty += Convert.ToInt32(t.Rows[i]["qty"]);
        }
        Label lbl14 = (Label)e.Item.FindControl("Label14");
        lbl14.Text = sumqty.ToString();
        Label lbl15 = (Label)e.Item.FindControl("Label15");
        lbl15.Text = sumprice.ToString();

          Label pname = (Label)lst2.FindControl("pname");
        Label qty = (Label)lst2.FindControl("qty");
        Label price = (Label)lst2.FindControl("unit_price");
        DataRow[] dr = t.Tables[0].Select("idfromIndex = '" + idfromindex + "'");//new DataRow[t.Tables[0].Rows.Count]; //=  //This will select all rows where the Name Column has value MAK
        for (int i = 0; i < t.Tables[0].Rows.Count; i++)
        {
            dr[i] = t.Tables[0].Rows[i];
        }
        for (int i = 0; i < dr.Length; i++)
        {
            pname.Text = dr[i]["pname"].ToString();
            price.Text = dr[i]["p_price"].ToString();
            qty.Text = dr[i]["qty"].ToString();
        }

    }
}

  private dataset setinnerDataList(string idfromindex)
 {

    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = myConnection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT [pname], [p_price], [idfromIndex], [qty] FROM [factors] WHERE ([idfromIndex] = " + idfromindex + ") ";
    DataView MyDv = new DataView();
    dataset dt = new DataTable();
    try
    {
        myConnection.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        dt.Load(dr);
        dr.Close();

    }
    catch (Exception EXP)
    {


    }
    finally
    { myConnection.Close(); }

    return dt;
 }</pre>

3 个答案:

答案 0 :(得分:0)

您尚未为内部DataList2_ItemDataBound的{​​{1}}事件定义ItemDataBound处理程序。您还需要设置内部DataList的控制值,就像在DataList处理程序中一样。

编辑:在下面添加了更多详细信息:

DataList1_ItemDataBound

答案 1 :(得分:0)

您必须为外部DataList 实施 OnItemDataBound 方法,以便您可以将 dataSource分配给内部DataList < / p>

<asp:DataList ID="outerDataList" runat="server" OnItemDataBound="outerRep_ItemDataBound">

在代码中你必须这样做

protected void outerRep_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            DataList innerDataList = e.Item.FindControl("innerDataList") as DataList;
            innerDataList.DataSource = drv.CreateChildView("CategoriesRelation");
            innerDataList.DataBind();
        }
    }

有关详情,请参阅Nested DataList

的工作示例

希望这会帮助你。

答案 2 :(得分:0)

  

感谢AYK的帮助,当我以这种方式改变它时工作:

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
   {
   if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        DataList dls1 = (DataList)sender;
        Label lbl = (Label)e.Item.FindControl("hiddenLable");
        string idfromindex = lbl.Text;
        DataList lst2 = (DataList)e.Item.FindControl("innerDataLiat");
        Panel pnl = (Panel)e.Item.FindControl("Panel1");           
         t = setinnerDataList(idfromindex);
        int sumqty = 0;
        double sumprice = 0;


        for (int i = 0; i < t.Tables[0].Rows.Count; i++)
        {
            sumprice += Convert.ToDouble(t.Tables[0].Rows[i]["p_price"]) * Convert.ToInt32(t.Tables[0].Rows[i]["qty"]);
            sumqty += Convert.ToInt32(t.Tables[0].Rows[i]["qty"]);

        }
        Label lbl14 = (Label)e.Item.FindControl("Label14");
        lbl14.Text = sumqty.ToString();
        Label lbl15 = (Label)e.Item.FindControl("Label15");
        lbl15.Text = sumprice.ToString();
        lst2.DataSource = t;
        lst2.DataBind();
     }
    }  

       <asp:DataList ID="innerDataLiat" runat="server" RepeatColumns="1" 
            BackColor="#CCCCCC" 
            CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" OnItemDataBound="DataList2_ItemDataBound"  >
            <FooterStyle BackColor="#CCCCCC"  />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="White" />
        <ItemTemplate >
        <table style="width: 100%;" ></table>
        <table style="width: 100%;" id="tbl1">
            <tr>
                <td>
                    &nbsp;product name:
                </td>
                <td>
                    &nbsp;<asp:Label ID="pname" runat="server" Text='<%# Eval("pname") %>'></asp:Label>
                </td>
                <td>
                    &nbsp;quantity :
                </td>
                <td>
                    &nbsp;<asp:Label ID="qty" runat="server" Text='<%# Eval("qty") %>'></asp:Label>
                </td>
                <td>
                    &nbsp;unit price:
                </td>
                <td>
                    &nbsp;<asp:Label ID="unit_price" runat="server" Text='<%# Eval("p_price") %>' ></asp:Label>
                </td>

            </tr>


        </table>
                   </ItemTemplate> 

        </asp:DataList>