Dropdownlist selected item text always returning t

2020-07-10 11:11发布

i am using this code to fill dropdownlist from database.

public void fillcountry()
{
    BL obj = new BL();
    DataSet ds = obj.dss("select * from Country  ");
    drplistcountry.DataSource = ds;
    drplistcountry.DataTextField = "CountryName";
    drplistcountry.DataValueField = "CountryId";
    drplistcountry.DataBind();
    drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
}

i am Using this fillcountry() in page load() event. and Rerutning selecteditm.text on Button Click event

drplistcountry is always showing First index text , How to solve it?

6条回答
我命由我不由天
2楼-- · 2020-07-10 11:50

Its is very simple but very difficult to find out, check your value Datavaluefield of all the items should not be blank or null.

Means:

ddl_Name.DataTextField = "NAME";
ddl_Name.DataValueField = "Roll";

if value is blank means you cannot will get the first items only.

查看更多
虎瘦雄心在
3楼-- · 2020-07-10 11:52

I was also facing same issue but in my case dropdown value was same for all items, which was causing issue. So, make sure you are binding dropdown list correctly.

:)

查看更多
戒情不戒烟
4楼-- · 2020-07-10 11:54

In .aspx page:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" 
EnableViewState="true" %>

For Dropdownlist Control set EnableViewState property to true.

In .aspx.cs page:

In PageLoad event check for following:

if(!IsPostBack)
{ 
    fillcountry();
}
查看更多
贼婆χ
5楼-- · 2020-07-10 11:55

An easy way You can use repeater to make select in html code like this

<select id="ddlAlbum" class="form-control" name="Cat">
        <asp:Repeater ID="rpt" runat="server">

            <ItemTemplate>

            <option  value='<%#Eval("CatID") %>'>
                <%#Eval("Title") %>
            </option>
                </ItemTemplate>

        </asp:Repeater>
        </select>

for get data from data base use ado.net

 public void fillAlbum()
    {
        SqlConnection myconnection = new SqlConnection("Data Source=.;Initial Catalog=person;Integrated Security=True");
        SqlDataAdapter myadapter = new SqlDataAdapter("Select CatID,Title  from Category", myconnection);
        DataTable dt = new DataTable();
        myadapter.Fill(dt);
        rpt.DataSource = dt;
        rpt.DataBind();



    }

and for get selected value. the select element name is ("cat") So

string cat = Request.Form["Cat"];

and store to db simply

  SqlConnection myconnection = new SqlConnection("Data Source=.;Initial Catalog=person;Integrated Security=True");
        SqlCommand mycommand = new SqlCommand("insert into Picture(Title,Pic,CatID) values(@pname,@pic,@cat)", myconnection);

        mycommand.Parameters.AddWithValue("pname", txtname.Text.Trim());
        mycommand.Parameters.AddWithValue("pic", path);
        string cat = Request.Form["Cat"];
        mycommand.Parameters.AddWithValue("cat", cat.ToString());


        myconnection.Open();
        mycommand.ExecuteNonQuery();
        myconnection.Close();
查看更多
Emotional °昔
6楼-- · 2020-07-10 11:57

Try this code

And you shoud call this function only once.

 public void fillcountry()
    {
        BL obj = new BL();
        DataSet ds = obj.dss("select * from Country  ");
        drplistcountry.DataSource = ds;
        drplistcountry.DataTextField = "CountryName";
        drplistcountry.DataValueField = "CountryId";
        drplistcountry.DataBind();
        drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
        drplistcountry.SelectedIndex = drplistcountry.Items.IndexOf(drplistcountry.Items.FindByText("--Select--"));
    }
查看更多
再贱就再见
7楼-- · 2020-07-10 12:07

DropdownList selected Item Text value will work like this.

String value="India";
drplist.SelectedIndex = drplist.Items.IndexOf(drplist.Items.FindByText(value));
查看更多
登录 后发表回答