473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. hshTable .Add("UserLevel",  UserLevel);
  2. hshTable .Add("TypeLevel",  TypeLevel);
  3. hshTable .Add("ActionLevel",  ActionLevel);
  4. hshTable.Add("StatusLevel", StatusLevel);
  5.  
  6. DetailsView1.DataSource = ashTable;
  7. DetailsView1.DataBind();

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

please help

thanks,
vineet
Jan 28 '09 #1
12 5322
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.
Feb 2 '09 #2
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.


Expand|Select|Wrap|Line Numbers
  1. //get the vales from the grid's selected row
  2.  Label User = (Label)GridView1.SelectedRow.Cells[6].FindControl("lblUserLevel");
  3.    Label Action = (Label)GridView1.SelectedRow.Cells[7].FindControl("lblActionLevel");
  4.    Label Type = (Label)GridView1.SelectedRow.Cells[8].FindControl("lblTypeLevel");
  5.    Label Status = (Label)GridView1.SelectedRow.Cells[9].FindControl("lblStatusLevel");
  6. //convert those grid values to string
  7.    string UserLevel = User.Text;
  8.    string ActionLevel = Action.Text;
  9.    string TypeLevel = Type.Text;
  10.    string StatusLevel = Status.Text;
  11. //find label in detailsview and assign them values
  12. ((Label)DetailsView1.FindControl("lblUser")).Text=UserLevel;//here it throws error if i chnage the defoultmode ffrom insert to anything else.
  13.        ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
  14.        ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
  15.        ((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 <EditItemTemplate></EditItemTemplate> 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
Feb 2 '09 #3
Frinavale
9,735 Expert Mod 8TB
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).
Feb 2 '09 #4
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
Feb 2 '09 #5
Frinavale
9,735 Expert Mod 8TB
Could you please post the ASP code that you're using so I can see what your doing?
Feb 2 '09 #6
below is my aspx file.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/bpMasterPage.master" AutoEventWireup="true" EnableEventValidation="False" CodeFile="testGerard.aspx.cs" Inherits="testGerard" %>
  2.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  6. <table>
  7.  
  8. <script language="javascript" type="text/javascript">
  9. var oldgridSelectedColor;
  10.  
  11. function setMouseOverColor(element)
  12. {
  13.     oldgridSelectedColor = element.style.backgroundColor;
  14.     element.style.backgroundColor='skyblue';
  15.     element.style.cursor='hand';
  16.     element.style.textDecoration='underline';
  17. }
  18.  
  19. function setMouseOutColor(element)
  20. {
  21.     element.style.backgroundColor=oldgridSelectedColor;
  22.     element.style.textDecoration='none';
  23. }
  24. </script>
  25.  
  26. <tr>
  27. <td>
  28.     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="DocActionDataSource" DataKeyNames="UserLevelID" 
  29.      EnableViewState="true"    OnRowCreated="GridView1_RowCreated"  onselectedindexchanged="GridView1_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" 
  30.         GridLines="None"  >
  31.         <RowStyle BackColor="#EFF3FB" />
  32.         <Columns >
  33.             <asp:CommandField ShowSelectButton="True" visible="false"/>
  34.             <asp:BoundField DataField="DocTypeID" HeaderText="DocTypeID" SortExpression="DocTypeID" />
  35.             <asp:BoundField DataField="StatusID" HeaderText="StatusID" SortExpression="StatusID" />
  36.             <asp:BoundField DataField="UserLevelID" HeaderText="UserLevelID" SortExpression="UserLevelID" />
  37.             <asp:BoundField DataField="ActionID" HeaderText="ActionID" SortExpression="ActionID" />
  38.             <asp:CheckBoxField DataField="Deny" HeaderText="Deny" SortExpression="Deny" />
  39.             <asp:TemplateField HeaderText="User Level" >
  40.                 <ItemTemplate>
  41.                     <asp:Label ID="lblUserLevel" runat="server" EnableViewState="true"></asp:Label>
  42.                 </ItemTemplate>
  43.                 <EditItemTemplate>
  44.                 <asp:DropDownList ID="DropDownList1" runat="server"  ></asp:DropDownList>
  45.                 </EditItemTemplate>
  46.             </asp:TemplateField>
  47.             <asp:TemplateField HeaderText="Action Level">
  48.                 <ItemTemplate>
  49.                     <asp:Label ID="lblActionLevel" runat="server" EnableViewState="true"></asp:Label>
  50.                 </ItemTemplate>
  51.             </asp:TemplateField>
  52.             <asp:TemplateField HeaderText="Type Level">
  53.                 <ItemTemplate>
  54.                     <asp:Label ID="lblTypeLevel" runat="server" EnableViewState="true"></asp:Label>
  55.                 </ItemTemplate>
  56.             </asp:TemplateField>
  57.         </Columns>
  58.         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  59.         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  60.         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  61.         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  62.         <EditRowStyle BackColor="#2461BF" />
  63.         <AlternatingRowStyle BackColor="White" />
  64.     </asp:GridView>
  65.     <asp:ObjectDataSource ID="DocActionDataSource" runat="server" SelectMethod="AdminDocActionList"
  66.         TypeName="bpDevDataClassesDataContext" OnObjectDisposing="CancelDisposing"></asp:ObjectDataSource>
  67. </td>
  68. <td style="vertical-align: top">
  69.  
  70. <asp:DetailsView  Runat="Server" ID="DetailsView1" FieldHeaderStyle-Font-Bold="true"
  71.         FieldHeaderStyle-HorizontalAlign="Right" 
  72.                         AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
  73.                         AutoGenerateInsertButton="True" AutoGenerateRows="False" BorderStyle="None" 
  74.                         BorderWidth="1px" CaptionAlign="Left" CellPadding="3" CssClass="Normal" DefaultMode="Insert"
  75.                          EmptyDataText="There are no records." Visible="False"> 
  76.  
  77. <FieldHeaderStyle HorizontalAlign="Right" Font-Bold="True" ></FieldHeaderStyle>
  78.         <Fields>
  79.  
  80.  
  81.     <asp:TemplateField HeaderText="UserLevel">
  82.  
  83.                         <ItemTemplate>
  84.                         <asp:Label ID="lblUser" runat="server" Text='<%# Bind("UserLevel") %>'></asp:Label>
  85.                              </ItemTemplate>
  86.  
  87.                                  </asp:TemplateField> 
  88.  
  89.     <asp:TemplateField HeaderText="Status">
  90.     <ItemTemplate>
  91.     <asp:Label ID="lblStatus" runat="server"></asp:Label>
  92.     </ItemTemplate>
  93.     </asp:TemplateField> 
  94.  
  95.     <asp:TemplateField HeaderText="ActionLevel">
  96.     <ItemTemplate>
  97.     <asp:Label ID="lblAction" runat="server" Text='<%# Bind("ActionLevel") %>'></asp:Label>
  98.     </ItemTemplate>
  99.     </asp:TemplateField> 
  100.  
  101.     <asp:TemplateField HeaderText="TypeLevel">
  102.     <ItemTemplate>
  103.     <asp:Label ID="lblType" runat="server" Text='<%# Bind("TypeLevel") %>'></asp:Label>
  104.     </ItemTemplate>
  105.     </asp:TemplateField>    
  106.  
  107.         </Fields>
  108.     </asp:DetailsView>
  109.  
  110.     <asp:ObjectDataSource ID="ObjectDataSourcedt1" runat="server" 
  111.         SelectMethod="AdminUserLevelList" TypeName="bpDevDataClassesDataContext">
  112.     </asp:ObjectDataSource>
  113.  
  114. </td>
  115. </tr>
  116.      </table>
  117. </asp:Content>
  118.  
  119.  


below is my code behind or cs file..
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8.  
  9. public partial class testGerard : System.Web.UI.Page
  10.  
  11. {
  12.  
  13.     // Cache userlevel list
  14.     private List<AdminUserLevelListResult> UserList;
  15.     private List<AdminDocActionListResult> AdminDocActionList;
  16.     private List<DocActionListResult> ActionList;
  17.     private List<DocTypeListResult> TypeList;
  18.  
  19.  
  20.     //---- Page load
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.  
  24.         bpDevDataClassesDataContext dc = new bpDevDataClassesDataContext();
  25.         var user = from u in dc.AdminUserLevelList() select u;
  26.         var adminAction = from a in dc.AdminDocActionList() select a;
  27.         var actionList = from a in dc.DocActionList() select a;
  28.         var typeList = from a in dc.DocTypeList() select a;
  29.         UserList = user.ToList();
  30.         AdminDocActionList = adminAction.ToList();
  31.         ActionList = actionList.ToList();
  32.         TypeList = typeList.ToList();
  33.         //   Initialize cached list
  34.         if (Page.IsPostBack)
  35.             GridView1.DataBind();
  36.  
  37.     }
  38.  
  39.  
  40.     //---- This is necessary
  41.     protected void CancelDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
  42.     {
  43.         e.Cancel = true;
  44.     }
  45.  
  46.  
  47.     //---- Row created
  48.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  49.     {
  50.         // Show current UserLevel
  51.         Label lblUserLevel = (Label)e.Row.FindControl("lblUserLevel");
  52.         if (lblUserLevel != null && e.Row.DataItem != null)
  53.         {
  54.             // e.Row.DataItem is null when line is selected, please investigate
  55.             int UserLevelID = (int)DataBinder.Eval(e.Row.DataItem, "UserLevelID");
  56.             var user = from u in UserList where u.UserLevelID == UserLevelID select u;
  57.             AdminUserLevelListResult[] userArray = user.ToArray();
  58.             if (userArray.Length > 0)
  59.                 lblUserLevel.Text = userArray[0].UserLevelDesc;
  60.         }
  61.  
  62.         //  ... Do same for other lookup columns
  63.  
  64.         Label lblActionLevel = (Label)e.Row.FindControl("lblActionLevel");
  65.         if (lblActionLevel != null && e.Row.DataItem != null)
  66.         {
  67.             int ActionLevelID = (int)DataBinder.Eval(e.Row.DataItem, "ActionID");
  68.             var action = from a in ActionList where a.ActionID == ActionLevelID select a;
  69.             DocActionListResult[] actionArray = action.ToArray();
  70.             if (actionArray.Length > 0)
  71.                 lblActionLevel.Text = actionArray[0].ActionDesc;
  72.         }
  73.         Label lblTypeLevel = (Label)e.Row.FindControl("lblTypeLevel");
  74.         if (lblTypeLevel != null && e.Row.DataItem != null)
  75.         {
  76.             string TypeID = (string)DataBinder.Eval(e.Row.DataItem, "DocTypeID");
  77.             var type = from t in TypeList where t.DocTypeID.Equals(TypeID) select t;
  78.             DocTypeListResult[] typeArray = type.ToArray();
  79.             if (typeArray.Length > 0)
  80.                 lblTypeLevel.Text = typeArray[0].DocTypeDesc;
  81.         }
  82.  
  83.         if (e.Row.RowType == DataControlRowType.DataRow)
  84.         {
  85.             e.Row.Attributes["onmouseover"] =
  86.                 "javascript:setMouseOverColor(this);";
  87.             e.Row.Attributes["onmouseout"] =
  88.                 "javascript:setMouseOutColor(this);";
  89.             e.Row.Attributes["onclick"] =
  90.             ClientScript.GetPostBackClientHyperlink
  91.                 (this.GridView1, "Select$" + e.Row.RowIndex);
  92.  
  93.         }
  94.  
  95.     }
  96.  
  97.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  98.     {
  99.  
  100.         //these vales have to be displayed in the deyailsview labels
  101.  
  102.         Label User = (Label)GridView1.SelectedRow.Cells[6].FindControl("lblUserLevel");
  103.         Label Action = (Label)GridView1.SelectedRow.Cells[7].FindControl("lblActionLevel");
  104.         Label Type = (Label)GridView1.SelectedRow.Cells[8].FindControl("lblTypeLevel");
  105.         string UserLevel = User.Text;
  106.         string ActionLevel = Action.Text;
  107.         string TypeLevel = Type.Text;
  108.  
  109.  
  110.         System.Diagnostics.Debug.WriteLine("its the line" + UserLevel);
  111.         ((Label)DetailsView1.FindControl("lblUser")).Text = UserLevel;
  112.         ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
  113.         ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
  114.  
  115.         DetailsView1.Visible = true;
  116.         DetailsView1.ChangeMode(DetailsViewMode.Edit);
  117.     }
  118.  
  119. }
  120.  
Feb 2 '09 #7
Frinavale
9,735 Expert Mod 8TB
Ok, quickly looking at this, I see one thing that can be causing a problem with your editing.

In your PageLoad event you have:
Expand|Select|Wrap|Line Numbers
  1.    if (Page.IsPostBack)
  2.              GridView1.DataBind();
This could be causing problems.
Move this to the PreRender event instead and see if that makes a difference.
Feb 2 '09 #8
No it still does not make any differance.

it is still the same.

it works without <EditItemTemplate></EditItemTemplate> 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)DetailsView1.FindControl("lblUser")).Text = UserLevel;
((Label)DetailsView1.FindControl("lblAction")).Tex t = ActionLevel;
((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
((Label)DetailsView1.FindControl("lblStatus")).Tex t = StatusLevel;
depending on in which column i put the <EditItemTemplate></EditItemTemplate>

or if i am changing the mode then it gives me an error in the first line.
Feb 2 '09 #9
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 edittemplatecolumn and why not viceversa.
Feb 3 '09 #10
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. //Databound event handler for the detailsview
  4. protected void DetailsView1_DataBound(object sender, EventArgs e)
  5. {
  6. if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
  7. {
  8. //tasks related to insert mode only
  9. }
  10. else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
  11. {
  12. //tasks related to edit mode only
  13. //*** Here I have inserted your code
  14. ((Label)DetailsView1.FindControl("lblUser")).Text = UserLevel;
  15. ((Label)DetailsView1.FindControl("lblAction")).Text = ActionLevel;
  16. ((Label)DetailsView1.FindControl("lblType")).Text = TypeLevel;
  17. ((Label)DetailsView1.FindControl("lblStatus")).Text = StatusLevel;
  18. }
  19. }
  20.  
Hope this helps.
Feb 4 '09 #11
Please follow up if no progress. Thanks.
Feb 4 '09 #12
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.
Feb 4 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ceri_Davies | last post by:
Hi, Is it possible to bind a text box to the results of a detailsview record. This is for a university project using visual web developer 2005 express beta. Unfortunatly this is a new eniroment to...
2
by: Andrew Robinson | last post by:
Is there any way to accomplish two way data binding in a Details View with a DataSet or DataTable as the DataSource. All I want is to get an updated DataSet or DataTable back from the...
9
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be...
5
by: Dave | last post by:
Hi all, Is it possible to populate a listbox from a hashtable (complexbind) where the ValueMember of the listbox maps to the "key" member of the hashtable AND the DisplayMember of the listbox maps...
0
by: aarnan | last post by:
I have two SqlDataSources that use two different stored procedures that take parameters to populate two controls, one dependant on an element of the other. The first data source,...
0
by: 47computers | last post by:
Hello. I'm having a bit of difficulty with some DropDownList controls within templates in a DetailsView on my web form. Basically, I have a form where a user is building a "location profile" with...
1
by: JJ | last post by:
Hi. I am having trouble getting a dropdownlist to work properly in a detailsview: The code is something like: <asp:DetailsView ID="dvwSubscriber" runat="server" AutoGenerateRows="False" ...
2
by: makennedy | last post by:
Hi Experts, Please help, I am a newbie to ASP.NET 2.0 may be I am doing something wrong or there may be a bug somewhere. Basically I have a TreeView Control which I have created...
1
by: vineetbindal | last post by:
Hi all, I have a datagrid and detailsview. when a user selects a row in datagrid onSelectedIndexchanged is fired and Detailsview comes into picture. I want to display the data of the selected row...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.