locked
How do I place a 'subform' in a web form? RRS feed

  • Question

  • User-1398449107 posted

    Hey forum,

    I have two related Sql Server Express Edition tables (below).  I'm trying to create a Web page that has a formview for table PlantName and a Gridview for PlantHeightChanges so that I don't have to re-enter the data in PlantName whenever I update the changes in PlantHeightChanges.  How do I get the Id value in the 'outer Web form' (bound to PlantName) to autopopulate in the id column of the 'subform' ( bound to PlantHeightChange)?  Is it possible?  Even a link to an article on the subject will be highly appreciated.  Thanx

     

    CREATE TABLE dbo.PlantName(

    Id int NOT NULL PRIMARY KEY,

    Name varchar(10) NOT NULL

    Type varchar(10) NOT NULL

    );

    CREATE TABLE dbo.PlantsHeightChanges (

    Id int NOT NULL

    CONSTRAINT fk_PlantHeight$Has$Plant

    FOREIGN KEY

    REFERENCES dbo.Plants (Id),

    Date datetime,

    Height decimal(12,6)

    CONSTRAINT pk_PlantsHeight

    PRIMARY KEY (Id, Date)

    );

     

     

     

    Thursday, September 27, 2007 12:44 PM

Answers

  • User-1310972242 posted

    I might have understood you incorrectly, but here's what I took from what you want.

     

    You have a form view that shows a plantID, plantName, and plantType.

    You have a gridview that shows the Height for plantID at specific Dates.

    Set their visibility = false until plantID has a value.  Then just use a textbox to pass the plant ID to both as a Select parameter

    Since FormView and GridView are both DataBound Controls that will share the commonality PlantID, why dont you just use the same datasource where your

     

    <asp:SqlDataSource runat='server' ID='SqlDataSource1' ConnectionString='<%YourConnectionString%>' />
     

    Codebehind:

     
    protected void Page_Load (object sender, EventArgs e)

    {

    SqlDataSource1.SelectCommand = "SELECT * FROM [PlantName], [PlantsHeightChanges] WHERE (Id = " + txtBoxID.Text + ")";

    if(txtBoxID.Text.Length > 0)

    {

    FormView1.Visible = true;

    GridView1.Visible = true;
    }
    }
     







     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 1:34 PM
  • User-1310972242 posted

    Doing an insert in gridview:::  USE THE DATAKEYNAMES attribute on your Gridview.  It'll make things nice and simple.

     

    First way to try is to simple replace your BOUNDFIELD column that is your ID in your gridview with something like this:

     

            <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="ID">
    <Columns>
    <asp:TemplateField>
    <InsertItemTemplate>
    <asp:TextBox runat="server" ID="txtInsertID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' ReadOnly="true" />
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label runat="server" ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" ID="txtEditID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' ReadOnly="true" />
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
      

     

    Realize that a gridview control is treated much like a DataTable in that we can manipulate it's cells from the code-behind.

    In C# it's much like:

     

     
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    //Check and see if we're inserting
    if(!GridView1.SelectedRow.FindControl("txtInsertID") == null)
    {
    TextBox myText = (TextBox)GridView1.SelectedRow.FindControl("txtInsertID");
    myText.Text = GridView1.SelectedDataKey.Value.ToString();
    }
    //Check and see if we're editing
    if (!GridView1.SelectedRow.FindControl("txtEditID") == null)
    {
    TextBox myEditText = (TextBox)GridView1.SelectedRow.FindControl("txtEditID");
    myEditText.Text = GridView1.SelectedDataKey.Value.ToString();
    }
    //The regular ItemTemplate should be using DataBinder.Eval(Container.DataItem, "ID")
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 30, 2007 12:33 PM

All replies

  • User1128022426 posted

    Maybe im missing somthing..But isnt the ID the same in both tables? sinces its a FK in PHC to a PK in PN?

    If thats the case, then bounding to ID should be straight fwd.

    Thanks.

    -Ash

     

    Thursday, September 27, 2007 1:31 PM
  • User-1310972242 posted

    I might have understood you incorrectly, but here's what I took from what you want.

     

    You have a form view that shows a plantID, plantName, and plantType.

    You have a gridview that shows the Height for plantID at specific Dates.

    Set their visibility = false until plantID has a value.  Then just use a textbox to pass the plant ID to both as a Select parameter

    Since FormView and GridView are both DataBound Controls that will share the commonality PlantID, why dont you just use the same datasource where your

     

    <asp:SqlDataSource runat='server' ID='SqlDataSource1' ConnectionString='<%YourConnectionString%>' />
     

    Codebehind:

     
    protected void Page_Load (object sender, EventArgs e)

    {

    SqlDataSource1.SelectCommand = "SELECT * FROM [PlantName], [PlantsHeightChanges] WHERE (Id = " + txtBoxID.Text + ")";

    if(txtBoxID.Text.Length > 0)

    {

    FormView1.Visible = true;

    GridView1.Visible = true;
    }
    }
     







     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 1:34 PM
  • User-1398449107 posted

    Thank you very much for the useful and informative response, JPuckett.  I JUST have one more question pertaining to the subject. 

    The relationship between the formview and gridview records is obviously a one (in formview) to many (in gridview) relationship, supposing that I often entered multiple rows of data at the same time in the gridview, how do I get the ID value in the formview to autopopulate in each new row of the gridview so that the user does not have to repeatedly enter it?

    Microsoft Access provides a wizard for adding a subform (with multiple gridview records) to an outer-form (with a single formview record) where the ID value found in the formview is automatically generated for each new row in the gridview.  However, I have not come across a way to simulate that property using vb.net.  Is it possible?

    Once again, thank you for the very informative last response.

    Sunday, September 30, 2007 11:08 AM
  • User-1398449107 posted

    Thank you Ash,

     Yes the ID value will be the same but the ID columns are in two different but related tables.  I'm sorry, I'm just transitioning to ASP.NET and am trying to gain as much stride as possible in as short a period of time as learning will allow.  Could you please clarify the type of binding you were referring to.

     Thanks.

    Sunday, September 30, 2007 11:16 AM
  • User-1310972242 posted

    Doing an insert in gridview:::  USE THE DATAKEYNAMES attribute on your Gridview.  It'll make things nice and simple.

     

    First way to try is to simple replace your BOUNDFIELD column that is your ID in your gridview with something like this:

     

            <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="ID">
    <Columns>
    <asp:TemplateField>
    <InsertItemTemplate>
    <asp:TextBox runat="server" ID="txtInsertID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' ReadOnly="true" />
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label runat="server" ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" ID="txtEditID" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' ReadOnly="true" />
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
      

     

    Realize that a gridview control is treated much like a DataTable in that we can manipulate it's cells from the code-behind.

    In C# it's much like:

     

     
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    //Check and see if we're inserting
    if(!GridView1.SelectedRow.FindControl("txtInsertID") == null)
    {
    TextBox myText = (TextBox)GridView1.SelectedRow.FindControl("txtInsertID");
    myText.Text = GridView1.SelectedDataKey.Value.ToString();
    }
    //Check and see if we're editing
    if (!GridView1.SelectedRow.FindControl("txtEditID") == null)
    {
    TextBox myEditText = (TextBox)GridView1.SelectedRow.FindControl("txtEditID");
    myEditText.Text = GridView1.SelectedDataKey.Value.ToString();
    }
    //The regular ItemTemplate should be using DataBinder.Eval(Container.DataItem, "ID")
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 30, 2007 12:33 PM