[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

DataBinding Issue

I have a GridView and DetailView, so all what I am trying to do is to take a value from the DetailView  field and show it in the GridView.  Basically using either DataBinding or Eval but I am getting an error when I try to do this.  I know this is not correct but just to show you what I am trying to do here:
DetailView.Post_ID = GridView.Post_ID.  The field I am interested is the Post_ID field in both views.
Thanks
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:DetailsView ID="MyDetailsView" runat="server" AutoGenerateRows="False" 
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
        CellPadding="2" DataKeyNames="Post_ID" DataSourceID="DV_DS" ForeColor="Black" 
        GridLines="None" Height="50px" Width="792px">
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <Fields>
            <asp:BoundField DataField="Post_ID" HeaderText="RCA ID" ReadOnly="True" 
                SortExpression="Post_ID" />
            <asp:BoundField DataField="SharePointAuthor" HeaderText="RCA Author" 
                SortExpression="SharePointAuthor" />
            <asp:BoundField DataField="Post_Title" HeaderText="RCA Title" 
                SortExpression="Post_Title" />
            <asp:BoundField DataField="Post_Content" HeaderText="Problem Description" 
                SortExpression="Post_Content" />
            <asp:BoundField DataField="Date_Of_Issue" HeaderText="Incident Date" 
                SortExpression="Date_Of_Issue" />
            <asp:BoundField DataField="StartTime" HeaderText="Start Time" 
                SortExpression="StartTime" />
            <asp:BoundField DataField="EndTime" HeaderText="End Time" 
                SortExpression="EndTime" />
            <asp:BoundField DataField="Status" HeaderText="Status" 
                SortExpression="Status" />
        </Fields>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
            HorizontalAlign="Center" />
    </asp:DetailsView>


GridView:
<div>
      <asp:gridview ID="Gridview1" runat="server" ShowFooter="True" 
            AutoGenerateColumns="False" onrowcreated="Gridview1_RowCreated" 
            BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
            CellPadding="4" GridLines="Horizontal" Width="644px">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />

            <asp:TemplateField HeaderText="Post ID">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox0" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="Date">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" CssClass="datepicker"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
           
            <asp:TemplateField HeaderText="Time" Visible="False">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server" Height="103px" TextMode="MultiLine" 
                         Width="375px"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" 
                        onclick="ButtonAdd_Click" />
                </FooterTemplate>
            </asp:TemplateField>
                 <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Remove</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
        </asp:gridview>

Open in new window

0
karinos57
Asked:
karinos57
  • 7
  • 6
1 Solution
 
karinos57Author Commented:
i forgot to mention, i have a code behind for this:
 public void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

Open in new window

0
 
Jared_SCommented:
Does the details view refresh when a new row is added or selected from the grid view?

You can pass the Post ID on each event to the data source for the details view, and then rebind the details view.
0
 
karinos57Author Commented:
no, the detail view is the same and it is going to have the same Post_ID.   As matter of fact i would like to copy or insert the same Post ID when row is added.  Because the detail stays the same.  Well i am trying to figure out but no success so far.. i would appreciate if u can help.  thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
karinos57Author Commented:
Also when the page loads the Deail view loads and i am using this code for the page load
 public void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

Open in new window

0
 
Jared_SCommented:
I understand now - sorry for the confusion.

I think moving your gridview binding into the PreRenderComplete event will put you on the right track.

If your details view is loading in the page load event, then those values should be available at the time of the PreRenderComplete event.

You should be able to get the post ID with something like MyDetailsView.DataItem("Post_ID").

Here is a good reference for the page's life cycle if you're interested:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
0
 
karinos57Author Commented:
thanks, i tried this method but it is not working.. :
first: in the ASPX code, i created a databound field  
<asp:BoundField DataField="Post_ID" HeaderText="Post ID" />
here is what i did when the page loads
 public void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Post_ID", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Post_ID"] = MyDetailsView.DataItem("Post_ID");
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;;
        dr["Column4"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
0
 
Jared_SCommented:
Can you debug and see if MyDetailsView.DataItem("Post_ID") is available when it's being bound?

Here is some psuedo-code matching a working project. It's a little different than what's been discussed, and definitely not the only way to do it.

 PreRenderComplete event binding
    MyDetailsView.DataBind()
    Gridview1.DataBind()

In the detailsview_databound event I set a session variable = target value.

Session("PostID") = MyDetailsView.DataItem("Post_ID")

and then on then on .aspx
 <asp:TextBox ID="TextBox0" runat="server" Text='<%# Eval("PostID") %>'</asp:TextBox>
0
 
karinos57Author Commented:
i am not sure this line of code:
In the detailsview_databound event I set a session variable = target value.

where should i put it? in the ASPX?  can you explain little bit more as i am newbie to asp? thanks
0
 
Jared_SCommented:
That's my fault for not being more clear.

The data bound event is just an event that fires after your detail view is bound to its data source. It goes in your code-behind.

It would look something like this:

protected void MyDetailsView_DataBound(object sender, System.EventArgs e)
{
	try {
		Session["PostID"] = MyDetailsView.DataItem("Post_ID") ;
	} catch {
	}
}

Open in new window

0
 
karinos57Author Commented:
under the DataItem is showing error line wich reads like "Non Invocable member system.web...
0
 
Jared_SCommented:
I think I mixed in vb syntax.

See if replacing MyDetailsView.DataItem("Post_ID")
with MyDetailsView.DataItem["Post_ID"] makes a difference for you.
0
 
Jared_SCommented:
Also, if you know how to use the debugger in Visual Studio, it's really helpful here.

You can add a breakpoint in your code behind in your details view data bound event, and then click 'debug'. When the code executes up to your breakpoint, add a watch window.

From the watch window you can check the values of your variables, and check syntax for the details view reference. The best part is you don't have to rerun your code,  you can just type it and see the results instantly.

It will save you LOADS of time.
0
 
karinos57Author Commented:
thnx
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now