?
Solved

Enable/Disable Dropdown list in ASP.NET Gridview

Posted on 2009-02-20
16
Medium Priority
?
1,428 Views
Last Modified: 2012-05-06
I have a dropdownlist in an ASP.NET gridview control.  The control appears in each row of the grid and has the proper values for each row.  My questions that I only want the user to be able to select a new value for the listbox only when they select "edit" for the row.    Is this possible?
0
Comment
Question by:Mike_Stevens
  • 10
  • 6
16 Comments
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693243
yes,
When you set the asp:TemplateField set the ItemTemplate`s drop down as enablade = false and true in the  EditItemTemplate
0
 
LVL 18

Accepted Solution

by:
David Robitaille earned 2000 total points
ID: 23693277
sorry, enablade =enabled
please send some code if it`s not clear
0
 

Author Comment

by:Mike_Stevens
ID: 23693423
<asp:DropDownList ID="Email_Category" AutoPostBack="true" Enabled="false"
                            DataTextField="Category" DataValueField="Category"
                            DataSourceID="SqlDataSource2" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Description") %>' />
                         </ItemTemplate>
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693528
that what i was thinking, it is in a ItemTemplate...
could you send the code between the  <asp:TemplateField ...</asp:TemplateField>
 
0
 

Author Comment

by:Mike_Stevens
ID: 23693555
Complete code

<asp:TemplateField HeaderText="Category" SortExpression="Description">
                         <ItemTemplate>                  
                        <asp:DropDownList ID="Email_Category" AutoPostBack="true" Enabled="false"
                            DataTextField="Category" DataValueField="Category"
                            DataSourceID="SqlDataSource2" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Description") %>' />
                         </ItemTemplate>
                            <HeaderStyle BackColor="#2B5E91" Font-Bold="False" ForeColor="White" />
                        </asp:TemplateField>
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693591
Voila!
<asp:TemplateField HeaderText="Category" SortExpression="Description">
                         <ItemTemplate>                   
                        <asp:DropDownList ID="Email_Category" AutoPostBack="true" Enabled="false"
                            DataTextField="Category" DataValueField="Category"
                            DataSourceID="SqlDataSource2" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Description") %>' />
                         </ItemTemplate>
<EditItemTemplate>                   
                        <asp:DropDownList ID="Email_Category" AutoPostBack="true" Enabled="true"
                            DataTextField="Category" DataValueField="Category"
                            DataSourceID="SqlDataSource2" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Description") %>' />
                         </EditItemTemplate>
 
                            <HeaderStyle BackColor="#2B5E91" Font-Bold="False" ForeColor="White" />
                        </asp:TemplateField>

Open in new window

0
 

Author Comment

by:Mike_Stevens
ID: 23693696
I tried that.....it allows the dropdownlist to be enabled/disabled.  When I change the value the screen posts back and the value is changed.  However when i click "update" to update the row the value is changed back to what originally was.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693759
i seen to see your SqlDataSource2 thet is binded to  the DropDownList and SqlDataSource1 (or whatever id it is) that is binded to the gridview.
There must be something with the update parameters.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693772
by the way, set the AutoPostBack="false", the post back when you chance the value is not needed...
0
 

Author Comment

by:Mike_Stevens
ID: 23693840
I already changed the postback value to false.   The grid is bound to SQLDatasource1.  The dropdownlist is bound to sqldatasoure2 becuase the values that populate the list are stored in a database table.   I am not sure where to loook to resolve the issue of the value changing back.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23693868
I need to see your SqlDataSource2 and SqlDataSource1.
 
0
 

Author Comment

by:Mike_Stevens
ID: 23693909
this is the form load events

 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Me.SqlDataSource1.ConnectionString = global_values.sConn
        Me.SqlDataSource2.ConnectionString = global_values.sConn
 
        If IsPostBack = False Then
            Dim sSQL1 As String = "SELECT * FROM tblEmail_Categories ORDER BY Category, Sort"
 
            Me.SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.Text
            Me.SqlDataSource2.SelectCommand = sSQL1
            Me.SqlDataSource2.DataBind()
        End If
 
        Dim sSQL As String = "SELECT * FROM tblMailing_List ORDER BY Email_Address"
 
        Me.SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.Text
        Me.SqlDataSource1.SelectCommand = sSQL
        Me.SqlDataSource1.DataBind()
 
 
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23694000
how do you think the update will be done? you need to set a update command...
i need to see the tblEmail_Categories and tblMailing_List structure and the update command (if you have any?)
 Also, setting those SqlDataSource in the form load is not the "normal" way of doing this. you should look at those tutorial.
http://www.asp.net/learn/data-access/tutorial-47-vb.aspx
http://www.asp.net/learn/data-access/tutorial-49-vb.aspx  
0
 

Author Comment

by:Mike_Stevens
ID: 23694069
Prior to adding the new dropdownlist I never needed a update command for the grid.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23694174
And how the data was updating???
All I can say is don`t know how you where doing this before, but it`s unusual to do this this way.
But i think the first goal of the question is answered
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 23695329
so, how are you going on that one?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question