• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1138
  • Last Modified:

How to lock the drop down menu selecteditem.value?

I use asp.net and c#

I have a form which has a drop down list databind with a table in MSSQL 2005 DB

When I selected an item, it will be locked and cannot change the drop down selected item.

How can I do that?
0
techques
Asked:
techques
  • 4
  • 4
1 Solution
 
MrAgileCommented:
hi There,

you can call a javascript function with an onchange event and then disable the dropdown list from the function.

or

You can call an event in the code behind of your page and then disable the dropdown list from there.

Sean
0
 
guru_samiCommented:
Hi techques,
So do you mean once your select the Item you want to lock it?
You can do something like this:

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

But the problem will b..what if you selected a wrong value? you will need a button or some other control to Enable back the DDL.

If that above code does not meet your requirements please provide more info on what you are trying to achieve.
0
 
techquesAuthor Commented:
Hi

When I choose item on drop down list, it still enable.

Then, I add a breakpoint on this.clientid.Enabled = false;

Actually, it did not run that code when i select an item.


validateRequest="false" AutoEventWireup="true"
 
<asp:dropdownlist id=clientid runat="server" AutoPostBack="True" OnSelectedIndexChanged="clientid_SelectedIndexChanged"></asp:dropdownlist>
 
C# code
protected void clientid_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.clientid.Enabled = false;
        }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
guru_samiCommented:
this is a sample working code. Not sure whats going on with your code:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
            DataSourceID="SqlDataSource1" DataTextField="ProductName" 
            DataValueField="ProductName"           onselectedindexchanged="DropDownList1_SelectedIndexChanged">        </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" 
SelectCommand="SELECT [ProductName] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
 
Code Behind .aspx.cs file:
 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList1.Enabled = false;
    }

Open in new window

0
 
techquesAuthor Commented:
As I use aspx to include a ascx placeholder. The drop down list is in the ascx page.

Will it the reason not execute
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

and cannot exeute
this.DropDownList1.Enabled = false;

as the id of this.DropDownList1 has changed to ct_101.DropDownList1

0
 
guru_samiCommented:
if the dropdown is in ascx file then use the selectedIndexChanged code in the code behind of ascx file
0
 
techquesAuthor Commented:
Yes, I did use selectedIndexChanged code in the code behind of ascx file, but the code did not execute when i select an item in drop down list.

0
 
guru_samiCommented:
thats weird...
do you have AutoPostBack=true for the dropdownlist correct?
Can you post your markup and code-behind
0
 
techquesAuthor Commented:
it is ok now as i use button to lock and unlock to set the this.DropDownList1.Enabled = false; and true;

Thanks for help
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now