没有获得下拉列表的选定值

时间:2018-01-22 05:22:35

标签: c# asp.net

我在Invoice.aspx form.i中有一个名为country的字段,其中包含下面提到的代码。

invoice.aspx

<asp:DropDownList ID="lstCountryy" runat="server" Width="216px" Height="27px" AutoPostback="false">
</asp:DropDownList>

invoice.aspx.cs

lstCountryy.SelectedValue = dtInquiry.Rows[0]["country"].ToString();

页面加载:

 protected void Page_Load(object sender, EventArgs e)
    {
        txtTotal.Attributes.Add("readonly", "readonly");
        txtvatt.Attributes.Add("readonly", "readonly");//added by chetan
        txtDiscount.Attributes.Add("readonly", "readonly");//added by chetan
        txtAmountInWords.Attributes.Add("readonly", "readonly");
        if (!IsPostBack)
        {           
            loadInvoiceDetails();

            //SetAllCountries();//added by chetan

            if (lstCountryy.SelectedValue == "U.A.E" || lstCountryy.SelectedValue == "BAHRAIN" || lstCountryy.SelectedValue == "SAUDI ARABIA")
            {


                txtvat.Text = "";
                txtvatt.Text = "";
                txtBankCharge.Text = "";
                txtDiscount.Text = "";
                txtDiscountText.Text = "";
                txtTotal.Text = "";

                vattr.Style.Add("display", "float");
                trdeclaration.Visible = false;

                //txtvat.Enabled = true;

            }
            else
            {
                txtvat.Text = "";
                txtvatt.Text = "";
                txtBankCharge.Text = "";
                txtDiscount.Text = "";
                txtDiscountText.Text = "";
                txtTotal.Text = "";


                vattr.Style.Add("display", "none");



                trdeclaration.Visible = true;
            }

        }


    }

绑定下拉列表:

protected void SetAllCountries()
    {
        try
        {
            string queryStrUserType = "SELECT country_id,country_name FROM  crm_countries";
            ClassDtBaseConnect clsDtResult = new ClassDtBaseConnect();
            DataTable dt = clsDtResult.GetDataTable(queryStrUserType);
            lstCountryy.DataSource = dt;
            lstCountryy.DataValueField = "country_id";
            lstCountryy.DataTextField = "country_name";
            lstCountryy.DataBind();
            lstCountryy.Items.Insert(0, "--Select--");

        }
        catch (Exception Ex)
        {

        }
    }

dtInquiry是我的查询表,其中有国家/地区字段,我从查询表单中获取值到发票表单。 我没有得到选定的值。它显示空白(“”)。我不知道这有什么不对。任何帮助都将受到高度赞赏。我知道这是一个基本问题。我是c#的初学者。

1 个答案:

答案 0 :(得分:0)

我没有得到所选的值。它应该是这样的:SetAllCountries应该是第一个然后loadinvoicedetails。

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {    
            SetAllCountries();     
            loadInvoiceDetails();
            .....
            .....
        }
     }