DropDownList how to change the default selected items in two ways

Source: Internet
Author: User
Tags eval modify tostring

Introduction

Actually do not want to sum up this aspect of content, found too simple, but in this also fell. So still record it, is to remind oneself, do not despise too much foundation of thing, have this kind of psychology, will carry a big somersault.

An example

Here is a simulation of the most commonly used example, in the list, select Modify, the selected records, shown above, and change the default check in DropDownList.

Way One

Code:

<%@ Page language= "C #" autoeventwireup= true "codebehind=" Default.aspx.cs "inherits=" Wolfy.DropDownListDemo.Default "%> <! DOCTYPE html>  
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
    
Using System.Web.UI.WebControls; namespace Wolfy.dropdownlistdemo {public partial class Default:System.Web.UI.Page {protected void Page
            _load (object sender, EventArgs e) {dropdownlistprovince.datasource = GetList ();
            Dropdownlistprovince.datatextfield = "Name";
            Dropdownlistprovince.datavaluefield = "ID";
            Dropdownlistprovince.databind ();
            Repeaterlist.datasource = GetList ();
        Repeaterlist.databind (); } protected list<province> GetList () {list<province> List = new list<province&
            gt; (); List.
            Add (New Province () {ID = 1, Name = "Beijing", Pid = 0}); List.
            Add (New Province () {ID = 2, Name = "Shanghai", Pid = 0}); List.
            Add (New Province () {ID = 3, Name = "Henan", Pid = 0}); List. Add (New province () {ID = 4, Name = "Hebei", Pid = 0}); List.
            Add (New Province () {ID = 5, Name = "Hunan", Pid = 0}); List.
            Add (New Province () {ID = 6, Name = "Hubei", Pid = 0});
        return list;  } protected void Linkupdate_click (object sender, EventArgs e) {LinkButton link = Sender
            As LinkButton;
                if (link!= null) {int id = Convert.ToInt32 (link.commandargument); Province Pro = GetList ().
                Find (a => a.id = = ID); This. Literalid.text = pro.id.
                ToString (); This. Literallevel.text = Pro.
                Pid.tostring (); DropDownList's index is zero-based and the ID of the bound data starts at 1, so for the corresponding need to subtract one//change the default check this. Dropdownlistprovince.selectedindex = Pro.
            ID-1; }
        }
    }
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.