Hashtable binding with detailsview.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vineetbindal
    New Member
    • Jan 2009
    • 57

    Hashtable binding with detailsview.

    Hi all,

    i have a hashtable which i have bounded to detailsview.. it is fine.

    but i donno what code should i use now to display the values of that hashlist on labels in my details view.

    Code:
    hshTable .Add("UserLevel",  UserLevel);
    hshTable .Add("TypeLevel",  TypeLevel);
    hshTable .Add("ActionLevel",  ActionLevel);
    hshTable.Add("StatusLevel", StatusLevel);
    
    DetailsView1.DataSource = ashTable;
    DetailsView1.DataBind();

    now how can i display these values on labels in my template column in my detailsview??

    please help

    thanks,
    vineet
    Last edited by Frinavale; Jan 30 '09, 04:32 PM. Reason: added [code] tags
  • sangam56
    New Member
    • Nov 2007
    • 68

    #2
    Hi bineetbindal,
    I tried myself to bind a detailsview to a hashtable. But this did not work. It threw the following error:

    DetailsView with id 'dvwTest' did not have any properties or attributes from which to generate fields. Ensure that your data source has content.

    This means hashtable is non-bindable datasource. I guess one work around would be to create a datatable with hashtable-keys as columns and hashtable-values as column-values. And then bind the gridview or detailsview to this datatable.

    But I am keen to know if you have succeeded to bind the detailsview to the hashtable. Waiting your response, thanks.

    Comment

    • vineetbindal
      New Member
      • Jan 2009
      • 57

      #3
      HI,

      I tried a lot but i was not able to do it, actually i am working on asp.net after few years, can you give me some code about how can i do it with datatable?

      actually the difficulty i am facing is --- i have a gridview. when a person clicks on the gridview. i have to display all the values of that row on the lables in details view(those labels are in tempalte column in detailsview). i am able to do that with the code below. till the time detailsview is in insertmode.


      Code:
      //get the vales from the grid's selected row
       Label User = (Label)GridView1.SelectedRow.Cells[6].FindControl("lblUserLevel");
         Label Action = (Label)GridView1.SelectedRow.Cells[7].FindControl("lblActionLevel");
         Label Type = (Label)GridView1.SelectedRow.Cells[8].FindControl("lblTypeLevel");
         Label Status = (Label)GridView1.SelectedRow.Cells[9].FindControl("lblStatusLevel");
      //convert those grid values to string
         string UserLevel = User.Text;
         string ActionLevel = Action.Text;
         string TypeLevel = Type.Text;
         string StatusLevel = Status.Text;
      //find label in detailsview and assign them values
      ((Label)DetailsView1.FindControl("lblUser")).Text=UserLevel;//here it throws error if i chnage the defoultmode ffrom insert to anything else.
             ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
             ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
             ((Label)DetailsView1.FindControl("lblStatus")).Text = StatusLevel;
      but as soon as change the defoultmode of detailsview from insert to anything else, it stops working. it also stops working even if i try to put a <EditItemTempla te></EditItemTemplat e> in my detailsview.


      i am not able to understand why it behaves so dramatically, i have been searching on the behavious of detailsview but did not get anything.

      thats why i tried to bind it by putting vallues in hashtable but unsuccessfull.

      can you give me an example how can i do it with datatable(some code will be really appreciatable.)

      regards,
      Vineet
      Last edited by Frinavale; Feb 2 '09, 04:21 PM. Reason: changed quote tags into [code] tags

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Have you checked out the DetailsView class on MSDN?
        There are some examples there on how to use the DetailsView with a SqlDataSource as it's source (which connects to a database).

        Comment

        • vineetbindal
          New Member
          • Jan 2009
          • 57

          #5
          yes i did,

          here i am not using any SqlDataSource as its source.

          my source is a row in my gridview.

          thats what the problem is

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Could you please post the ASP code that you're using so I can see what your doing?

            Comment

            • vineetbindal
              New Member
              • Jan 2009
              • 57

              #7
              below is my aspx file.

              Code:
              <%@ Page Title="" Language="C#" MasterPageFile="~/bpMasterPage.master" AutoEventWireup="true" EnableEventValidation="False" CodeFile="testGerard.aspx.cs" Inherits="testGerard" %>
              
              <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
              </asp:Content>
              <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
              <table>
              
              <script language="javascript" type="text/javascript">
              var oldgridSelectedColor;
              
              function setMouseOverColor(element)
              {
                  oldgridSelectedColor = element.style.backgroundColor;
                  element.style.backgroundColor='skyblue';
                  element.style.cursor='hand';
                  element.style.textDecoration='underline';
              }
              
              function setMouseOutColor(element)
              {
                  element.style.backgroundColor=oldgridSelectedColor;
                  element.style.textDecoration='none';
              }
              </script>
              
              <tr>
              <td>
                  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="DocActionDataSource" DataKeyNames="UserLevelID" 
              	 EnableViewState="true"	OnRowCreated="GridView1_RowCreated"  onselectedindexchanged="GridView1_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" 
              		GridLines="None"  >
              		<RowStyle BackColor="#EFF3FB" />
              		<Columns >
              			<asp:CommandField ShowSelectButton="True" visible="false"/>
              			<asp:BoundField DataField="DocTypeID" HeaderText="DocTypeID" SortExpression="DocTypeID" />
              			<asp:BoundField DataField="StatusID" HeaderText="StatusID" SortExpression="StatusID" />
              			<asp:BoundField DataField="UserLevelID" HeaderText="UserLevelID" SortExpression="UserLevelID" />
              			<asp:BoundField DataField="ActionID" HeaderText="ActionID" SortExpression="ActionID" />
              			<asp:CheckBoxField DataField="Deny" HeaderText="Deny" SortExpression="Deny" />
              			<asp:TemplateField HeaderText="User Level" >
              				<ItemTemplate>
              					<asp:Label ID="lblUserLevel" runat="server" EnableViewState="true"></asp:Label>
              				</ItemTemplate>
              				<EditItemTemplate>
              				<asp:DropDownList ID="DropDownList1" runat="server"  ></asp:DropDownList>
              				</EditItemTemplate>
              			</asp:TemplateField>
              			<asp:TemplateField HeaderText="Action Level">
              				<ItemTemplate>
              					<asp:Label ID="lblActionLevel" runat="server" EnableViewState="true"></asp:Label>
              				</ItemTemplate>
              			</asp:TemplateField>
              			<asp:TemplateField HeaderText="Type Level">
              				<ItemTemplate>
              					<asp:Label ID="lblTypeLevel" runat="server" EnableViewState="true"></asp:Label>
              				</ItemTemplate>
              			</asp:TemplateField>
              		</Columns>
              		<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
              		<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
              		<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
              		<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
              		<EditRowStyle BackColor="#2461BF" />
              		<AlternatingRowStyle BackColor="White" />
              	</asp:GridView>
              	<asp:ObjectDataSource ID="DocActionDataSource" runat="server" SelectMethod="AdminDocActionList"
              		TypeName="bpDevDataClassesDataContext" OnObjectDisposing="CancelDisposing"></asp:ObjectDataSource>
              </td>
              <td style="vertical-align: top">
              
              <asp:DetailsView  Runat="Server" ID="DetailsView1" FieldHeaderStyle-Font-Bold="true"
                      FieldHeaderStyle-HorizontalAlign="Right" 
                                      AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
                                      AutoGenerateInsertButton="True" AutoGenerateRows="False" BorderStyle="None" 
                                      BorderWidth="1px" CaptionAlign="Left" CellPadding="3" CssClass="Normal" DefaultMode="Insert"
                                       EmptyDataText="There are no records." Visible="False"> 
                    
              <FieldHeaderStyle HorizontalAlign="Right" Font-Bold="True" ></FieldHeaderStyle>
                      <Fields>
                      
                     
                  <asp:TemplateField HeaderText="UserLevel">
                 
                                      <ItemTemplate>
                                      <asp:Label ID="lblUser" runat="server" Text='<%# Bind("UserLevel") %>'></asp:Label>
                                           </ItemTemplate>
                                          
                                               </asp:TemplateField> 
                  
                  <asp:TemplateField HeaderText="Status">
                  <ItemTemplate>
                  <asp:Label ID="lblStatus" runat="server"></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField> 
                  
                  <asp:TemplateField HeaderText="ActionLevel">
                  <ItemTemplate>
                  <asp:Label ID="lblAction" runat="server" Text='<%# Bind("ActionLevel") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField> 
                  
                  <asp:TemplateField HeaderText="TypeLevel">
                  <ItemTemplate>
                  <asp:Label ID="lblType" runat="server" Text='<%# Bind("TypeLevel") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>    
                      
                      </Fields>
                  </asp:DetailsView>
              
                  <asp:ObjectDataSource ID="ObjectDataSourcedt1" runat="server" 
                      SelectMethod="AdminUserLevelList" TypeName="bpDevDataClassesDataContext">
                  </asp:ObjectDataSource>
              
              </td>
              </tr>
                   </table>
              </asp:Content>


              below is my code behind or cs file..
              Code:
              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.Data;
              using System.Web.UI;
              using System.Web.UI.WebControls;
              
              public partial class testGerard : System.Web.UI.Page
               
              {
                 
                  // Cache userlevel list
                  private List<AdminUserLevelListResult> UserList;
                  private List<AdminDocActionListResult> AdminDocActionList;
                  private List<DocActionListResult> ActionList;
                  private List<DocTypeListResult> TypeList;
              
              
                  //---- Page load
                  protected void Page_Load(object sender, EventArgs e)
                  {
              
                      bpDevDataClassesDataContext dc = new bpDevDataClassesDataContext();
                      var user = from u in dc.AdminUserLevelList() select u;
                      var adminAction = from a in dc.AdminDocActionList() select a;
                      var actionList = from a in dc.DocActionList() select a;
                      var typeList = from a in dc.DocTypeList() select a;
                      UserList = user.ToList();
                      AdminDocActionList = adminAction.ToList();
                      ActionList = actionList.ToList();
                      TypeList = typeList.ToList();
                      //   Initialize cached list
                      if (Page.IsPostBack)
                          GridView1.DataBind();
              
                  }
              
              
                  //---- This is necessary
                  protected void CancelDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
                  {
                      e.Cancel = true;
                  }
              
              
                  //---- Row created
                  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
                  {
                      // Show current UserLevel
                      Label lblUserLevel = (Label)e.Row.FindControl("lblUserLevel");
                      if (lblUserLevel != null && e.Row.DataItem != null)
                      {
                          // e.Row.DataItem is null when line is selected, please investigate
                          int UserLevelID = (int)DataBinder.Eval(e.Row.DataItem, "UserLevelID");
                          var user = from u in UserList where u.UserLevelID == UserLevelID select u;
                          AdminUserLevelListResult[] userArray = user.ToArray();
                          if (userArray.Length > 0)
                              lblUserLevel.Text = userArray[0].UserLevelDesc;
                      }
              
                      //  ... Do same for other lookup columns
              
                      Label lblActionLevel = (Label)e.Row.FindControl("lblActionLevel");
                      if (lblActionLevel != null && e.Row.DataItem != null)
                      {
                          int ActionLevelID = (int)DataBinder.Eval(e.Row.DataItem, "ActionID");
                          var action = from a in ActionList where a.ActionID == ActionLevelID select a;
                          DocActionListResult[] actionArray = action.ToArray();
                          if (actionArray.Length > 0)
                              lblActionLevel.Text = actionArray[0].ActionDesc;
                      }
                      Label lblTypeLevel = (Label)e.Row.FindControl("lblTypeLevel");
                      if (lblTypeLevel != null && e.Row.DataItem != null)
                      {
                          string TypeID = (string)DataBinder.Eval(e.Row.DataItem, "DocTypeID");
                          var type = from t in TypeList where t.DocTypeID.Equals(TypeID) select t;
                          DocTypeListResult[] typeArray = type.ToArray();
                          if (typeArray.Length > 0)
                              lblTypeLevel.Text = typeArray[0].DocTypeDesc;
                      }
              
                      if (e.Row.RowType == DataControlRowType.DataRow)
                      {
                          e.Row.Attributes["onmouseover"] =
                              "javascript:setMouseOverColor(this);";
                          e.Row.Attributes["onmouseout"] =
                              "javascript:setMouseOutColor(this);";
                          e.Row.Attributes["onclick"] =
                          ClientScript.GetPostBackClientHyperlink
                              (this.GridView1, "Select$" + e.Row.RowIndex);
              
                      }
              
                  }
              
                  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
                  {
              
                      //these vales have to be displayed in the deyailsview labels
              
                      Label User = (Label)GridView1.SelectedRow.Cells[6].FindControl("lblUserLevel");
                      Label Action = (Label)GridView1.SelectedRow.Cells[7].FindControl("lblActionLevel");
                      Label Type = (Label)GridView1.SelectedRow.Cells[8].FindControl("lblTypeLevel");
                      string UserLevel = User.Text;
                      string ActionLevel = Action.Text;
                      string TypeLevel = Type.Text;
                     
              
                      System.Diagnostics.Debug.WriteLine("its the line" + UserLevel);
                      ((Label)DetailsView1.FindControl("lblUser")).Text = UserLevel;
                      ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
                      ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
              
                      DetailsView1.Visible = true;
                      DetailsView1.ChangeMode(DetailsViewMode.Edit);
                  }
              
              }

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Ok, quickly looking at this, I see one thing that can be causing a problem with your editing.

                In your PageLoad event you have:
                Code:
                   if (Page.IsPostBack)
                             GridView1.DataBind();
                This could be causing problems.
                Move this to the PreRender event instead and see if that makes a difference.

                Comment

                • vineetbindal
                  New Member
                  • Jan 2009
                  • 57

                  #9
                  No it still does not make any differance.

                  it is still the same.

                  it works without <EditItemTempla te></EditItemTemplat e> but as soon as i add one in my detailsview or change the defoultmode from insert to anything else it throws me an error in one of the lines
                  ((Label)Details View1.FindContr ol("lblUser")). Text = UserLevel;
                  ((Label)Details View1.FindContr ol("lblAction") ).Text = ActionLevel;
                  ((Label)Details View1.FindContr ol("lblType")). Text = TypeLevel;
                  ((Label)Details View1.FindContr ol("lblStatus") ).Text = StatusLevel;
                  depending on in which column i put the <EditItemTempla te></EditItemTemplat e>

                  or if i am changing the mode then it gives me an error in the first line.

                  Comment

                  • vineetbindal
                    New Member
                    • Jan 2009
                    • 57

                    #10
                    hi i tried doing it as suggested by Sangam, through datatable and that thing works fine.... thanks a lot for that. but i am still confused why it did not work the way i was oing it, wht it was working fine with insert mode without edittemplatecol umn and why not viceversa.

                    Comment

                    • sangam56
                      New Member
                      • Nov 2007
                      • 68

                      #11
                      Hi Vineetbandal,
                      I suspect null reference exception is being thrown while finding the labels in edit mode.I suggest to put the edit mode specific code in the particular section in the databound event hander of the detailsview, like this:

                      Code:
                       
                       
                      //Databound event handler for the detailsview
                      protected void DetailsView1_DataBound(object sender, EventArgs e)
                      {
                      if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                      {
                      //tasks related to insert mode only
                      }
                      else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
                      {
                      //tasks related to edit mode only
                      //*** Here I have inserted your code
                      ((Label)DetailsView1.FindControl("lblUser")).Text = UserLevel;
                      ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
                      ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
                      ((Label)DetailsView1.FindControl("lblStatus")).Text = StatusLevel;
                      }
                      }
                      Hope this helps.

                      Comment

                      • sangam56
                        New Member
                        • Nov 2007
                        • 68

                        #12
                        Please follow up if no progress. Thanks.

                        Comment

                        • vineetbindal
                          New Member
                          • Jan 2009
                          • 57

                          #13
                          Hi thanks a lot for the effort.. but i tried the solution u gave me before saving the values in a datatable and binding it to the detailsview and works absolutely fine//... but i have another problem now... when i clikc on edit button in my detailsview .....it just says... EmptyDataText=" There are no records." and it is supposed to show the dropdowns in the detailsview.

                          Comment

                          Working...