none
DropDownList的值没取到!? RRS feed

  • 问题

  •     下面这段代码中有三个DropDownList,分别是DropDownList_City、dropDownList_Road、DropDownList_Address,分别代表着城市、路段、门牌号。

        这段代码放在 protected void DropDownList_Address_SelectedIndexChanged(object sender, EventArgs e) 中正常运行,可是放在protected void Page_Load(object sender, EventArgs e)、 DropDownList_City_SelectedIndexChanged、 DropDownList_Road_SelectedIndexChanged中运行,会出现如图1所示错误。

        我想,这是因为页面刚打开,DropDownList的初始值没有被selected到。该如何修改呢?

    ---------------------------代码1--------------------------

    public void BindLable_TypeOfPayment()
        {
            string strconn = "Data Source=.;Initial Catalog=长铁物业公司资产数据库;Integrated Security=True";
            SqlConnection cn = new SqlConnection(strconn);
            cn.Open();
            string typeofpaymentTop = "select top (1) TypeOfPayment from ContractTable where City='";
            string typeofpaymentSearch = DropDownList_City.SelectedItem.Value.ToString() + "' and Road ='" + DropDownList_Road.SelectedItem.Value.ToString() + "' and [Address] ='" + DropDownList_Address.SelectedItem.Value.ToString() + "' and ContractType ='租赁' Order by LeaseDateTo DESC";
            typeofpaymentTop = typeofpaymentTop + typeofpaymentSearch;
            SqlCommand cm = new SqlCommand(typeofpaymentTop, cn);
            SqlDataReader dr = cm.ExecuteReader();
            dr.Read();
            
            if (dr["TypeOfPayment"].ToString() == "")
                Label_TypeOfPayment.Text = "首次启用,无前期租赁方式";
            else
                Label_TypeOfPayment.Text = dr["TypeOfPayment"].ToString();
            
            cn.Close();
    
        }

    ---------------------------图1-----------------------------


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年5月29日 1:18

答案

  •     给sql中union一个“未选择”,整个世界清静了,规避了这个问题!

    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月14日 10:16

全部回复

  • 你好,你的估計是對的。從那錯誤信息來看,應該是那幾個DropDownList還有值,或還沒有SelectedItem。

    你可以先用
    If (null != DropDownList_City.SelectedItem)

    如果可以通過才去取它的值吧。


    大家一齊探討、學習和研究,謝謝!
    MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
    MCT, Microsoft Community Star(TW & HK),
    Microsoft MVP for VB.NET since 2003
    My MSMVP Blog

    2013年5月29日 7:39
  •     我发现这个错误就是在于如果DropDownList如果没有经过点击选取,就不会有初始值,(尽管每个DropDownList显示出来了City、Road、Address),所以当页面一打开的时候,下面这段代码是不会显示结果的,必须等每个DropDownList重新点击选择后才能开始工作。

        有什么好办法可以把初始值也正确显示么?


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年5月31日 0:21
  • 你的Dropdowlist请尝试为其中的某个ListView设置Selected=True:

    <asp:DropDownList ID="id" runat="server">
          <asp:ListItem Text="a" Selected="True" Value="a"></asp:ListItem>
      </asp:DropDownList>

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月31日 1:13
  •     我的情况不一样啊,我的DropDownList的数据源是绑定了SQLDataSource的,没有ListItem啊!

    <asp:DropDownList ID="DropDownList_City" runat="server" 
                        AutoPostBack="True" DataSourceID="SqlDataSource_City" DataTextField="City" 
                        DataValueField="City" Width="80px">
                    </asp:DropDownList>
    
    
    <asp:SqlDataSource ID="SqlDataSource_City" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
                        
                        SelectCommand="SELECT DISTINCT a.City FROM ContractTable AS a INNER JOIN PropertyTable AS b ON a.City + a.Road + a.Address = b.City + b.Road + b.Address WHERE (b.PropertyState = '1') order by city">
                    </asp:SqlDataSource>


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月4日 0:20
  • 你的下拉列表是在FormView里边还是独立于数据控件(在外边)的?


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月4日 1:42
  •     在外边的!就是最简单的网页,网页上放了一些SQLDataSource、DropDownList控件,没有FormView。

    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月4日 2:11
  • 在Page_Load中:

    if(!IsPostBack)
    {
          Dropdownlist1.SelectedIndex = 0;
    }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月4日 2:24
  •     根据您的提示做了修改,当DropDownList_City改变的时候其他的DropDownList也会有初始值了,可是这个初始值发生了错位,例如:长沙市 对应-车站南路,广州 对应 惠州,可是我Response了一下,发现界面中的DropDownList值和Response值不同步,在Response中显示出来的DropDownList_Road和DropDownList_Address的值,虽然会变,但会慢一步!!!如图,下面有代码!

        总之DropDownList显示的值是对的,而Response出来的值错位了,慢了一拍!

    --------------------------------------图1---------------------------------------

    ----------------------------------代码----------------------------------

    protected void DropDownList_City_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList_Road.SelectedIndex = 0;
            DropDownList_Address.SelectedIndex = 0;
            string strconn = "Data Source=.;Initial Catalog=长铁物业公司资产数据库;Integrated Security=True";
            SqlConnection cn = new SqlConnection(strconn);
            cn.Open();
            string typeofpaymentTop = "select top (1) ContractCompany from ContractTable where City='";
            string typeofpaymentSearch = DropDownList_City.SelectedItem.Value.ToString() + "' and Road ='" + DropDownList_Road.SelectedItem .Value.ToString() + "' and [Address] ='" + DropDownList_Address.SelectedItem.Value.ToString() + "' and ContractType ='租赁' Order by LeaseDateTo DESC";
            typeofpaymentTop = typeofpaymentTop + typeofpaymentSearch;
            SqlCommand cm = new SqlCommand(typeofpaymentTop, cn);
            SqlDataReader dr = cm.ExecuteReader();
            dr.Read();
            Response.Write(typeofpaymentTop);


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月4日 3:44
  • 那你在Page_Load中设法同步这些数值(同时给三个Dropdownlist的SelectedIndex都赋值为0)。

    或者把三个Dropdownlist的AutoPostBack=True,应该会自动更新的。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月4日 6:06
  • 那你在Page_Load中设法同步这些数值(同时给三个Dropdownlist的SelectedIndex都赋值为0)。

    或者把三个Dropdownlist的AutoPostBack=True,应该会自动更新的。



        我是这样做的啊,可是就是不行。感觉我代码并没有错,而是程序执行时,可能先运行了前台再运行后台,造成的错位!

        是不是不应该在SelectedIndex事件中写啊,是不是要在其他的事件中写?


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!


    2013年6月4日 9:19
  • 你前台写了啥?

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月4日 9:21
  • 没写啥啊!就是下面的代码:(两部分,3个dropDownList是通过3个sqldatasource联动的

    <asp:DropDownList 
                        ID="DropDownList_City" runat="server" 
                        AutoPostBack="True" DataSourceID="SqlDataSource_City" DataTextField="City" 
                        DataValueField="City" Width="80px" 
                        onselectedindexchanged="DropDownList_City_SelectedIndexChanged" 
                        onload="DropDownList_City_Load">
                    </asp:DropDownList>
                &nbsp;&nbsp;&nbsp;
                    所在路段:<asp:DropDownList ID="DropDownList_Road" runat="server" 
                        AutoPostBack="True" DataSourceID="SqlDataSource_Road" DataTextField="Road" 
                        DataValueField="Road" Width="80px" 
                        onselectedindexchanged="DropDownList_Road_SelectedIndexChanged" >
                    </asp:DropDownList>
    &nbsp;
                </td>
                <td align="left">
                    门牌号:<asp:DropDownList ID="DropDownList_Address" runat="server" AutoPostBack="True" 
                        DataSourceID="SqlDataSource_Address" DataTextField="Address" 
                        DataValueField="Address" 
                        onselectedindexchanged="DropDownList_Address_SelectedIndexChanged">
                    </asp:DropDownList>

    ----------------------------------------------sqldatasource----------------------------------

    <asp:SqlDataSource ID="SqlDataSource_City" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
                        
                        SelectCommand="SELECT DISTINCT a.City FROM ContractTable AS a INNER JOIN PropertyTable AS b ON a.City + a.Road + a.Address = b.City + b.Road + b.Address WHERE (b.PropertyState = '1') order by city">
                    </asp:SqlDataSource>
                </td>
                <td width="600">
                    <asp:SqlDataSource ID="SqlDataSource_Road" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
                        
                        SelectCommand="SELECT DISTINCT [Road] FROM [PropertyTable] WHERE ([City] = @City) and propertystate='1'">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownList_City" Name="City" 
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
                <td>
                    <asp:SqlDataSource ID="SqlDataSource_Address" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
                        
                        SelectCommand="SELECT [Address] FROM [PropertyTable] WHERE (([City] = @City) AND ([Road] = @Road)) and propertystate='1'">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownList_City" Name="City" 
                                PropertyName="SelectedValue" Type="String" />
                            <asp:ControlParameter ControlID="DropDownList_Road" Name="Road" 
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!



    2013年6月5日 0:43
  •     给sql中union一个“未选择”,整个世界清静了,规避了这个问题!

    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月14日 10:16