dropdownlist自动选择第一个值 [英] dropdownlist selects first value automatically

查看:80
本文介绍了dropdownlist自动选择第一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



i在一个页面中也有树下拉列表(ok)按钮点击事件。

所有下拉数据都来自数据库。



1下拉标准

2下拉列表

3下拉列章



当我点击确定按钮然后第3个下拉总是自动从列表中选择第一个值。

i也在同一页面中有文件上传控件。





谢谢。





hi there

i have tree dropdownlist in one page also (ok) button click event.
all dropdown data comes from database.

1 dropdown for standard
2 dropdown for subject
3 dropdown for chapter

when i click on ok button then 3rd dropdown always automatically select first value from list.
i also have fileupload control in same page.


Thanks.


//on page load event
if (!Page.IsPostBack)
       {
           FillClass();
       }

public void FillClass()
    {
        cn.Close();
        cn.Open();
        string com = "Select * from Class";
        SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "classname";
        DropDownList1.DataValueField = "srno";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
        cn.Close();
    }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {       
            cn.Close();
            cn.Open();
            string com = "Select * from SubjectName where std='" + DropDownList1.SelectedItem.Text + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList2.DataSource = dt;
            DropDownList2.DataTextField = "subject";
            DropDownList2.DataValueField = "srno";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
            cn.Close();        
    }
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

       
            cn.Close();
            cn.Open();
            string com = "Select * from Chapter where std='" + DropDownList1.SelectedItem.Text + "' and subject='" + DropDownList2.SelectedItem.Text + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList3.DataSource = dt;
            DropDownList3.DataTextField = "chapter";
            DropDownList3.DataValueField = "srno";
            DropDownList3.DataBind();
            DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
            cn.Close();
        }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DropDownList3.Text;       
    }










<%@ Page Title="" Language="C#" MasterPageFile="~/admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="papergenr.aspx.cs" Inherits="admin_papergenr" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 

        onselectedindexchanged="DropDownList1_SelectedIndexChanged">        
    </asp:DropDownList>
    

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
    </asp:DropDownList>


    <asp:DropDownList ID="DropDownList3" runat="server">
    </asp:DropDownList>



    <br />
   
 
 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</asp:Content>

推荐答案

确保 -

1.如果您已在页面加载事件中编写了绑定下拉列表列表的逻辑,则必须编写检查回帖。以下代码块可以帮助

Make sure that-
1. If you have written the logic to bind dropdownlist list in the page load event then you must write the check for post back. Following code block may help
if(!Page.IsPostback)
{
   // logic for binding dropdownlist 
}



2.确保在OK按钮点击事件中没有为绑定下拉列表编写任何逻辑,除非确实需要。



如果这样并没有解决你的问题,请用相应的代码更新你的问题,以便建议实际的解决方案。



希望,它有帮助:)


2. Make sure that you haven't written any logic for binding dropdownlist in the OK button click event unless it is really required.

If this doesn't solve you problem, please update your question with the respective code so that actual solution can be suggested.

Hope, it helps :)


这篇关于dropdownlist自动选择第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆