0

I have a field named country in Invoice.aspx form. I have the below code.

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();

Page load:

 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;
            }

        }
      
       
    }

Bind dropdownlist:

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 is my Inquiry table in which country field is there and I am fetching the value from inquiry form to invoice form. I am not getting the selected value; it shows blank (""). I don't know what's wrong with that. I am a beginner in C#.

halfer
  • 19,824
  • 17
  • 99
  • 186
chetan kambli
  • 794
  • 5
  • 21
  • 2
    Did you fill the dropdown first ? – lucky Jan 22 '18 at 05:24
  • please show your dropdown related code here... may be you not setting data-source correctly. – Muhammad Asad Jan 22 '18 at 05:31
  • I don't see `DropDownList` bound with any data source, which data source you're binding to that control? – Tetsuya Yamamoto Jan 22 '18 at 05:32
  • I am sure, you are binding drop down in page load without IsPostback condition which binds the drop down every time making its selection null – Krishna Jan 22 '18 at 05:34
  • @HafizAsad...i updated my code – chetan kambli Jan 22 '18 at 05:41
  • 1
    Why SetAllCountries commented in page load ? – Krishna Jan 22 '18 at 05:46
  • The ListControl.SelectedValue property is a getter only. Use `DropDownList1.Items.FindByValue("some_value_here").Selected = true;` to set the value of a ListControl. See https://stackoverflow.com/a/6296615/242 and https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx for more info. – Dhaust Jan 22 '18 at 06:01
  • @Krishna...after uncommenting setallcountries.....i am getting the selected country..while debugging selected value shows ""....selected value should be country id so that it will check for if condition – chetan kambli Jan 22 '18 at 08:48
  • for this line: lstCountryy.SelectedValue = dtInquiry.Rows[0]["country"].ToString();............selected value shows "" .it should be "18"(id of the country" – chetan kambli Jan 22 '18 at 08:52
  • @Stormcloak....yes... – chetan kambli Jan 22 '18 at 09:05

1 Answers1

0

I was not getting the selected value.It should be like this: SetAllCountries Should be first and then loadinvoicedetails.

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {    
            SetAllCountries();     
            loadInvoiceDetails();
            .....
            .....
        }
     }
chetan kambli
  • 794
  • 5
  • 21