Results 1 to 4 of 4

Thread: Alternative to DataList to show aingle records

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Alternative to DataList to show aingle records

    I'm using a datalist that uses an itemtemplate to display results from an objectdatasource. The thing is, I'm only fetching a single record so a datalist doesn't seem like the best option. Is there a more suitable control I can use?

    Also, how do I access the data on an objectdatasource that has been created in design view from code-behind. I've tried google but with no success.

    Cheers

  2. #2
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Alternative to DataList to show aingle records

    How about a GridView? It's essentially a table.

    All objects that run on the server can be accessed by its name. For instance, if you had a GridView named GridView1 with a data source called DS you can access their properties like:

    me.GridView1 or me.DS

    For your case, you probably want to get the selection the user clicked. This can be done a few ways. With the GridView, you can have multiple columns. One column can be a hyperlink that has query parameters attached to it. Or, you can have hidden column with the values you want to collects. When a user selects an item from the gridview, you can access that event and get the selected row and all its values.

    jason
    if i was able to help, rate my post!

  3. #3
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Alternative to DataList to show aingle records

    Here's an example of a Gridview, with its event:

    Code:
     <asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoGenerateColumns="false"
                BackColor="White" BorderWidth="0px" BorderStyle="Solid" CellPadding="0"
                EmptyDataText="No Folders" ForeColor="#333333" GridLines ="Both" Width="100%" 
                OnSorting="GridViewSortEventHandler" ShowHeader="true" >
                    <HeaderStyle BackColor="#d3e0ef" Font-Bold="True" ForeColor="White"  />
                    <AlternatingRowStyle BackColor="#d3e0ef" ForeColor="#284775" />
                    <RowStyle BackColor="white" ForeColor="#333333" />
            
                    <Columns>
                        <asp:TemplateField SortExpression="GridViewSortEventHandler" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="10" ItemStyle-Width="20"  HeaderImageUrl="~/images/up_directory_icon.gif" >
                            <ItemTemplate>
                                <asp:Label runat="server" ID="objValue" Visible="false" Text='<%#Eval("objectValue")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30" HeaderText="Sel" >
                            <ItemTemplate>
                                <asp:CheckBox ID="cb" runat="server" OnCheckedChanged="cb_Click" AutoPostBack="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="50" HeaderImageUrl="~/images/folder.gif">
                            <ItemTemplate>
                                <asp:ImageButton ID="selectDir" runat="server" ImageUrl="~/images/folder.gif" OnClick="selectDir_click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left">
                            <ItemTemplate>
                                    <asp:Label runat="server" ID="dirName" Text='<%#Eval("directoryName")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>                
                    </Columns>        
            </asp:GridView>
    To bind the data, you can do something like this:

    Code:
    Public Sub bindData(ByVal getList As List(Of directoryList))
            If (getList.Count < 1) Then
                Dim dl As New directoryList
                dl.directoryName = ""
                getList.Add(dl)
    
                Me.GridView1.DataSource = getList
                Me.GridView1.DataBind()
    
                Me.GridView1.Rows(0).Visible = False
    
            Else
                Me.GridView1.DataSource = getList
                Me.GridView1.DataBind()
            End If
            
        End Sub
    In this example, I have a list of objects with corresponding columns. You can list, datasets, arrays, etc.

    To get a selected row, assuming this is coming from a button click, you can get the button and find its parent row

    Code:
     Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand
            'get the button that the user clicked.  can also use linkedbutton, imagebutton, etc
            Dim btn As Button = DirectCast(sender, Button)
             ' get the gridview it was on
            Dim gv As GridViewRow = DirectCast(btn.Parent.Parent, GridViewRow)
            ' get teh string value from that controls, assuming it's called myValue
            Dim myButtonId As String = DirectCast(gv.FindControl("myValue"), Label).Text
    end sub
    This should give you a start. if you have any questions, post back here

    jason
    if i was able to help, rate my post!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: Alternative to DataList to show aingle records

    Quote Originally Posted by jasonwucinski View Post
    Here's an example of a Gridview, with its event:

    Code:
     <asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoGenerateColumns="false"
                BackColor="White" BorderWidth="0px" BorderStyle="Solid" CellPadding="0"
                EmptyDataText="No Folders" ForeColor="#333333" GridLines ="Both" Width="100%" 
                OnSorting="GridViewSortEventHandler" ShowHeader="true" >
                    <HeaderStyle BackColor="#d3e0ef" Font-Bold="True" ForeColor="White"  />
                    <AlternatingRowStyle BackColor="#d3e0ef" ForeColor="#284775" />
                    <RowStyle BackColor="white" ForeColor="#333333" />
            
                    <Columns>
                        <asp:TemplateField SortExpression="GridViewSortEventHandler" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="10" ItemStyle-Width="20"  HeaderImageUrl="~/images/up_directory_icon.gif" >
                            <ItemTemplate>
                                <asp:Label runat="server" ID="objValue" Visible="false" Text='<%#Eval("objectValue")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30" HeaderText="Sel" >
                            <ItemTemplate>
                                <asp:CheckBox ID="cb" runat="server" OnCheckedChanged="cb_Click" AutoPostBack="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="50" HeaderImageUrl="~/images/folder.gif">
                            <ItemTemplate>
                                <asp:ImageButton ID="selectDir" runat="server" ImageUrl="~/images/folder.gif" OnClick="selectDir_click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left">
                            <ItemTemplate>
                                    <asp:Label runat="server" ID="dirName" Text='<%#Eval("directoryName")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>                
                    </Columns>        
            </asp:GridView>
    To bind the data, you can do something like this:

    Code:
    Public Sub bindData(ByVal getList As List(Of directoryList))
            If (getList.Count < 1) Then
                Dim dl As New directoryList
                dl.directoryName = ""
                getList.Add(dl)
    
                Me.GridView1.DataSource = getList
                Me.GridView1.DataBind()
    
                Me.GridView1.Rows(0).Visible = False
    
            Else
                Me.GridView1.DataSource = getList
                Me.GridView1.DataBind()
            End If
            
        End Sub
    In this example, I have a list of objects with corresponding columns. You can list, datasets, arrays, etc.

    To get a selected row, assuming this is coming from a button click, you can get the button and find its parent row

    Code:
     Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand
            'get the button that the user clicked.  can also use linkedbutton, imagebutton, etc
            Dim btn As Button = DirectCast(sender, Button)
             ' get the gridview it was on
            Dim gv As GridViewRow = DirectCast(btn.Parent.Parent, GridViewRow)
            ' get teh string value from that controls, assuming it's called myValue
            Dim myButtonId As String = DirectCast(gv.FindControl("myValue"), Label).Text
    end sub
    This should give you a start. if you have any questions, post back here

    jason
    Thank you, Jason. I will give this a go and see how I get on :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width