Solved

Can I bind a drop down list selectedvalue in C# code behind?

Posted on 2009-06-30
3
1,566 Views
Last Modified: 2013-12-17
I have written the attached code within the onitemupdating event of my asp.net 3.5 listview control. I am attempting to update an item in my backend database which is bound to a drop down list control in the edititemtemplate of my frontend listview control but by using C#. Currently no errors are flagged but my update is now occurring? My e.NewValues reference doesn't seem to be registering although I can see that the correct value seems to be getting attached in debug?

Thanks.
Frontend drop down list control within my listviews edititemtemplate:
 
<asp:DropDownList 
    ID="InterviewerEditDDL" 
    runat="server" 
    DataSourceID="InterviewerEditDataSource" 
    DataTextField="InterviewerName" 
    DataValueField="InterviewerID" 
    CssClass="ddlTxtBoxInterviewer">
</asp:DropDownList>
<asp:LinqDataSource 
    ID="InterviewerEditDataSource" 
    ContextTypeName="HRsystemDataContext" 
    TableName="ApplicantInterviewers" 
    runat="server" 
    onselecting="InterviewerEditDataSource_Selecting" />
 
OnItemUpdating event of my listview (lvInterviewers):
 
protected void lvInterviewers_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.Items[e.ItemIndex].FindControl("InterviewerEditDDL");
 
        e.OldValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
 
        if (ddlEditInterviewerID.SelectedValue != "20")
        {
            e.NewValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
        }
    }

Open in new window

0
Comment
Question by:Shepwedd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:nikege
ID: 24749249
Actually I don't understand how are you binding DropDownList's SelectedValue.
try this:
<asp:DropDownList 
    ID="InterviewerEditDDL" 
    runat="server" 
    DataSourceID="InterviewerEditDataSource" 
    DataTextField="InterviewerName" 
    DataValueField="InterviewerID" 
    CssClass="ddlTxtBoxInterviewer"
    SelectedValue='<%# Eval("InterviewID")%>'>
</asp:DropDownList>

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24752551
nikege,

When I add:

SelectedValue='<%# Eval("InterviewerID")%>'

I get the error: "HttpException was unhandled by user code"? That's why I am attempting to bind my drop down lists selectedvalue in code behind (onitemupdating event) but my update doesn't seem to occur?

Thanks.
0
 
LVL 2

Accepted Solution

by:
nikege earned 500 total points
ID: 24752797
I am surprised that you got an error when you bind it using declarative syntax. If you can provide stack trace I will be able to answer why it happens.
But about binding from codebehind - Sure you can bind drop down from the codebehind. You should do it in ItemDataBound or DataBound events of ListView. Look at the code bellow. It shows how you can use ItemDataBound event.
Also look at this article http://msdn.microsoft.com/en-us/library/ms178366.aspx. In the end of article you can find 'Using Binding for a Lookup Table ' section.
<%@ Page language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
  protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      // Display the e-mail address in italics.
      Label EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
      EmailAddressLabel.Font.Italic = true;
    }
  }
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
  </head>
  <body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">
 
      <h3>ListView ItemDataBound Example</h3>
 
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        ConvertEmptyStringToNull="true"        
        OnItemDataBound="ContactsListView_ItemDataBound"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr style="background-color: #ADD8E6" runat="server">
                <th runat="server">First Name</th>
                <th runat="server">Last Name</th>
                <th runat="server">E-mail Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr style="background-color: #CAEEFF" runat="server">
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
 
      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT FirstName, LastName, EmailAddress FROM Person.Contact">
      </asp:SqlDataSource>
 
    </form>
  </body>
</html>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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