Codebehind dropdownlist listview datasource

Codebehind dropdownlist listview datasource
0.0 0

#1

I started to use ASP.NET and I’m having problem with Codebehind.
What do I have to write for my code to show on the webpage.
<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“Fun4.aspx.cs” Inherits=“Fun4” %>

Welcome To DogNews

            <AlternatingItemTemplate>
                <li style="background-color: #FFFFFF; color: #284775;">Dogs:
                <asp:Label ID="DogsLabel" runat="server" Text='<%# Eval("Dogs") %>' />

                    <br />
                    Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    <br />

                </li>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <li style="background-color: #999999;">Dogs:
                <asp:Label ID="DogsLabel1" runat="server" Text='<%# Eval("Dogs") %>' />
                <br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </li>
        </EditItemTemplate>
        <EmptyDataTemplate>
            No data was returned.
        </EmptyDataTemplate>
       <InsertItemTemplate>
            <li style="">Dogs:
                <asp:TextBox ID="DogsTextBox" runat="server" Text='<%# Bind("Dogs") %>' />
                <br />Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
            </li>
        </InsertItemTemplate>
        <ItemSeparatorTemplate>

  • Dogs:
                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Email")%>'>
                            <asp:ListItem>Select Gender</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                            <asp:ListItem>Male</asp:ListItem>
                        </asp:DropDownList>
                    <br />
                   
                    
                    Name:
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    <br />
                </li>
            </ItemTemplate>
            <LayoutTemplate>
                <ul id="itemPlaceholderContainer" runat="server" style="font-family: Verdana, Arial, Helvetica, sans-serif;">
                    <li runat="server" id="itemPlaceholder" />
                </ul>
                <div style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF;">
                </div>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <li style="background-color: #E2DED6;font-weight: bold;color: #333333;">Dogs:
                    <asp:Label ID="DogsLabel" runat="server" Text='<%# Eval("Dogs") %>' />
                    <br />
                    Name:
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    <br />
                </li>
            </SelectedItemTemplate>
        </asp:ListView>
    
    </div>
    </form>
    

  • #2

    Looks like you’ve opted to use WebForms for your project. What made you decide to use that instead of using MVC? It looks like you’re using a gridview and a drop down list. Is the dropdown list inside the gridview?


    #3

    I did not wanted to start with MVC. Yea there’s a drop down list inside the gridview. I don’t know how I use codebehind for it to appear on a webpage. Thank You.

    This is my Codebehind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Fun4 : System.Web.UI.Page
    {
    private object ds;

    public object DropDownList1 { get; private set; }
    
        
    
    protected void Page_Load(object sender, EventArgs e)
    
    {
    
        ListView2.DataSource = ds;
        ListView2.DataBind();
        DataBind();
    }
    

    }


    #4

    I will take a look at this when I get home and have more time to explain how things work in the asp.net webforms world. Just an FYI, Microsoft has plans to discontinue webforms. The latest version, ASP.NET Core, only uses MVC and no longer implements webforms.


    #5

    YIKES!!! As soon as I finish this project I’ll definitely start using MVC. Thank you for letting me know.


    #6

    I was not feeling well last night so I didn’t have a chance to look over it. I haven’t worked with straight asp.net controls in awhile so I’m going to create a proof of concept project to see how things work.

    As for the setup that I see in the code behind, drop down lists and grids require the datasource to be IEnumerable of some object (basically a list or array). This could be strings, numbers, or complex objects. If it’s a list of complex objects, you will also need to set the display property and the value property. You can either load the data in the code behind, which is what it looks like you’re trying to do, or you can create a datasource in the markup that links to a database. I see you’re also using commands so that will also take some wiring up in the code behind to get it to fire correctly.

    I’ll try to have some code examples similar to what you’re trying to do sometime today so you can see how things wireup and how to communicate with the controls between the code behind and the markup.

    Also, which version of visual studio are you using to create your project? Which version of the .NET framework are you using for your project?


    #7

    Ok. I’ve been working on this on and off all day between my normal work projects. Rather than posting a bunch of code snippets here, I decided to push the proof of concept project to my github for you to review. This project is somewhat similar to the one it looked like you were building.

    About this project:

    • It was created in Visual Studio 2015
    • It uses bootstrap (came already installed in the application so I used it for styling)
    • It does not contain any client or server-side validation.
    • Make sure you rebuild the solution to pull in the packages that are needed (project does not include the packages folder)
    • The project adds, updates, and removes records from a list in the code behind.

    If you have any questions, let me know.