开发者

What control to use to display main & sub tables

ASP.NET 3.5 & SQL 2008 I have this main table & a subtable. For each set of calculation, 1 record is added to the main table & several records to the subtotal.

Most of user entered data goes into the subtable & some into the main table. Currently I have a web page开发者_开发技巧 set up with several textboxes to get the data from the user & the resulting calculations are displayed in a gridview.

The gridview shows all the records from the Main table. Each time the user selects a record from the gridview, I want to invoke another page where the user can change the data for the selected record.

It will be nice if I can display the 1 record from the main tablle & all the related records form the subtable.I don't want to use free floating textboxes.

What controls will be the easiest to use for such an application?


you can use grid view in two pages it self...

Here you have to use session variables to store the value of gridview rows in first page then retrieve the session value and display the row in gridview control in the second page.

If you use gridview control to display the data which get from database in the first page ,as far as I know you can also just pass the unique value of record(such as id) to an other page ,then use the unique value as a condition to search the record in database and display the record by using gridview control in the second page.

Sample:

1.Code in the first page(.aspx):

   <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
   </head>
   <body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" 
            onrowdatabound="GridView1_RowDataBound">
        <Columns>
        <asp:TemplateField>
        <HeaderTemplate>PassValue</HeaderTemplate>
        <ItemTemplate>
        <a href='OtherPage.aspx?id=<%# Container.DataItemIndex %>' target="_blank">Pass</a>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

2.Code in the first page(.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Bind();
    }
}
public void Bind()
{
    this.GridView1.DataSource = Get_source();
    this.GridView1.DataBind();
}
// You can get the datatbale form your own database.
// I get the datasource like this in order to run the sample convenient.
public DataTable Get_source()
{
    DataTable dt = new DataTable();
    DataRow dr;
    string XM = "true,false,false,true";
    string XMM = "1,2,3,4";
    string[] list1 = XM.Split(',');
    string[] list2 = XMM.Split(',');
    dt.Columns.Add(new DataColumn("ActiveStatus"));
    dt.Columns.Add(new DataColumn("number"));
    for (int i = 0; i < list1.Length; i++)
    {
        dr = dt.NewRow();
        dr["ActiveStatus"] = list1[i];
        dr["number"] = list2[i];
        dt.Rows.Add(dr);
    }
    return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    int i = e.Row.RowIndex;
    Session["row"+i] = e.Row;
}

3.Code in the second page(which named as OtherPage.aspx)

OtherPage.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

4.Code in the second page(.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["id"] != null)
    {
        string i = Request.QueryString["id"].ToString();
        if (Session["row"+i] != null)
        {
            DataTable dt = new DataTable();
            DataRow dr = dt.NewRow();
            GridViewRow row = (GridViewRow)Session["row" + i];
            for (int xm = 0; xm < row.Cells.Count; xm++)
            {
                dt.Columns.Add(new DataColumn());
                dr[xm] = row.Cells[xm].Text;
            }
            dt.Rows.Add(dr);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }
    }
}

if you want to modify the data in second page, you can modify ... I hope it will helps you...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜