如何在运行时将我的datalist的数据源设置为datatable [英] How can I set my datalist 's datasource to datatable at runtime

查看:113
本文介绍了如何在运行时将我的datalist的数据源设置为datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面中有两个数据列表,其中一个嵌套在另一个数据列表中。

我想在页面加载事件期间在运行时设置数据源,但我的内部数据list没有显示数据表的值,我不知道如何在运行时将内部数据列表数据源设置为数据表。



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

请帮帮我。



在页面中我想显示哪位用户购买了哪些产品并显示每种产品的数量和价格并显示有关付费索引的一些信息,如索引号或索引银行名称。

问题是,对于一个索引号我可以有几个产品而且我不知道每个索引的确切产品数量因为我使用内部datalist来显示与同一索引相关的产品信息,并且我没有对我的内部数据库进行任何控制,因为我不知道每个索引的确切产品数量所以我必须将我的内部数据源数据源设置为datatable运行时,但我的内部datalist只显示空表。

但(父)datalist运行良好



我真的需要帮助



这是我的代码:

I have two data lists in my page and one of them is nested in the other one.
I would like to set their data sources in run time during page load event, but my inner data list doesn''t show the value of the data table and I don''t know how to set my inner data lists data source to data table in run time.

I have searched a lot about it but I couldn''t find useful information.
Please help me.

In the page I would like to show which user bought which products and show the quantity and price of each product and show some information about paid index, Like index number or index bank name.
The problem is, for one index number I can have several products and I dont know the exact number of products of each index beacause of that I used inner datalist to show products information which are related to the same index and I did not put any control in my innerdatalist because I dont know the exact number of products for each index so I have to set my innerdatalist datasource to datatable at runtime but my inner datalist only show the empty table.
but the (parent)datalist works well

I really need help

This is my code:

<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>customer information</legend>
      <table style="width: 100%;">
        <tr>
        <td>
          customer code:
        </td>
        <td>
                         <asp:Label ID="Label1" runat="server" Text='<%# Eval("idFromUsers") %>'></asp:Label>
                    </td>
                    
                     <td>
                         telephone:
                    </td>
                     <td>
                         <asp:Label ID="Label2" runat="server" Text='<%# Eval("user_phone") %>'></asp:Label>
                    </td>

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

        </fieldset>
        <fieldset>
        <legend>index card information</legend>
          <table style="width: 100%;">
              <asp:Label ID="hiddenLable" runat="server" Text='<%# Eval("idfromIndex") %>' Visible="false"></asp:Label>
                <tr>
                    <td>
                         index number :
                    </td>
                    <td>
                         <asp:Label ID="Label7" runat="server" Text='<%# Eval("IndexNO") %>'></asp:Label>
                    </td>
                    <td>
                         index date :
                    </td>
                    <td>
                         <asp:Label ID="Label8" runat="server" ></asp:Label>
                    </td>
                    <td>
                         bank name:
                    </td>
                    <td>
                         <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>
          
                       </ItemTemplate> 

            </asp:DataList>
         
        </fieldset>
        <fieldset><legend>shopping cart information</legend>
            <table style="width: 100%;">
                <tr>
                    <td >
                         Total Quantity of purchased products :
                    </td>
                    <td>
                         <asp:Label ID="Label14" runat="server" Text=""></asp:Label>
                    </td>
                  <td>
                         Total price:
                    </td>
                    <td>
                         <asp:Label ID="Label15" runat="server" Text=""></asp:Label>
                    </td>
                    
                </tr>
                <tr>
                <td>  date of purchase:</td>
                <td dir="rtl"> <asp:Label ID="Label16" runat="server" Text='<%# Eval("orderDate") %>'  /></td>
                </tr>
                
            </table>
            </fieldset>

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

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

            </asp:DataList>





This is my C# code:



This is my C# code:

  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 < 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];
    }
//this method is for paging my outer datalist
 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();
        
            DataTable t = setinnerDataList(idfromindex);
            tb2 = t;
            int sumqty = 0;
            double sumprice = 0;
            for (int i = 0; i < 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();
           
            lst2.DataSource = t;
            lst2.DataBind();
           
        }
    }

private DataTable 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();
        DataTable dt = new DataTable();
        try
        {
            myConnection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
           
            dt.Load(dr);
            dr.Close();

        }
        catch (Exception EXP)
        {


        }
        finally
        { myConnection.Close(); }
       
        return dt;
    }





Is there any one to help me I really need help



Is there any one to help me I really need help

推荐答案

I think you are not getting proper datasource for inner datalist. debug it properly.



Check this code:

I think you are not getting proper datasource for inner datalist. debug it properly.

Check this code:
<asp:DataList runat="server" ID="dl1" OnItemDataBound="dl1_ItemDataBound">
    <ItemTemplate>
        <table width="100%" border="1">
            <tr>
                <td>'<%#Eval("first") %>'</td>
                <td>'<%#Eval("last") %>'</td>
            </tr>
        </table>
        <asp:DataList runat="server" ID="dl2">
            <ItemTemplate>
                <table width="100%" border="1">
                    <tr>
                        <td>'<%#Eval("d1") %>'</td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </ItemTemplate>
</asp:DataList>










dl1.DataSource = Getdl1();
        dl1.DataBind();







private DataTable Getdl1()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("first"), new DataColumn("last") });
    dt.Rows.Add("first1", "last1");
    dt.Rows.Add("first2", "last2");
    dt.Rows.Add("first3", "last3");
    dt.Rows.Add("first4", "last4");
    dt.Rows.Add("first5", "last5");
    return dt;
}
private DataTable Getdl2()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("d1") });
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    return dt;
}
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList dl = (DataList)e.Item.FindControl("dl2");
    dl.DataSource = Getdl2();
    dl.DataBind();
}


it works well when I change my code,This is edited code:



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();

}

}



it works well when I change my code,This is edited code:

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>
                     name :
                </td>
                <td>
                     <asp:label id="pname" runat="server" text="<%# Eval("pname") %>" xmlns:asp="#unknown"></asp:label>
                </td>
                <td>
                     quantity:
                </td>
                <td>
                     <asp:label id="qty" runat="server" text="<%# Eval("qty") %>" xmlns:asp="#unknown"></asp:label>
                </td>
                <td>
                     unit_price:
                </td>
                <td>
                     <asp:label id="unit_price" runat="server" text="<%# Eval("p_price") %>" xmlns:asp="#unknown"></asp:label>
                </td>

            </tr>


        </table>
                   </itemtemplate> 


这篇关于如何在运行时将我的datalist的数据源设置为datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆