Solved

GridView and SelectedItem in DropDownList when editing

Posted on 2006-11-01
5
4,174 Views
Last Modified: 2012-06-27
I've tried for 2 days now and searched the net with no results. (well, lots of results, but still having problems)

Grid has templated fields for Item and Edit.  The Item field is a label with initials, and the Edit field is a DropDownList (that is populated from a sql data source and works just fine).  The problem when entering edit mode, I need to have the drop down show the selected initials.  currently all i can get is the first entry in the drop down list.  Any idea of what I'm missing????

  <asp:SqlDataSource ID="DropDownSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TaxConnectionString %>"
            SelectCommand="Proc_Tax706Logs_GetAdmins" SelectCommandType="StoredProcedure">
  </asp:SqlDataSource>
 <asp:GridView
            ID="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataSourceID="TaxLogSelectStatement"
            DataKeyNames="ID, AdminInitials"
            AllowSorting="True"
            AllowPaging="True">
 <ItemTemplate>
      <table cellpadding="0" cellspacing="0" border="1" align="left" >
            <tr>
                 <td ><asp:Label ID="AccountNoLabel" STYLE="text-align:center; " runat="server"
                            Text='<%# Eval("AccountNumber") %>' foreColor="teal"  Font-Bold="True" Font-Size="12px"  Width=75 /></td>
                <td ><asp:Label ID="AccountNameLabel" runat="server"  Text='<%# Eval("AccountName") %>'
                             foreColor="teal" Font-Bold="True" Font-Size="12px"  Width="200" /></td>
                <td ><asp:Label ID="AdminLabel" runat="server" STYLE="text-align: center; " 
                            Text='<%# Eval("AdminInitials") %>&nbsp;' Font-Bold="false" Font-Size="12px" width="50" /></td>
            </tr>

       </table>
  </ItemTemplate>
  <EditItemTemplate>
       <table >
          <tr >
                <td ><asp:Label ID="Label5" runat="server" Text='Account Number' /></td>
                <td ><asp:textbox ID="AccountNoTextBox"  runat="server"
                           Text='<%# Bind("AccountNumber") %>'  /></td>
           </tr >
           <tr >
                 <td ><asp:Label ID="Label6" runat="server" Text='Name' /></td>
                 <td ><asp:textbox ID="AccountNameTextBox" AutoPostBack="false" Text='<%# Bind("AccountName") %>'  /></td>
          </tr>
          <tr id="AdministratorEditRow" >
               <td ><asp:Label ID="Label7" runat="server" style=" text-align:right;" Text='Administrator' /></td>
               <td ><asp:DropDownList ID="OfficerDropDownListUpdate" AutoPostBack="false" runat="server" DataSourceID="DropDownSqlDataSource"
                          DataTextField="AdminInitials" SelectedText='<%# Eval("AdminInitials") %>' DataValueField="AdminID" /></td>
                            </tr>
              </tr>
      </table>
  </EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

thanks for taking a look

0
Comment
Question by:PTBNA
  • 3
5 Comments
 
LVL 6

Expert Comment

by:LeeHenry
ID: 17852169
In your code behind, you should set the dropdownlist selected value when in edit mode

OfficerDropDownListUpdate.SelectedIndex = yourSelectedIndex;

0
 

Author Comment

by:PTBNA
ID: 17852710
Ok, but which of the events should I use,
row databound,
row editing
row created,

I've worked with all of them, but am missing something to put it all together.

Do you have some sample code??

Plus, I know you can't just set the OfficerDropDownListUpdate.SelectedIndex, it has to be found in the Gridview control list.
0
 

Author Comment

by:PTBNA
ID: 17853616
Well, in case anyone's looking, I did a test, putting a label just below the Drop down list, and "Eval'd" the AdminInitials.  

And as expected they are there.  So, why with the Dropdown bound to a sql data source, and the Selected text set to the item in the grid, don't we get the Dropdownlist selected Item set???

0
 

Author Comment

by:PTBNA
ID: 17853667
Found the solution.

Cannot set the SelectedText value in the drop down.  Must use the SelectedValue.  Changing the statement, now the dropdownlist shows the selected item when we are in edit mode.

thanks for the help
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 17854794
Question closed - 500 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

770 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