Sooraj
Sooraj

Reputation: 10567

How to insert content in text box when drop down menu item is changed in C#

In the following code i wish to insert a value corresponding to the drop down menu Invoiceno in the text box TotalAmount. Also i wish to enable a text box ChequeNumber if the value of drop down menu PaymentMode is cheque. But the values appear in the text box only after i press the submit button. What am i doing wrong?

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  namespace OrderManagementSystem
  {
  public partial class MakePayment : System.Web.UI.Page
  {
    Dictionary<int, int> InvoiceAmount = new Dictionary<int, int>();



    protected void Page_Load(object sender, EventArgs e)
    {
        ChequeNumber.Enabled = false;

    }

    protected void InvoiceNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        InvoiceAmount.Add(1111, 100000);
        InvoiceAmount.Add(2222, 200000);
        InvoiceAmount.Add(3333, 300000);
        InvoiceAmount.Add(4444, 400000);


        int InvoiceValue = Convert.ToInt32(InvoiceNo.SelectedValue);
        foreach (KeyValuePair<int, int> item in InvoiceAmount)
        {
            if (InvoiceValue == item.Key)
            {
                TotalAmount.Text = Convert.ToString(item.Value);
            }
        }
    }

    protected void PaymentMode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (PaymentMode.SelectedValue == "Cheque")
        {

            ChequeNumber.Enabled = true;

            if (ChequeNumber.Text=="")
            {

            }
        }

    }

    protected void TotalAmount_TextChanged(object sender, EventArgs e)
    {

    }
  }
 }

Upvotes: 0

Views: 142

Answers (2)

Rakesh Sharma
Rakesh Sharma

Reputation: 26

1> add AutoPostBack="true" on DropDownList in your .aspx page.

2> the second problem lies in your page load function

protected void Page_Load(object sender, EventArgs e)  
{    
     ChequeNumber.Enabled = false;  
}

the textbox ChequeNumber is disabled every time when the page is loaded. To resolve this issue you need to keep the state of ChequeNumber in ViewState["xyz"]

Upvotes: 0

Stilgar
Stilgar

Reputation: 23571

Add AutoPostBack="true" on your DropDowlList declaration. This will make the page post to the server when the value is changed. You may consider adding UpdatePanel to make the postback via AJAX.

Upvotes: 2

Related Questions