Dropdownlist datasource dependency

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Jan 13, 2009.

  1. Hi,

    I have a gridview that has 2 dropdownlists.
    DropDownList 4 is bound to AccessDataSource4.
    DropDownList2 needs to be bound to a DataSourceID depending on the value of
    DropDownList4.

    For example, in insert mode, if the user selects value "Mailboxes" in
    DropDownList4 then I want DropDownList2 to be bound to AccessDataSource2, if
    not then AccessDataSource3.

    I don't get any error, however the values in the dropdownlist in
    DetailsView1 are not correct.

    My code is as follows:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    Title="Admin-Manage Web Account-Mailboxes" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    DetailsView1.Visible = true;
    }

    protected void DetailsView1_ItemInserted(object sender,
    DetailsViewInsertedEventArgs e)
    {
    GridView1.DataBind();
    }


    protected void DetailsView1_Prerender(object sender, EventArgs e)
    {
    DetailsView dv = (DetailsView)sender;

    DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;


    if (DL4.Text == "Mailboxes")
    {
    DL2.DataSourceID = "AccessDataSource2";
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    dv.DataBind();
    }
    else
    {
    DL2.DataSourceID = "AccessDataSource3";
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    dv.DataBind();
    }
    }

    protected void DropDownList4_TableTypeSelectedIndexChanged(object
    sender, EventArgs e)
    {
    DetailsView dv = (DetailsView)sender;

    DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;


    if (DL4.Text.ToString() == "Mailboxes")
    {
    DL2.DataSourceID = "AccessDataSource2";
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    dv.DataBind();
    }
    else
    {
    DL2.DataSourceID = "AccessDataSource3";
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    dv.DataBind();
    }
    }

    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <br />
    <span style="color: navy; font-family: Verdana"><strong>Mailboxes for
    Web Account </strong></span>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="12pt"
    Font-Bold="True" ForeColor="Navy"></asp:Label><br />
    <br />
    <asp:Menu ID="Menu2" runat="server" BackColor="#FFC080" BorderColor="Navy"
    BorderStyle="Inset" BorderWidth="1px" Font-Bold="False"
    Font-Strikeout="False"
    Font-Underline="False" ForeColor="Navy" Orientation="Horizontal">
    <StaticMenuStyle HorizontalPadding="5px" />
    <StaticMenuItemStyle BackColor="#FFC080" Font-Names="verdana"
    Font-Size="8pt"
    HorizontalPadding="15px" />
    <StaticHoverStyle Font-Bold="True" />
    <Items>
    <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Message Center"
    Value="Message Center" ToolTip="Review mailboxes for logged in web account">
    </asp:MenuItem>
    <asp:MenuItem
    NavigateUrl="~/systemadminOnly/ManageCALLMaster.aspx" Text="Manage CALLMaster"
    Value="Manage CALLMaster" ToolTip="Manage CALLMaster
    databases"></asp:MenuItem>
    <asp:MenuItem
    NavigateUrl="~/systemadminOnly/ManageWebAccounts.aspx" Text="Manage Web
    Accounts"
    Value="Manage Web Accounts" ToolTip="Manage Web
    Accounts"></asp:MenuItem>
    <asp:MenuItem
    NavigateUrl="http://localhost/aspnetadmin/default.aspx?applicationPhysicalPath=C:\Inetpub\wwwroot\CMWebManager\&applicationUrl=/TestAspnetConfig"
    Text="Manage Roles/Web Account via ASP.Net" Value="Manage
    via ASP.Net" ToolTip="Must be locally connected to web server"></asp:MenuItem>
    </Items>
    </asp:Menu>
    <!--</strong></span>-->
    <br />
    <span style="font-size: 10pt; font-family: Verdana; color:
    navy;">Mailboxes</span>
    <!--<asp:Label ID="Label10" runat="server" Text='<%#
    Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="10pt"
    Font-Bold="True" ForeColor="Navy"></asp:Label><br />-->
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AllowSorting="True"
    AutoGenerateColumns="False"
    DataKeyNames="WebAccountName,WebAccountID" DataSourceID="AccessDataSource1"
    Font-Names="Verdana" Font-Size="8pt" BorderColor="#FFC080"
    BorderStyle="Solid" BorderWidth="1px" CellPadding="5" CellSpacing="1"
    ForeColor="Navy" ToolTip="Are you sure you want to delete this Mailbox for
    this Web Account?">
    <Columns>
    <asp:TemplateField AccessibleHeaderText="Will Delete"
    ShowHeader="False">
    <ControlStyle ForeColor="DarkSlateGray" />
    <ItemStyle BackColor="Lavender" BorderColor="SlateGray"
    BorderStyle="Inset" BorderWidth="2px" />
    <HeaderStyle BackColor="Lavender" />
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton2" runat="server"
    CommandName="Delete" OnClientClick="return confirm('Are you sure you want to
    delete this Mailbox from this Web Account?');">Delete</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="TableType"
    SortExpression="TableType">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList3" runat="server"
    AutoPostBack="True"
    DataSourceID="AccessDataSource4"
    DataTextField="TableType"
    DataValueField="TableType" SelectedValue='<%#
    Bind("TableType") %>' >
    </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Type ID" SortExpression="TypeID">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"
    SelectedValue='<%# Bind("TypeID") %>'
    Width="109px">
    </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TypeID") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="WebAccountID"
    SortExpression="WebAccountID">
    <EditItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("WebAccountID") %>'></asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("WebAccountID") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Width="0px" />
    <HeaderStyle Width="0px" />
    </asp:TemplateField>
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName"
    SortExpression="WebAccountName" InsertVisible="False"
    Visible="False" />
    </Columns>
    <EmptyDataTemplate>
    There are no mailboxes associated with this user.
    </EmptyDataTemplate>
    <RowStyle BackColor="BlanchedAlmond" />
    <HeaderStyle BackColor="#FFC080" BorderColor="#FFC080" />
    <AlternatingRowStyle BackColor="SeaShell" />
    <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
    </asp:GridView>
    <br />
    <br />
    <br />
    <asp:LinkButton ID="LinkButton1" runat="server"
    OnClick="LinkButton1_Click" Font-Names="verdana" Font-Size="8pt"
    BackColor="Lavender" BorderColor="SlateGray" BorderStyle="Inset"
    BorderWidth="1px" ForeColor="DarkSlateGray">Add Mailboxes</asp:LinkButton><br
    />
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataSourceID="AccessDataSource1"
    DefaultMode="Insert"
    Height="50px" Visible="False" Width="276px" Font-Names="Verdana"
    Font-Size="8pt" OnPreRender="DetailsView1_Prerender"
    BorderStyle="Solid" BorderWidth="1px" ForeColor="Navy"
    BorderColor="#FFC080" ToolTip="Correct Mailbox Selected?">
    <Fields>
    <asp:BoundField DataField="WebAccountID"
    HeaderText="WebAccountID"
    InsertVisible="False" SortExpression="WebAccountID" />
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName" InsertVisible="False"
    SortExpression="WebAccountName" />
    <asp:TemplateField HeaderText="TableType"
    SortExpression="TableType">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server"
    DataSourceID="AccessDataSource4"
    DataTextField="TableType"
    DataValueField="TableType" SelectedValue='<%#
    Bind("TableType") %>'
    OnSelectedIndexChanged="DropDownList4_TableTypeSelectedIndexChanged">
    </asp:DropDownList>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="TypeID" SortExpression="TypeID">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%#
    Bind("BoxNumber") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server"
    DataSourceID="AccessDataSource2"
    DataTextField="BoxNumber"
    DataValueField="BoxNumber" SelectedValue='<%#
    Bind("BoxNumber") %>'
    Width="150px">
    </asp:DropDownList>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("TypeID") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField InsertVisible="False" ShowInsertButton="True" >
    <ControlStyle Font-Names="Verdana" />
    </asp:CommandField>
    </Fields>
    <CommandRowStyle BackColor="Lavender" BorderColor="SlateGray"
    BorderStyle="Inset"
    BorderWidth="2px" ForeColor="DarkSlateGray" />
    <HeaderStyle BackColor="#FFC080" BorderColor="#FFC080" />
    <FieldHeaderStyle BackColor="#FFC080" />
    <InsertRowStyle Width="100px" />
    </asp:DetailsView>
    <br />
    <asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="<%$ ConnectionStrings:CALLMasterMDB %>"

    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = ?) ORDER BY
    [WebAccountName], [TableType], [TypeID]"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] = ?"
    InsertCommand="INSERT INTO [WebAccount] ([WebAccountName], [TypeID],
    [TableType]) VALUES (?, ?, ?)"
    UpdateCommand="UPDATE WebAccount SET TypeID = ?, TableType = ? WHERE
    (WebAccountID = ?)">
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"

    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    @WebAccountID" InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = @WebAccountName) ORDER
    BY [WebAccountName], [TableType], [TypeID]"

    UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    @TableType WHERE (WebAccountID = @WebAccountID)">
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    <asp:parameter Name="TableType" />
    </UpdateParameters>
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource2" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT BoxNumber FROM Mailboxes ORDER BY BoxNumber">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT [BoxNumber] FROM [Mailboxes] ORDER BY
    [BoxNumber]">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource3" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"

    SelectCommand="SELECT AttendantID FROM Attendant GROUP BY
    AttendantID ORDER BY AttendantID">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"

    SelectCommand="SELECT AttendantID FROM Attendant GROUP BY
    AttendantID ORDER BY AttendantID">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource4" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"


    SelectCommand="SELECT TableType FROM WebAccountTableType ORDER BY
    TableType">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"


    SelectCommand="SELECT TableType FROM WebAccountTableType ORDER BY
    TableType">
    </asp:SqlDataSource>
    <asp:HyperLink ID="HyperLink2" runat="server" Font-Names="Verdana"
    Font-Size="8pt"
    NavigateUrl="~/systemadminOnly/Registration.aspx" ToolTip="Quick Add
    Users for Website Access"
    Width="131px">Add Web Account</asp:HyperLink>
    <br />
    <br />
    <asp:HyperLink ID="HyperLink7" runat="server"
    Font-Names="Verdana" Font-Size="8pt"
    NavigateUrl="~/Default.aspx" ToolTip="Review Account Mailboxes, Messages,
    Notifications"
    Width="145px">Message Center</asp:HyperLink><asp:HyperLink
    ID="HyperLink6" runat="server"
    Font-Names="Verdana" Font-Size="8pt"
    NavigateUrl="~/systemadminOnly/ManageCALLMaster.aspx"
    ToolTip="Maintain CALLMaster Database" Visible='<%#
    User.IsInRole("systemadmin") %>'
    Width="156px">Manage CALLMaster</asp:HyperLink>

    <asp:HyperLink ID="HyperLink9" runat="server"
    Font-Names="Verdana" Font-Size="8pt"
    NavigateUrl="~/SystemAdminOnly/ManageWebAccounts.aspx"
    ToolTip="Manage Web Accounts" Visible='<%#
    User.IsInRole("systemadmin") %>'
    Width="156px">Manage Web Accounts</asp:HyperLink>

    <asp:HyperLink ID="HyperLink8" runat="server" Font-Names="Verdana"
    Font-Size="8pt"

    NavigateUrl="http://localhost/aspnetadmin/default.aspx?applicationPhysicalPath=C:\Inetpub\wwwroot\CMWebManager\&applicationUrl=/TestAspnetConfig"
    ToolTip="From Local Machine ONLY" Width="242px">Manage Roles/Web
    Account via ASP.Net</asp:HyperLink><br />
    <br />
    </asp:Content>


    --
    Thanks
    Morris
     
    Morris Neuman, Jan 13, 2009
    #1
    1. Advertising

  2. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    From your description, you use DetailsView to do some record inserting in
    ASP.NET application. And the DetailsView InsertTemplate contains two
    dropdownlists. You want to make one of the dropdownlist populate items
    depending on another dropdownlist's selected value(and bound data from a
    selected datasource control), correct?

    According to the page markup and code you provided, I think you have hooked
    up too much functions. Based on my test, I think what you need to do is the
    following two steps:

    1. Make sure that the DDL2 (which will bind data depending on DDL4) has a
    default datasource and data/text field assigned. Also, DDL4 has to set a
    default selected item(that matches DDL2's default databind setting).
    Thus, when the page first load or after you've inserted a record, the DDL4
    and DDL2 will return to the default status.

    2. You need to register DDL4's "SelectedIndexChanged" event and rebind DDL2
    at that time(according to DDL4's current selected value).


    Since your page is a bit too big(with other controls and style stuffs),
    I've created a simplified test page here, you can refer to it and modify
    your page according to it:


    =========aspx ==============
    <%@ Page Language="C#" AutoEventWireup="true"
    CodeBehind="DropDownTestPage.aspx.cs"
    Inherits="WebApplication1.DropDownTestPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>


    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:testdbConnectionString
    %>"
    DeleteCommand="DELETE FROM [table1] WHERE [t1_id] = @t1_id"
    InsertCommand="INSERT INTO [table1] ([t1_name]) VALUES
    (@t1_name)"
    SelectCommand="SELECT [t1_id], [t1_name] FROM [table1]"
    UpdateCommand="UPDATE [table1] SET [t1_name] = @t1_name WHERE
    [t1_id] = @t1_id">
    <DeleteParameters>
    <asp:parameter Name="t1_id" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="t1_name" Type="String" />
    <asp:parameter Name="t1_id" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="t1_name" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>

    </div>
    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateRows="False"
    DataKeyNames="t1_id" DataSourceID="SqlDataSource1"
    DefaultMode="Insert"
    Height="50px" Width="125px" OnLoad="DetailsView1_Load">

    <Fields>
    <asp:BoundField DataField="t1_id" HeaderText="t1_id"
    InsertVisible="False"
    ReadOnly="True" SortExpression="t1_id" />
    <asp:BoundField DataField="t1_name" HeaderText="t1_name"
    SortExpression="t1_name" />
    <asp:TemplateField HeaderText="Template Field" >
    <InsertItemTemplate>

    Table Type:<asp:dropdownlist ID="DDL4" runat="server"
    AutoPostBack="True"
    onselectedindexchanged="DDL4_SelectedIndexChanged">
    <asp:ListItem Selected="True" >Table1</asp:ListItem>
    <asp:ListItem>Table2</asp:ListItem>
    </asp:dropdownlist>

    <br />
    Items:<asp:dropdownlist ID="DDL2" runat="server"
    DataTextField="message"
    DataValueField="message"
    DataSourceID="SqlDataSource2"></asp:dropdownlist>

    </InsertItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowInsertButton="True" />
    </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:logdbConnectionString %>"
    SelectCommand="SELECT [message] FROM [tb_logs]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$ ConnectionStrings:logdbConnectionString %>"
    SelectCommand="SELECT [id] FROM [tb_logs]"></asp:SqlDataSource>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
    </body>
    </html>




    ===========code behind===================
    public partial class DropDownTestPage : System.Web.UI.Page
    {



    protected void DDL4_SelectedIndexChanged(object sender, EventArgs e)
    {
    DropDownList ddl2 = DetailsView1.FindControl("DDL2") as
    DropDownList;
    DropDownList ddl4 = DetailsView1.FindControl("DDL4") as
    DropDownList;

    string DDL4_VALUE = ddl4.SelectedValue;

    if (DDL4_VALUE == "Table1")
    {
    ddl2.DataSourceID = "SqlDataSource2";
    ddl2.DataTextField = ddl2.DataValueField = "message";
    }
    else
    {
    ddl2.DataSourceID = "SqlDataSource3";
    ddl2.DataTextField = ddl2.DataValueField = "id";
    }

    ddl2.DataBind();
    }

    protected void DetailsView1_Load(object sender, EventArgs e)
    {


    }

    }

    ==================================

    If there is anything unclear on this, please feel free to let me know.


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.





    --------------------

    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >Subject: Dropdownlist datasource dependency
    >Date: Mon, 12 Jan 2009 21:33:01 -0800


    >Hi,
    >
    >I have a gridview that has 2 dropdownlists.
    >DropDownList 4 is bound to AccessDataSource4.
    >DropDownList2 needs to be bound to a DataSourceID depending on the value

    of
    >DropDownList4.
    >
    >For example, in insert mode, if the user selects value "Mailboxes" in
    >DropDownList4 then I want DropDownList2 to be bound to AccessDataSource2,

    if
    >not then AccessDataSource3.
    >
    >I don't get any error, however the values in the dropdownlist in
    >DetailsView1 are not correct.
    >
    >My code is as follows:
    >
    ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    >Title="Admin-Manage Web Account-Mailboxes" %>
    >
    ><script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Label1.DataBind();
    > }
    >
    > protected void LinkButton1_Click(object sender, EventArgs e)
    > {
    > DetailsView1.Visible = true;
    > }
    >
    > protected void DetailsView1_ItemInserted(object sender,
    >DetailsViewInsertedEventArgs e)
    > {
    > GridView1.DataBind();
    > }
    >
    >
    > protected void DetailsView1_Prerender(object sender, EventArgs e)
    > {
    > DetailsView dv = (DetailsView)sender;
    >
    > DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    > DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;
    >
    >
    > if (DL4.Text == "Mailboxes")
    > {
    > DL2.DataSourceID = "AccessDataSource2";
    > DL2.DataTextField = "BoxNumber";
    > DL2.DataValueField = "Boxnumber";
    > dv.DataBind();
    > }
    > else
    > {
    > DL2.DataSourceID = "AccessDataSource3";
    > DL2.DataTextField = "AttendantID";
    > DL2.DataValueField = "AttendantID";
    > dv.DataBind();
    > }
    > }
    >
    > protected void DropDownList4_TableTypeSelectedIndexChanged(object
    >sender, EventArgs e)
    > {
    > DetailsView dv = (DetailsView)sender;
    >
    > DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    > DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;
    >
    >
    > if (DL4.Text.ToString() == "Mailboxes")
    > {
    > DL2.DataSourceID = "AccessDataSource2";
    > DL2.DataTextField = "BoxNumber";
    > DL2.DataValueField = "Boxnumber";
    > dv.DataBind();
    > }
    > else
    > {
    > DL2.DataSourceID = "AccessDataSource3";
    > DL2.DataTextField = "AttendantID";
    > DL2.DataValueField = "AttendantID";
    > dv.DataBind();
    > }
    > }
    >
    ></script>
    >
    ><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    >Runat="Server">
    > <br />
    > <span style="color: navy; font-family: Verdana"><strong>Mailboxes for
    >Web Account </strong></span>
    > <asp:Label ID="Label1" runat="server" Text='<%#
    >Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="12pt"
    >Font-Bold="True" ForeColor="Navy"></asp:Label><br />
    > <br />
    > <asp:Menu ID="Menu2" runat="server" BackColor="#FFC080"

    BorderColor="Navy"
    > BorderStyle="Inset" BorderWidth="1px" Font-Bold="False"
    >Font-Strikeout="False"
    > Font-Underline="False" ForeColor="Navy" Orientation="Horizontal">
    > <StaticMenuStyle HorizontalPadding="5px" />
    > <StaticMenuItemStyle BackColor="#FFC080" Font-Names="verdana"
    >Font-Size="8pt"
    > HorizontalPadding="15px" />
    > <StaticHoverStyle Font-Bold="True" />
    > <Items>
    > <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Message

    Center"
    >Value="Message Center" ToolTip="Review mailboxes for logged in web

    account">
    > </asp:MenuItem>
    > <asp:MenuItem
    >NavigateUrl="~/systemadminOnly/ManageCALLMaster.aspx" Text="Manage

    CALLMaster"
    > Value="Manage CALLMaster" ToolTip="Manage CALLMaster
    >databases"></asp:MenuItem>
    > <asp:MenuItem
    >NavigateUrl="~/systemadminOnly/ManageWebAccounts.aspx" Text="Manage Web
    >Accounts"
    > Value="Manage Web Accounts" ToolTip="Manage Web
    >Accounts"></asp:MenuItem>
    > <asp:MenuItem
    >NavigateUrl="http://localhost/aspnetadmin/default.aspx?applicationPhysicalP

    ath=C:\Inetpub\wwwroot\CMWebManager\&applicationUrl=/TestAspnetConfig"
    > Text="Manage Roles/Web Account via ASP.Net" Value="Manage
    >via ASP.Net" ToolTip="Must be locally connected to web

    server"></asp:MenuItem>
    > </Items>
    > </asp:Menu>
    > <!--</strong></span>-->
    > <br />
    > <span style="font-size: 10pt; font-family: Verdana; color:
    >navy;">Mailboxes</span>
    > <!--<asp:Label ID="Label10" runat="server" Text='<%#
    >Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="10pt"
    >Font-Bold="True" ForeColor="Navy"></asp:Label><br />-->
    > <br />
    > <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    >AllowSorting="True"
    > AutoGenerateColumns="False"
    >DataKeyNames="WebAccountName,WebAccountID" DataSourceID="AccessDataSource1"
    > Font-Names="Verdana" Font-Size="8pt" BorderColor="#FFC080"
    >BorderStyle="Solid" BorderWidth="1px" CellPadding="5" CellSpacing="1"
    >ForeColor="Navy" ToolTip="Are you sure you want to delete this Mailbox for
    >this Web Account?">
    > <Columns>
    >
     
    Steven Cheng, Jan 13, 2009
    #2
    1. Advertising

  3. Hi,

    Thanks for the help. I tried to apply your sample code to my web page and
    get error:
    Exception Details: System.InvalidOperationException: Databinding methods
    such as Eval(), XPath(), and Bind() can only be used in the context of a
    databound control.

    The page loads and I can click on LinkButton1 to view the DetailsView1. I
    see "Attendant" value in DropDownList4 per the default set in the
    DetailsView1_Prerender. DropDownList2 is bound to AccessDataSource3 so it is
    in sync with the "Attendant" value in DropDownList4.

    I get this error when I use DropDownList4 and select value "Mailboxes". In
    the debugging I see that the error occurs when the programs executes the
    DL2.DataBind() statement in the DropDownList4_TableTypeSelectedIndexChanged
    script section.

    My abbreviated code is as follows:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    Title="Admin-Manage Web Account-Mailboxes" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    DetailsView1.Visible = true;

    }



    protected void DetailsView1_Prerender(object sender, EventArgs e)
    {
    DetailsView dv = (DetailsView)sender;

    DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;
    String TableType_Default = "Attendant" as string;
    DL4.Text = TableType_Default;
    }

    protected void DetailsView1_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList4_TableTypeSelectedIndexChanged(object
    sender, EventArgs e)
    {

    DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    DropDownList;
    DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    DropDownList;

    string DL4_Value = DL4.SelectedValue;

    if (DL4_Value == "Mailboxes")
    {
    DL2.DataSourceID = "AccessDataSource2";
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    }
    else
    {
    DL2.DataSourceID = "AccessDataSource3";
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    }

    DL2.DataBind();
    }

    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <br />
    <span style="color: navy; font-family: Verdana"><strong>Table Types for
    Web Account </strong></span>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="12pt"
    Font-Bold="True" ForeColor="Navy"></asp:Label><br />
    <br />

    <!--</strong></span>-->
    <br />


    <br />
    <br />
    <asp:LinkButton ID="LinkButton1" runat="server"
    OnClick="LinkButton1_Click" Font-Names="verdana" Font-Size="8pt"
    BackColor="Lavender" BorderColor="SlateGray" BorderStyle="Inset"
    BorderWidth="1px" ForeColor="DarkSlateGray">Add Table
    Types</asp:LinkButton><br />
    <br />

    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataSourceID="AccessDataSource1"
    DefaultMode="Insert"
    Height="50px" Visible="False" Width="276px" Font-Names="Verdana"
    Font-Size="8pt" OnPreRender="DetailsView1_Prerender"
    BorderStyle="Solid" BorderWidth="1px" ForeColor="Navy"
    BorderColor="#FFC080" ToolTip="Correct Mailbox Selected?"
    DataKeyNames="WebAccountName,WebAccountID">
    <Fields>
    <asp:BoundField DataField="WebAccountID"
    HeaderText="WebAccountID"
    InsertVisible="False" SortExpression="WebAccountID" />
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName" InsertVisible="False"
    SortExpression="WebAccountName" />
    <asp:TemplateField HeaderText="TableType"
    SortExpression="TableType">
    <InsertItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server"
    DataSourceID="AccessDataSource4"
    DataTextField="WTableType" AutoPostBack="True"
    DataValueField="WTableType" SelectedValue='<%#
    Bind("TableType") %>'

    OnSelectedIndexChanged="DropDownList4_TableTypeSelectedIndexChanged">
    </asp:DropDownList>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="TypeID" SortExpression="TypeID">
    <InsertItemTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server"
    DataSourceID="AccessDataSource3"
    DataTextField="AttendantID"
    DataValueField="AttendantID" SelectedValue='<%#
    Bind("TypeID") %>'
    Width="150px">
    </asp:DropDownList>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("TypeID") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField InsertVisible="False" ShowInsertButton="True" >
    <ControlStyle Font-Names="Verdana" />
    </asp:CommandField>
    </Fields>
    <CommandRowStyle BackColor="Lavender" BorderColor="SlateGray"
    BorderStyle="Inset"
    BorderWidth="2px" ForeColor="DarkSlateGray" />
    <HeaderStyle BackColor="#FFC080" BorderColor="#FFC080" />
    <FieldHeaderStyle BackColor="#FFC080" />
    <InsertRowStyle Width="100px" />
    </asp:DetailsView>
    <br />

    <asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="<%$ ConnectionStrings:CALLMasterMDB %>"
    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = ?) ORDER BY
    [WebAccountName], [TableType], [TypeID]"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] = ?"
    InsertCommand="INSERT INTO [WebAccount] ([WebAccountName], [TypeID],
    [TableType]) VALUES (?, ?, ?)"
    UpdateCommand="UPDATE WebAccount SET TypeID = ?, TableType = ? WHERE
    (WebAccountID = ?)">
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:AccessDataSource>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    @WebAccountID" InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = @WebAccountName) ORDER
    BY [WebAccountName], [TableType], [TypeID]"
    UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    @TableType WHERE (WebAccountID = @WebAccountID)">
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    <asp:parameter Name="TableType" />
    </UpdateParameters>
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:SqlDataSource>

    <asp:AccessDataSource ID="AccessDataSource2" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT BoxNumber FROM Mailboxes ORDER BY BoxNumber">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT [BoxNumber] FROM [Mailboxes] ORDER BY
    [BoxNumber]">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource3" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT AttendantID FROM Attendant GROUP BY
    AttendantID ORDER BY AttendantID">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT AttendantID FROM Attendant GROUP BY
    AttendantID ORDER BY AttendantID">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource4" runat="server"
    DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT WTableType FROM WebAccountTableType ORDER BY
    WTableType">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT TableType FROM WebAccountTableType ORDER BY
    TableType">
    </asp:SqlDataSource>
    <br />
    </asp:Content>


    I hope you will point me in the correct direction.

    --
    Thanks
    Morris


    ""Steven Cheng"" wrote:

    > Hi Morris,
    >
    > From your description, you use DetailsView to do some record inserting in
    > ASP.NET application. And the DetailsView InsertTemplate contains two
    > dropdownlists. You want to make one of the dropdownlist populate items
    > depending on another dropdownlist's selected value(and bound data from a
    > selected datasource control), correct?
    >
    > According to the page markup and code you provided, I think you have hooked
    > up too much functions. Based on my test, I think what you need to do is the
    > following two steps:
    >
    > 1. Make sure that the DDL2 (which will bind data depending on DDL4) has a
    > default datasource and data/text field assigned. Also, DDL4 has to set a
    > default selected item(that matches DDL2's default databind setting).
    > Thus, when the page first load or after you've inserted a record, the DDL4
    > and DDL2 will return to the default status.
    >
    > 2. You need to register DDL4's "SelectedIndexChanged" event and rebind DDL2
    > at that time(according to DDL4's current selected value).
    >
    >
    > Since your page is a bit too big(with other controls and style stuffs),
    > I've created a simplified test page here, you can refer to it and modify
    > your page according to it:
    >
    >
    > =========aspx ==============
    > <%@ Page Language="C#" AutoEventWireup="true"
    > CodeBehind="DropDownTestPage.aspx.cs"
    > Inherits="WebApplication1.DropDownTestPage" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title></title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    >
    >
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:testdbConnectionString
    > %>"
    > DeleteCommand="DELETE FROM [table1] WHERE [t1_id] = @t1_id"
    > InsertCommand="INSERT INTO [table1] ([t1_name]) VALUES
    > (@t1_name)"
    > SelectCommand="SELECT [t1_id], [t1_name] FROM [table1]"
    > UpdateCommand="UPDATE [table1] SET [t1_name] = @t1_name WHERE
    > [t1_id] = @t1_id">
    > <DeleteParameters>
    > <asp:parameter Name="t1_id" Type="Int32" />
    > </DeleteParameters>
    > <UpdateParameters>
    > <asp:parameter Name="t1_name" Type="String" />
    > <asp:parameter Name="t1_id" Type="Int32" />
    > </UpdateParameters>
    > <InsertParameters>
    > <asp:parameter Name="t1_name" Type="String" />
    > </InsertParameters>
    > </asp:SqlDataSource>
    >
    > </div>
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AutoGenerateRows="False"
    > DataKeyNames="t1_id" DataSourceID="SqlDataSource1"
    > DefaultMode="Insert"
    > Height="50px" Width="125px" OnLoad="DetailsView1_Load">
    >
    > <Fields>
    > <asp:BoundField DataField="t1_id" HeaderText="t1_id"
    > InsertVisible="False"
    > ReadOnly="True" SortExpression="t1_id" />
    > <asp:BoundField DataField="t1_name" HeaderText="t1_name"
    > SortExpression="t1_name" />
    > <asp:TemplateField HeaderText="Template Field" >
    > <InsertItemTemplate>
    >
    > Table Type:<asp:dropdownlist ID="DDL4" runat="server"
    > AutoPostBack="True"
    > onselectedindexchanged="DDL4_SelectedIndexChanged">
    > <asp:ListItem Selected="True" >Table1</asp:ListItem>
    > <asp:ListItem>Table2</asp:ListItem>
    > </asp:dropdownlist>
    >
    > <br />
    > Items:<asp:dropdownlist ID="DDL2" runat="server"
    > DataTextField="message"
    > DataValueField="message"
    > DataSourceID="SqlDataSource2"></asp:dropdownlist>
    >
    > </InsertItemTemplate>
    > </asp:TemplateField>
    > <asp:CommandField ShowInsertButton="True" />
    > </Fields>
    > </asp:DetailsView>
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:logdbConnectionString %>"
    > SelectCommand="SELECT [message] FROM [tb_logs]"></asp:SqlDataSource>
    > <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    > ConnectionString="<%$ ConnectionStrings:logdbConnectionString %>"
    > SelectCommand="SELECT [id] FROM [tb_logs]"></asp:SqlDataSource>
    > <br />
    > <asp:Button ID="Button1" runat="server" Text="Button" />
    > </form>
    > </body>
    > </html>
    >
    >
    >
    >
    > ===========code behind===================
    > public partial class DropDownTestPage : System.Web.UI.Page
    > {
    >
    >
    >
    > protected void DDL4_SelectedIndexChanged(object sender, EventArgs e)
    > {
    > DropDownList ddl2 = DetailsView1.FindControl("DDL2") as
    > DropDownList;
    > DropDownList ddl4 = DetailsView1.FindControl("DDL4") as
    > DropDownList;
    >
    > string DDL4_VALUE = ddl4.SelectedValue;
    >
    > if (DDL4_VALUE == "Table1")
    > {
    > ddl2.DataSourceID = "SqlDataSource2";
    > ddl2.DataTextField = ddl2.DataValueField = "message";
    > }
    > else
    > {
    > ddl2.DataSourceID = "SqlDataSource3";
    > ddl2.DataTextField = ddl2.DataValueField = "id";
    > }
    >
    > ddl2.DataBind();
    > }
    >
    > protected void DetailsView1_Load(object sender, EventArgs e)
    > {
    >
    >
    > }
    >
    > }
    >
    > ==================================
    >
    > If there is anything unclear on this, please feel free to let me know.
    >
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 2 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions. Issues of this
    > nature are best handled working with a dedicated Microsoft Support Engineer
    > by contacting Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
    > --------------------
    >
    > >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >Subject: Dropdownlist datasource dependency
    > >Date: Mon, 12 Jan 2009 21:33:01 -0800

    >
    > >Hi,
    > >
    > >I have a gridview that has 2 dropdownlists.
    > >DropDownList 4 is bound to AccessDataSource4.
    > >DropDownList2 needs to be bound to a DataSourceID depending on the value

    > of
    > >DropDownList4.
    > >
    > >For example, in insert mode, if the user selects value "Mailboxes" in
    > >DropDownList4 then I want DropDownList2 to be bound to AccessDataSource2,

    > if
    > >not then AccessDataSource3.
    > >
    > >I don't get any error, however the values in the dropdownlist in
    > >DetailsView1 are not correct.
    > >
    > >My code is as follows:
    > >
    > ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    > >Title="Admin-Manage Web Account-Mailboxes" %>
    > >
    > ><script runat="server">
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > Label1.DataBind();
    > > }
    > >
    > > protected void LinkButton1_Click(object sender, EventArgs e)
    > > {
    > > DetailsView1.Visible = true;
    > > }
    > >
    > > protected void DetailsView1_ItemInserted(object sender,
    > >DetailsViewInsertedEventArgs e)
    > > {
    > > GridView1.DataBind();
    > > }
    > >
    > >
    > > protected void DetailsView1_Prerender(object sender, EventArgs e)
    > > {
    > > DetailsView dv = (DetailsView)sender;
    > >
    > > DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    > > DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;
    > >
    > >
    > > if (DL4.Text == "Mailboxes")
    > > {
    > > DL2.DataSourceID = "AccessDataSource2";
    > > DL2.DataTextField = "BoxNumber";
    > > DL2.DataValueField = "Boxnumber";
    > > dv.DataBind();
    > > }
    > > else
    > > {
    > > DL2.DataSourceID = "AccessDataSource3";
    > > DL2.DataTextField = "AttendantID";
    > > DL2.DataValueField = "AttendantID";
    > > dv.DataBind();
    > > }
    > > }
    > >
    > > protected void DropDownList4_TableTypeSelectedIndexChanged(object
    > >sender, EventArgs e)
    > > {
    > > DetailsView dv = (DetailsView)sender;
    > >
    > > DropDownList DL2 = dv.FindControl("DropDownList2") as DropDownList;
    > > DropDownList DL4 = dv.FindControl("DropDownList4") as DropDownList;
    > >
    > >
    > > if (DL4.Text.ToString() == "Mailboxes")
    > > {
    > > DL2.DataSourceID = "AccessDataSource2";
    > > DL2.DataTextField = "BoxNumber";
    > > DL2.DataValueField = "Boxnumber";
    > > dv.DataBind();
    > > }
    > > else
    > > {
    > > DL2.DataSourceID = "AccessDataSource3";
    > > DL2.DataTextField = "AttendantID";
    > > DL2.DataValueField = "AttendantID";
    > > dv.DataBind();
    > > }
    > > }
    > >
    > ></script>
    > >
    > ><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > >Runat="Server">
    > > <br />
    > > <span style="color: navy; font-family: Verdana"><strong>Mailboxes for
    > >Web Account </strong></span>
    > > <asp:Label ID="Label1" runat="server" Text='<%#
    > >Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="12pt"
    > >Font-Bold="True" ForeColor="Navy"></asp:Label><br />
    > > <br />
    > > <asp:Menu ID="Menu2" runat="server" BackColor="#FFC080"

    > BorderColor="Navy"
    > > BorderStyle="Inset" BorderWidth="1px" Font-Bold="False"
    > >Font-Strikeout="False"
     
    Morris Neuman, Jan 14, 2009
    #3
  4. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    As for the new exception you encountered:

    ===================
    >Exception Details: System.InvalidOperationException: Databinding methods
    >such as Eval(), XPath(), and Bind() can only be used in the context of a
    >databound control.
    >==================


    it is due to the <%# Bind %> like databinding expression is only supported
    when you perform databinding on the DetailsView control. However, when you
    call dropdownlist.DataBind method(as you do it in Dropdownlist4's
    SelectedIndexChanged event), the <%# Bind %> expression is out of
    DetailsView's databinding context(not data is available for bind to it) and
    thus the exception got raised.

    after some research, I've found a very good web article mentioned this
    issue and provide some good examples on how to resolve this
    problem(actually the sample is exactly focus on how to implement such kind
    of cascading Dropdownlist association ):

    #Demo for 2-way databinding cascading lists within a FormView
    http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .




    --------------------
    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >References: <>

    <>
    >Subject: RE: Dropdownlist datasource dependency
    >Date: Tue, 13 Jan 2009 17:12:00 -0800


    >
    >Hi,
    >
    >Thanks for the help. I tried to apply your sample code to my web page and
    >get error:
    >Exception Details: System.InvalidOperationException: Databinding methods
    >such as Eval(), XPath(), and Bind() can only be used in the context of a
    >databound control.
    >
    >The page loads and I can click on LinkButton1 to view the DetailsView1. I
    >see "Attendant" value in DropDownList4 per the default set in the
    >DetailsView1_Prerender. DropDownList2 is bound to AccessDataSource3 so it

    is
    >in sync with the "Attendant" value in DropDownList4.
    >
    >I get this error when I use DropDownList4 and select value "Mailboxes".

    In
    >the debugging I see that the error occurs when the programs executes the
    >DL2.DataBind() statement in the

    DropDownList4_TableTypeSelectedIndexChanged
    >script section.
    >
    >My abbreviated code is as follows:
    >
    ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    >Title="Admin-Manage Web Account-Mailboxes" %>
    >
    ><script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Label1.DataBind();
    > }
    >
    > protected void LinkButton1_Click(object sender, EventArgs e)
    > {
    > DetailsView1.Visible = true;
    >
     
    Steven Cheng, Jan 15, 2009
    #4
  5. Thanks, I tried the demo per the link and find that my code keeps looping in
    the ddl2TypeID_DataBound script. This is invoked by the DropDownList2
    OnDataBound method per the demo.

    My code is as follows:


    <%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    Title="Admin-Manage Web Account-Mailboxes" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    DetailsView1.Visible = true;
    }

    protected void DetailsView1_ItemInserted(object sender,
    DetailsViewInsertedEventArgs e)
    {
    GridView1.DataBind();
    }
    protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    {
    //DropDownList ddl2 = (DropDownList)sender;
    //DropDownList ddl4 = (DropDownList)sender;

    DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    DropDownList;
    DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    DropDownList;

    string DL4_Value = DL4.SelectedValue;

    DL4_Value = "Mailboxes";

    if (DL4_Value == "Mailboxes")
    {
    DL2.DataSourceID = "AccessDataSource2";
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    //DL2.DataBind();
    //dv.DataBind();
    }
    else
    {
    DL2.DataSourceID = "AccessDataSource3";
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    //DL2.DataBind();
    //dv.DataBind();
    }

    if (DL2 != null)
    {
    DL2.DataBind();
    }

    Page.Error += new EventHandler(Page_Error);
    }

    void Page_Error(object sender, EventArgs e)
    {
    Response.Write("Error" + e.ToString());
    }


    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <br />
    <span style="color: navy; font-family: Verdana"><strong>Table Types for
    Web Account </strong></span>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Request.QueryString["User"] %>' Font-Names="Verdana" Font-Size="12pt"
    Font-Bold="True" ForeColor="Navy"></asp:Label><br />
    <br />

    <asp:LinkButton ID="LinkButton1" runat="server"
    OnClick="LinkButton1_Click" Font-Names="verdana" Font-Size="8pt"
    BackColor="Lavender" BorderColor="SlateGray" BorderStyle="Inset"
    BorderWidth="1px" ForeColor="DarkSlateGray">Add Table
    Types</asp:LinkButton><br />
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataSourceID="AccessDataSource1"
    DefaultMode="Insert" Visible="False" Width="276px"
    Font-Names="Verdana"
    Font-Size="8pt"
    BorderStyle="Solid" BorderWidth="1px" ForeColor="Navy"
    BorderColor="#FFC080" ToolTip="Correct Table Type Selected?"
    DataKeyNames="WebAccountName,WebAccountID">
    <Fields>
    <asp:BoundField DataField="WebAccountID"
    HeaderText="WebAccountID"
    InsertVisible="False" SortExpression="WebAccountID" />
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName" InsertVisible="False"
    SortExpression="WebAccountName" />
    <asp:TemplateField HeaderText="TableType & ID"
    SortExpression="TableType">
    <InsertItemTemplate>
    <table style="width: 100%">
    <tr>
    <td style="width: 76px">
    <asp:Label ID="Label5" runat="server"
    style="text-align: left"
    Text="Select Table Name"></asp:Label>
    </td>
    <td style="width: 131px">
    <asp:DropDownList ID="DropDownList4"
    runat="server" AutoPostBack="True"
    DataSourceID="AccessDataSource4"
    DataTextField="WTableType"
    DataValueField="WTableType"
    SelectedValue='<%# Bind("TableType") %>'
    Width="122px" >
    <asp:ListItem Selected
    ="True">Mailboxes</asp:ListItem>
    </asp:DropDownList>
    </td>
    <td>
    <asp:Label ID="Label4" runat="server"
    style="text-align: left"
    Text=" then select Record ID">
    </asp:Label>
    </td>
    <td>
    <asp:DropDownList ID="DropDownList2"
    runat="server"
    DataSourceID="AccessDataSource2"
    DataTextField="BoxNumber"
    DataValueField="BoxNumber"
    OnDataBound="ddl2TypeID_DataBound"
    SelectedValue='<%# Bind("TypeID") %>'
    Width="110px">
    </asp:DropDownList>
    </td>
    </tr>
    </table>

    <asp:AccessDataSource ID="AccessDataSource2"
    runat="server"
    DataFile="C:\Program
    Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT BoxNumber FROM Mailboxes ORDER
    BY BoxNumber">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT [BoxNumber] FROM [Mailboxes]
    ORDER BY [BoxNumber]">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource3"
    runat="server"
    DataFile="C:\Program
    Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT AttendantID FROM Attendant
    GROUP BY AttendantID ORDER BY AttendantID">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT AttendantID FROM Attendant
    GROUP BY AttendantID ORDER BY AttendantID">
    </asp:SqlDataSource>
    <asp:AccessDataSource ID="AccessDataSource4"
    runat="server"
    DataFile="C:\Program
    Files\CallMaster\Data\Callmaster.mdb"
    SelectCommand="SELECT WTableType FROM
    WebAccountTableType ORDER BY WTableType">
    </asp:AccessDataSource>
    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT TableType FROM
    WebAccountTableType ORDER BY TableType">
    </asp:SqlDataSource>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField InsertVisible="False" ShowInsertButton="True" >
    <ControlStyle Font-Names="Verdana" />
    </asp:CommandField>
    </Fields>
    <CommandRowStyle BackColor="Lavender" BorderColor="SlateGray"
    BorderStyle="Inset"
    BorderWidth="2px" ForeColor="DarkSlateGray" />
    <HeaderStyle BackColor="#FFC080" BorderColor="#FFC080" />
    <FieldHeaderStyle BackColor="#FFC080" />
    <InsertRowStyle Width="100px" />
    </asp:DetailsView>
    <br />
    <asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="<%$ ConnectionStrings:CALLMasterMDB %>"
    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = ?) ORDER BY
    [WebAccountName], [TableType], [TypeID]"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] = ?"
    InsertCommand="INSERT INTO [WebAccount] ([WebAccountName], [TypeID],
    [TableType]) VALUES (?, ?, ?)"
    UpdateCommand="UPDATE WebAccount SET TypeID = ?, TableType = ? WHERE
    (WebAccountID = ?)">
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:AccessDataSource>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    @WebAccountID" InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    SelectCommand="SELECT [WebAccountID], [WebAccountName], [TableType],
    [TypeID] FROM [WebAccount] WHERE ([WebAccountName] = @WebAccountName) ORDER
    BY [WebAccountName], [TableType], [TypeID]"
    UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    @TableType WHERE (WebAccountID = @WebAccountID)">
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    <asp:parameter Name="TableType" />
    </UpdateParameters>
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <InsertParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />
    </asp:Content>


    Hopefully you can tell me what I am doing wrong in invoking this method.
    --
    Thanks
    Morris


    ""Steven Cheng"" wrote:

    > Hi Morris,
    >
    > As for the new exception you encountered:
    >
    > ===================
    > >Exception Details: System.InvalidOperationException: Databinding methods
    > >such as Eval(), XPath(), and Bind() can only be used in the context of a
    > >databound control.
    > >==================

    >
    > it is due to the <%# Bind %> like databinding expression is only supported
    > when you perform databinding on the DetailsView control. However, when you
    > call dropdownlist.DataBind method(as you do it in Dropdownlist4's
    > SelectedIndexChanged event), the <%# Bind %> expression is out of
    > DetailsView's databinding context(not data is available for bind to it) and
    > thus the exception got raised.
    >
    > after some research, I've found a very good web article mentioned this
    > issue and provide some good examples on how to resolve this
    > problem(actually the sample is exactly focus on how to implement such kind
    > of cascading Dropdownlist association ):
    >
    > #Demo for 2-way databinding cascading lists within a FormView
    > http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    >
    >
    >
    > --------------------
    > >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >References: <>

    > <>
    > >Subject: RE: Dropdownlist datasource dependency
    > >Date: Tue, 13 Jan 2009 17:12:00 -0800

    >
    > >
    > >Hi,
    > >
    > >Thanks for the help. I tried to apply your sample code to my web page and
    > >get error:
    > >Exception Details: System.InvalidOperationException: Databinding methods
    > >such as Eval(), XPath(), and Bind() can only be used in the context of a
    > >databound control.
    > >
    > >The page loads and I can click on LinkButton1 to view the DetailsView1. I
    > >see "Attendant" value in DropDownList4 per the default set in the
    > >DetailsView1_Prerender. DropDownList2 is bound to AccessDataSource3 so it

    > is
    > >in sync with the "Attendant" value in DropDownList4.
    > >
    > >I get this error when I use DropDownList4 and select value "Mailboxes".

    > In
    > >the debugging I see that the error occurs when the programs executes the
    > >DL2.DataBind() statement in the

    > DropDownList4_TableTypeSelectedIndexChanged
    > >script section.
    > >
    > >My abbreviated code is as follows:
    > >
    > ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    > >Title="Admin-Manage Web Account-Mailboxes" %>
    > >
    > ><script runat="server">
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > Label1.DataBind();
    > > }
    > >
    > > protected void LinkButton1_Click(object sender, EventArgs e)
    > > {
    > > DetailsView1.Visible = true;
    > >

    >
    >
     
    Morris Neuman, Jan 16, 2009
    #5
  6. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    Based on the code you provided, the "infinite loop in your databind method"
    is cause by "you call DataBind method on Dropdownlist inside dropdownlist's
    own databound" event. This cause the databinding opeation happend
    recursivelyl(endless).

    This is the "DataBound" event handler in which you invoke dropdownlist's
    DataBind method.
    ===========
    protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    {
    ==================

    According to the article I gave you in previous message, you should
    manually use a for loop to add items into dropdownlist instead of using
    Databind. You can still use DataSource control, but not directly bind them
    to dropdownlist. You can manually call DatasourceControl.Select method to
    get the resultset and loop through the resultset and Add items into
    dropdownlist.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    --------------------
    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >Subject: RE: Dropdownlist datasource dependency
    >Date: Thu, 15 Jan 2009 16:04:01 -0800


    >
    >Thanks, I tried the demo per the link and find that my code keeps looping

    in
    >the ddl2TypeID_DataBound script. This is invoked by the DropDownList2
    >OnDataBound method per the demo.
    >
    >My code is as follows:
    >
    >
    ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    >Title="Admin-Manage Web Account-Mailboxes" %>
    ><%@ Import Namespace="System.Web.UI.WebControls" %>
    >
    ><script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Label1.DataBind();
    > }
    >
    > protected void LinkButton1_Click(object sender, EventArgs e)
    > {
    > DetailsView1.Visible = true;
    > }
    >
    > protected void DetailsView1_ItemInserted(object sender,
    >DetailsViewInsertedEventArgs e)
    > {
    > GridView1.DataBind();
    > }
    > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > {
    > //DropDownList ddl2 = (DropDownList)sender;
    > //DropDownList ddl4 = (DropDownList)sender;
    >
    > DropDownList DL2 = DetailsView1.FindControl("DropDownList2")

    as
    >DropDownList;
    > DropDownList DL4 = DetailsView1.FindControl("DropDownList4")

    as
    >DropDownList;
    >
    > string DL4_Value = DL4.SelectedValue;
    >
     
    Steven Cheng, Jan 19, 2009
    #6
  7. Hi,

    I removed the Bind (SelectedValue='<%# Bind("TypeID") %>') in the
    DropDownList2
    <asp:DropDownList ID="DropDownList2" runat="server"
    DataSourceID="AccessDataSource2"
    DataTextField="BoxNumber"
    DataValueField="BoxNumber"
    OnDataBound="ddl2TypeID_DataBound"
    Width="110px">
    </asp:DropDownList>

    DropDownList4 is defined as follows:
    <asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"
    DataSourceID="AccessDataSource4"
    DataTextField="WTableType"
    DataValueField="WTableType" SelectedValue='<%#
    Bind("TableType") %>'
    Width="122px" >
    <asp:ListItem Selected ="True">Mailboxes</asp:ListItem>
    </asp:DropDownList>


    My DetailsView1 is bound to a datasource and contains the 2 DropDownLists as
    Insert Templates.

    In the script ddl2TypeID_DataBound, I have the following:

    protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    {
    DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    DropDownList;
    DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    DropDownList;

    string DL4_Value = DL4.SelectedValue;

    if (DL4_Value == "Mailboxes")
    {
    DL2.DataSourceID = "AccessDataSource2";
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    }
    else
    {
    DL2.DataSourceID = "AccessDataSource3";
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    }

    if (DL2 != null)
    {
    DL2.DataBind();
    }

    Page.Error += new EventHandler(Page_Error);
    }

    void Page_Error(object sender, EventArgs e)
    {
    Response.Write("Error" + e.ToString());
    }

    Even if I comment out the DL2.DataBind(); statement in the script, it
    continues to loop. Sorry I am new at this and learning so things seem a bit
    difficult. I would appreciate if you can show by changing my code sample
    what I should do.
    --
    Thanks
    Morris


    ""Steven Cheng"" wrote:

    > Hi Morris,
    >
    > Based on the code you provided, the "infinite loop in your databind method"
    > is cause by "you call DataBind method on Dropdownlist inside dropdownlist's
    > own databound" event. This cause the databinding opeation happend
    > recursivelyl(endless).
    >
    > This is the "DataBound" event handler in which you invoke dropdownlist's
    > DataBind method.
    > ===========
    > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > {
    > ==================
    >
    > According to the article I gave you in previous message, you should
    > manually use a for loop to add items into dropdownlist instead of using
    > Databind. You can still use DataSource control, but not directly bind them
    > to dropdownlist. You can manually call DatasourceControl.Select method to
    > get the resultset and loop through the resultset and Add items into
    > dropdownlist.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > --------------------
    > >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >Subject: RE: Dropdownlist datasource dependency
    > >Date: Thu, 15 Jan 2009 16:04:01 -0800

    >
    > >
    > >Thanks, I tried the demo per the link and find that my code keeps looping

    > in
    > >the ddl2TypeID_DataBound script. This is invoked by the DropDownList2
    > >OnDataBound method per the demo.
    > >
    > >My code is as follows:
    > >
    > >
    > ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    > >Title="Admin-Manage Web Account-Mailboxes" %>
    > ><%@ Import Namespace="System.Web.UI.WebControls" %>
    > >
    > ><script runat="server">
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > Label1.DataBind();
    > > }
    > >
    > > protected void LinkButton1_Click(object sender, EventArgs e)
    > > {
    > > DetailsView1.Visible = true;
    > > }
    > >
    > > protected void DetailsView1_ItemInserted(object sender,
    > >DetailsViewInsertedEventArgs e)
    > > {
    > > GridView1.DataBind();
    > > }
    > > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > > {
    > > //DropDownList ddl2 = (DropDownList)sender;
    > > //DropDownList ddl4 = (DropDownList)sender;
    > >
    > > DropDownList DL2 = DetailsView1.FindControl("DropDownList2")

    > as
    > >DropDownList;
    > > DropDownList DL4 = DetailsView1.FindControl("DropDownList4")

    > as
    > >DropDownList;
    > >
    > > string DL4_Value = DL4.SelectedValue;
    > >

    >
    >
     
    Morris Neuman, Jan 19, 2009
    #7
  8. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    I think there might still exists some code logic that cause an recursive
    method call. Would you try send me the test page for me to perform some
    local test? Also, I suggest you try best to simplify it, remove all other
    unrelated elemetns(such as format, style, other controls). And for
    database, I suggest you use a simple table so that I can easily create a
    mock table for test.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    --------------------
    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >Subject: RE: Dropdownlist datasource dependency
    >Date: Mon, 19 Jan 2009 14:37:02 -0800


    >
    >Hi,
    >
    >I removed the Bind (SelectedValue='<%# Bind("TypeID") %>') in the
    >DropDownList2
    > <asp:DropDownList ID="DropDownList2" runat="server"
    > DataSourceID="AccessDataSource2"
    > DataTextField="BoxNumber"
    > DataValueField="BoxNumber"
    > OnDataBound="ddl2TypeID_DataBound"
    > Width="110px">
    > </asp:DropDownList>
    >
    >DropDownList4 is defined as follows:
    > <asp:DropDownList ID="DropDownList4" runat="server"

    AutoPostBack="True"
    > DataSourceID="AccessDataSource4"
    > DataTextField="WTableType"
    > DataValueField="WTableType" SelectedValue='<%#
    >Bind("TableType") %>'
    > Width="122px" >
    > <asp:ListItem Selected ="True">Mailboxes</asp:ListItem>
    > </asp:DropDownList>
    >
    >
    >My DetailsView1 is bound to a datasource and contains the 2 DropDownLists

    as
    >Insert Templates.
    >
    >In the script ddl2TypeID_DataBound, I have the following:
    >
    > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > {
    > DropDownList DL2 = DetailsView1.FindControl("DropDownList2")

    as
    >DropDownList;
    > DropDownList DL4 = DetailsView1.FindControl("DropDownList4")

    as
    >DropDownList;
    >
    > string DL4_Value = DL4.SelectedValue;
    >
    > if (DL4_Value == "Mailboxes")
    > {
    > DL2.DataSourceID = "AccessDataSource2";
    > DL2.DataTextField = "BoxNumber";
    > DL2.DataValueField = "Boxnumber";
    > }
    > else
    > {
    > DL2.DataSourceID = "AccessDataSource3";
    > DL2.DataTextField = "AttendantID";
    > DL2.DataValueField = "AttendantID";
    > }
    >
    > if (DL2 != null)
    > {
    > DL2.DataBind();
    > }
    >
    > Page.Error += new EventHandler(Page_Error);
    > }
    >
    > void Page_Error(object sender, EventArgs e)
    > {
    > Response.Write("Error" + e.ToString());
    > }
    >
    >Even if I comment out the DL2.DataBind(); statement in the script, it
    >continues to loop. Sorry I am new at this and learning so things seem a

    bit
    >difficult. I would appreciate if you can show by changing my code sample
    >what I should do.
    >--
    >Thanks
    >Morris
    >
    >
    >""Steven Cheng"" wrote:
    >
    >> Hi Morris,
    >>
    >> Based on the code you provided, the "infinite loop in your databind

    method"
    >> is cause by "you call DataBind method on Dropdownlist inside

    dropdownlist's
    >> own databound" event. This cause the databinding opeation happend
    >> recursivelyl(endless).
    >>
    >> This is the "DataBound" event handler in which you invoke dropdownlist's
    >> DataBind method.
    >> ===========
    >> protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    >> {
    >> ==================
    >>
    >> According to the article I gave you in previous message, you should
    >> manually use a for loop to add items into dropdownlist instead of using
    >> Databind. You can still use DataSource control, but not directly bind

    them
    >> to dropdownlist. You can manually call DatasourceControl.Select method

    to
    >> get the resultset and loop through the resultset and Add items into
    >> dropdownlist.
    >>
    >> Sincerely,
    >>
    >> Steven Cheng
    >>
    >> Microsoft MSDN Online Support Lead
    >>
    >>
    >> Delighting our customers is our #1 priority. We welcome your comments

    and
    >> suggestions about how we can improve the support we provide to you.

    Please
    >> feel free to let my manager know what you think of the level of service
    >> provided. You can send feedback directly to my manager at:
    >> .
    >>
    >> ==================================================
    >> Get notification to my posts through email? Please refer to
    >>

    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >>
    >> --------------------
    >> >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >> >Subject: RE: Dropdownlist datasource dependency
    >> >Date: Thu, 15 Jan 2009 16:04:01 -0800

    >>
    >> >
    >> >Thanks, I tried the demo per the link and find that my code keeps

    looping
    >> in
    >> >the ddl2TypeID_DataBound script. This is invoked by the DropDownList2
    >> >OnDataBound method per the demo.
    >> >
    >> >My code is as follows:
    >> >
    >> >
    >> ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    >> >Title="Admin-Manage Web Account-Mailboxes" %>
    >> ><%@ Import Namespace="System.Web.UI.WebControls" %>
    >> >
    >> ><script runat="server">
    >> >
    >> > protected void Page_Load(object sender, EventArgs e)
    >> > {
    >> > Label1.DataBind();
    >> > }
    >> >
    >> > protected void LinkButton1_Click(object sender, EventArgs e)
    >> > {
    >> > DetailsView1.Visible = true;
    >> > }
    >> >
    >> > protected void DetailsView1_ItemInserted(object sender,
    >> >DetailsViewInsertedEventArgs e)
    >> > {
    >> > GridView1.DataBind();
    >> > }
    >> > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    >> > {
    >> > //DropDownList ddl2 = (DropDownList)sender;
    >> > //DropDownList ddl4 = (DropDownList)sender;
    >> >
    >> > DropDownList DL2 =

    DetailsView1.FindControl("DropDownList2")
    >> as
    >> >DropDownList;
    >> > DropDownList DL4 =

    DetailsView1.FindControl("DropDownList4")
    >> as
    >> >DropDownList;
    >> >
    >> > string DL4_Value = DL4.SelectedValue;
    >> >

    >>
    >>

    >
     
    Steven Cheng, Jan 22, 2009
    #8
  9. Hi Steven,

    I have uploaded the project and the database to our FTP site. You can
    download it at
    www.speechsoft.com/guestupload/CMWebManagerDemo-StevenCheng.zip.

    I have removed all the formating and tried to make the pages simple. As you
    can see from the project, there are 2 dropdownlists (DDL4 and DDL2). I need
    to bind DDL2 to either datasource2 or datasource3 based on the selected value
    of DDL4.

    Hopefully you you will be able to see the problem and provide a solution.
    --
    Thanks
    Morris


    ""Steven Cheng"" wrote:

    > Hi Morris,
    >
    > I think there might still exists some code logic that cause an recursive
    > method call. Would you try send me the test page for me to perform some
    > local test? Also, I suggest you try best to simplify it, remove all other
    > unrelated elemetns(such as format, style, other controls). And for
    > database, I suggest you use a simple table so that I can easily create a
    > mock table for test.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > --------------------
    > >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >Subject: RE: Dropdownlist datasource dependency
    > >Date: Mon, 19 Jan 2009 14:37:02 -0800

    >
    > >
    > >Hi,
    > >
    > >I removed the Bind (SelectedValue='<%# Bind("TypeID") %>') in the
    > >DropDownList2
    > > <asp:DropDownList ID="DropDownList2" runat="server"
    > > DataSourceID="AccessDataSource2"
    > > DataTextField="BoxNumber"
    > > DataValueField="BoxNumber"
    > > OnDataBound="ddl2TypeID_DataBound"
    > > Width="110px">
    > > </asp:DropDownList>
    > >
    > >DropDownList4 is defined as follows:
    > > <asp:DropDownList ID="DropDownList4" runat="server"

    > AutoPostBack="True"
    > > DataSourceID="AccessDataSource4"
    > > DataTextField="WTableType"
    > > DataValueField="WTableType" SelectedValue='<%#
    > >Bind("TableType") %>'
    > > Width="122px" >
    > > <asp:ListItem Selected ="True">Mailboxes</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > >
    > >My DetailsView1 is bound to a datasource and contains the 2 DropDownLists

    > as
    > >Insert Templates.
    > >
    > >In the script ddl2TypeID_DataBound, I have the following:
    > >
    > > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > > {
    > > DropDownList DL2 = DetailsView1.FindControl("DropDownList2")

    > as
    > >DropDownList;
    > > DropDownList DL4 = DetailsView1.FindControl("DropDownList4")

    > as
    > >DropDownList;
    > >
    > > string DL4_Value = DL4.SelectedValue;
    > >
    > > if (DL4_Value == "Mailboxes")
    > > {
    > > DL2.DataSourceID = "AccessDataSource2";
    > > DL2.DataTextField = "BoxNumber";
    > > DL2.DataValueField = "Boxnumber";
    > > }
    > > else
    > > {
    > > DL2.DataSourceID = "AccessDataSource3";
    > > DL2.DataTextField = "AttendantID";
    > > DL2.DataValueField = "AttendantID";
    > > }
    > >
    > > if (DL2 != null)
    > > {
    > > DL2.DataBind();
    > > }
    > >
    > > Page.Error += new EventHandler(Page_Error);
    > > }
    > >
    > > void Page_Error(object sender, EventArgs e)
    > > {
    > > Response.Write("Error" + e.ToString());
    > > }
    > >
    > >Even if I comment out the DL2.DataBind(); statement in the script, it
    > >continues to loop. Sorry I am new at this and learning so things seem a

    > bit
    > >difficult. I would appreciate if you can show by changing my code sample
    > >what I should do.
    > >--
    > >Thanks
    > >Morris
    > >
    > >
    > >""Steven Cheng"" wrote:
    > >
    > >> Hi Morris,
    > >>
    > >> Based on the code you provided, the "infinite loop in your databind

    > method"
    > >> is cause by "you call DataBind method on Dropdownlist inside

    > dropdownlist's
    > >> own databound" event. This cause the databinding opeation happend
    > >> recursivelyl(endless).
    > >>
    > >> This is the "DataBound" event handler in which you invoke dropdownlist's
    > >> DataBind method.
    > >> ===========
    > >> protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > >> {
    > >> ==================
    > >>
    > >> According to the article I gave you in previous message, you should
    > >> manually use a for loop to add items into dropdownlist instead of using
    > >> Databind. You can still use DataSource control, but not directly bind

    > them
    > >> to dropdownlist. You can manually call DatasourceControl.Select method

    > to
    > >> get the resultset and loop through the resultset and Add items into
    > >> dropdownlist.
    > >>
    > >> Sincerely,
    > >>
    > >> Steven Cheng
    > >>
    > >> Microsoft MSDN Online Support Lead
    > >>
    > >>
    > >> Delighting our customers is our #1 priority. We welcome your comments

    > and
    > >> suggestions about how we can improve the support we provide to you.

    > Please
    > >> feel free to let my manager know what you think of the level of service
    > >> provided. You can send feedback directly to my manager at:
    > >> .
    > >>
    > >> ==================================================
    > >> Get notification to my posts through email? Please refer to
    > >>

    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    > >>
    > >> --------------------
    > >> >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >> >Subject: RE: Dropdownlist datasource dependency
    > >> >Date: Thu, 15 Jan 2009 16:04:01 -0800
    > >>
    > >> >
    > >> >Thanks, I tried the demo per the link and find that my code keeps

    > looping
    > >> in
    > >> >the ddl2TypeID_DataBound script. This is invoked by the DropDownList2
    > >> >OnDataBound method per the demo.
    > >> >
    > >> >My code is as follows:
    > >> >
    > >> >
    > >> ><%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    > >> >Title="Admin-Manage Web Account-Mailboxes" %>
    > >> ><%@ Import Namespace="System.Web.UI.WebControls" %>
    > >> >
    > >> ><script runat="server">
    > >> >
    > >> > protected void Page_Load(object sender, EventArgs e)
    > >> > {
    > >> > Label1.DataBind();
    > >> > }
    > >> >
    > >> > protected void LinkButton1_Click(object sender, EventArgs e)
    > >> > {
    > >> > DetailsView1.Visible = true;
    > >> > }
    > >> >
    > >> > protected void DetailsView1_ItemInserted(object sender,
    > >> >DetailsViewInsertedEventArgs e)
    > >> > {
    > >> > GridView1.DataBind();
    > >> > }
    > >> > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > >> > {
    > >> > //DropDownList ddl2 = (DropDownList)sender;
    > >> > //DropDownList ddl4 = (DropDownList)sender;
    > >> >
    > >> > DropDownList DL2 =

    > DetailsView1.FindControl("DropDownList2")
    > >> as
    > >> >DropDownList;
    > >> > DropDownList DL4 =

    > DetailsView1.FindControl("DropDownList4")
    > >> as
    > >> >DropDownList;
    > >> >
    > >> > string DL4_Value = DL4.SelectedValue;
    > >> >
    > >>
    > >>

    > >

    >
    >
     
    Morris Neuman, Jan 23, 2009
    #9
  10. Hi Morris,

    Steven asked for sick leave today. I'll follow up this case.

    I've tested your code and think you can try following steps to achieve your
    requirement.

    1. Remove DataSourceID="SqlDataSource2" from DropDownList2.
    2. Remove OnDataBound="ddl2TypeID_DataBound" from DropDownList2 or else
    there'll be a infinite loop.
    3. Remove SelectedValue='<%# Eval("TypeID") %>' from DropDownList2. I don't
    know what's the effect you need by doing this. Could you clarify it?
    4. Add the SelectedIndexChanged event handler for DropDownList4 and rebind
    the DropDownList 2 in this event handler.

    Here's the edited code for Default.aspx.

    <%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    Title="Admin-Manage Web Account-Mailboxes" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.DataBind();
    }


    protected void DetailsView1_ItemInserted(object sender,
    DetailsViewInsertedEventArgs e)
    {
    }


    void Page_Error(object sender, EventArgs e)
    {
    Response.Write("Error" + e.ToString());
    }


    protected void SqlDataSource1_Selected(object sender,
    SqlDataSourceStatusEventArgs e)
    {

    }

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
    BindDDL2();
    }
    void BindDDL2() {
    DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    DropDownList;
    DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    DropDownList;
    SqlDataSource sds2 = DetailsView1.FindControl("SqlDataSource2") as
    SqlDataSource;
    SqlDataSource sds3 = DetailsView1.FindControl("SqlDataSource3") as
    SqlDataSource;
    string DL4_Value = DL4.SelectedValue;

    if (DL4_Value == "Mailboxes")
    {
    DL2.DataSource = sds2;
    DL2.DataTextField = "BoxNumber";
    DL2.DataValueField = "Boxnumber";
    }
    else
    {
    DL2.DataSource = sds3;
    DL2.DataTextField = "AttendantID";
    DL2.DataValueField = "AttendantID";
    }

    if (DL2 != null)
    {
    DL2.DataBind();
    }

    Page.Error += new EventHandler(Page_Error);
    }

    protected void DropDownList4_SelectedIndexChanged(object sender,
    EventArgs e)
    {
    BindDDL2();
    }
    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <br />

    <span style="color: navy; font-family: Verdana"><strong>Table Types for
    Web Account </strong></span>
    <asp:Label ID="Label1" runat="server" Text="cmadmin"
    Font-Names="Verdana" Font-Size="12pt" Font-Bold="True"
    ForeColor="Navy"></asp:Label><br />
    <br />
    <!--</strong></span>-->
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataSourceID="SqlDataSource1"
    DefaultMode="Insert"
    DataKeyNames="WebAccountName,WebAccountID"
    ondatabound="DetailsView1_DataBound">
    <Fields>
    <asp:BoundField DataField="WebAccountID"
    HeaderText="WebAccountID"
    InsertVisible="False" SortExpression="WebAccountID" />
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName" InsertVisible="False"
    SortExpression="WebAccountName" />
    <asp:TemplateField HeaderText="TableType &amp; ID"
    SortExpression="TableType">
    <InsertItemTemplate>
    <table >
    <tr>
    <td >
    <asp:Label ID="Label5" runat="server"
    Text="Select Table Name"></asp:Label>
    </td>
    <td >
    <asp:DropDownList
    OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged"
    ID="DropDownList4" runat="server" AutoPostBack="True"
    DataSourceID="SqlDataSource4"
    DataTextField="WTableType"
    DataValueField="WTableType"
    SelectedValue='<%# Bind("TableType") %>'>
    <asp:ListItem Selected
    ="True">Mailboxes</asp:ListItem>
    </asp:DropDownList>
    </td>
    <td>
    <asp:Label ID="Label4" runat="server"
    style="text-align: left"
    Text=" then select Record ID">
    </asp:Label>
    </td>
    <td>
    <asp:DropDownList ID="DropDownList2"
    runat="server"
    DataTextField="BoxNumber"
    DataValueField="BoxNumber"
    >

    </asp:DropDownList>
    </td>
    </tr>
    </table>
    &nbsp;

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT [BoxNumber] FROM [Mailboxes]
    ORDER BY [BoxNumber]">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT AttendantID FROM Attendant
    GROUP BY AttendantID ORDER BY AttendantID">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT WTableType FROM
    WebAccountTableType ORDER BY WTableType Desc"
    >

    </asp:SqlDataSource>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField InsertVisible="False" ShowInsertButton="True"
    >

    <ControlStyle Font-Names="Verdana" />
    </asp:CommandField>
    </Fields>

    </asp:DetailsView>
    <br />

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"

    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    @WebAccountID" InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    SelectCommand="SELECT [WebAccountID], [WebAccountName],
    [TableType], [TypeID] FROM [WebAccount] WHERE ([WebAccountName] =
    @WebAccountName) ORDER BY [WebAccountName], [TableType], [TypeID]"

    UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    @TableType WHERE (WebAccountID = @WebAccountID)"
    >

    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    <asp:parameter Name="TableType" />
    </UpdateParameters>
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <InsertParameters>
    <asp:ControlParameter ControlID="Label1" Name="WebAccountName"
    PropertyName="Text" Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:SqlDataSource>

    <br />
    <br />
    &nbsp;&nbsp;&nbsp;

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
    bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
    bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;
    <br />
    <br />
    </asp:Content>




    Please let me know if above code is what you need and feel free to ask if
    you need additional assistance.

    Regards,
    Allen Chen
    Microsoft Online Support
     
    Allen Chen [MSFT], Jan 23, 2009
    #10
  11. Hi Allen,

    Thanks for the info. For the most part it worked (i.e. when I selected the
    value of DDL4, DDL2 bound to the correct datasource).

    1) However, when I inserted the DetailsView1 record the value selected in
    DDL2 did not get added to the record as it should be bound to TypeID field
    per DetailsView1 DataSourceID1. This is what I was attempting per the
    SelectedValue='<%# Bind("TypeID") %>' from DropDownList2. How do I do that?

    2) Per your code, you initiate DetailsView1_DataBound where you call
    BindDDL2(); and you also initiate DropDownList4_SelectedIndexChanged where
    you call BindDDL2(); What event triggers the DetailsView_DataBound?

    3) You have a blank script SqlDataSource1_Selected? Is that required?

    Once issue 1 above is addressed then this should work as desired.

    Look forward to your response.
    --
    Thanks
    Morris


    "Allen Chen [MSFT]" wrote:

    > Hi Morris,
    >
    > Steven asked for sick leave today. I'll follow up this case.
    >
    > I've tested your code and think you can try following steps to achieve your
    > requirement.
    >
    > 1. Remove DataSourceID="SqlDataSource2" from DropDownList2.
    > 2. Remove OnDataBound="ddl2TypeID_DataBound" from DropDownList2 or else
    > there'll be a infinite loop.
    > 3. Remove SelectedValue='<%# Eval("TypeID") %>' from DropDownList2. I don't
    > know what's the effect you need by doing this. Could you clarify it?
    > 4. Add the SelectedIndexChanged event handler for DropDownList4 and rebind
    > the DropDownList 2 in this event handler.
    >
    > Here's the edited code for Default.aspx.
    >
    > <%@ Page Language="C#" MasterPageFile="~/MasterPage1.master"
    > Title="Admin-Manage Web Account-Mailboxes" %>
    > <%@ Import Namespace="System.Web.UI.WebControls" %>
    >
    > <script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Label1.DataBind();
    > }
    >
    >
    > protected void DetailsView1_ItemInserted(object sender,
    > DetailsViewInsertedEventArgs e)
    > {
    > }
    >
    >
    > void Page_Error(object sender, EventArgs e)
    > {
    > Response.Write("Error" + e.ToString());
    > }
    >
    >
    > protected void SqlDataSource1_Selected(object sender,
    > SqlDataSourceStatusEventArgs e)
    > {
    >
    > }
    >
    > protected void DetailsView1_DataBound(object sender, EventArgs e)
    > {
    > BindDDL2();
    > }
    > void BindDDL2() {
    > DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    > DropDownList;
    > DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    > DropDownList;
    > SqlDataSource sds2 = DetailsView1.FindControl("SqlDataSource2") as
    > SqlDataSource;
    > SqlDataSource sds3 = DetailsView1.FindControl("SqlDataSource3") as
    > SqlDataSource;
    > string DL4_Value = DL4.SelectedValue;
    >
    > if (DL4_Value == "Mailboxes")
    > {
    > DL2.DataSource = sds2;
    > DL2.DataTextField = "BoxNumber";
    > DL2.DataValueField = "Boxnumber";
    > }
    > else
    > {
    > DL2.DataSource = sds3;
    > DL2.DataTextField = "AttendantID";
    > DL2.DataValueField = "AttendantID";
    > }
    >
    > if (DL2 != null)
    > {
    > DL2.DataBind();
    > }
    >
    > Page.Error += new EventHandler(Page_Error);
    > }
    >
    > protected void DropDownList4_SelectedIndexChanged(object sender,
    > EventArgs e)
    > {
    > BindDDL2();
    > }
    > </script>
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <br />
    >
    > <span style="color: navy; font-family: Verdana"><strong>Table Types for
    > Web Account </strong></span>
    > <asp:Label ID="Label1" runat="server" Text="cmadmin"
    > Font-Names="Verdana" Font-Size="12pt" Font-Bold="True"
    > ForeColor="Navy"></asp:Label><br />
    > <br />
    > <!--</strong></span>-->
    > <br />
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AutoGenerateInsertButton="True"
    > AutoGenerateRows="False" DataSourceID="SqlDataSource1"
    > DefaultMode="Insert"
    > DataKeyNames="WebAccountName,WebAccountID"
    > ondatabound="DetailsView1_DataBound">
    > <Fields>
    > <asp:BoundField DataField="WebAccountID"
    > HeaderText="WebAccountID"
    > InsertVisible="False" SortExpression="WebAccountID" />
    > <asp:BoundField DataField="WebAccountName"
    > HeaderText="WebAccountName" InsertVisible="False"
    > SortExpression="WebAccountName" />
    > <asp:TemplateField HeaderText="TableType & ID"
    > SortExpression="TableType">
    > <InsertItemTemplate>
    > <table >
    > <tr>
    > <td >
    > <asp:Label ID="Label5" runat="server"
    > Text="Select Table Name"></asp:Label>
    > </td>
    > <td >
    > <asp:DropDownList
    > OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged"
    > ID="DropDownList4" runat="server" AutoPostBack="True"
    > DataSourceID="SqlDataSource4"
    > DataTextField="WTableType"
    > DataValueField="WTableType"
    > SelectedValue='<%# Bind("TableType") %>'>
    > <asp:ListItem Selected
    > ="True">Mailboxes</asp:ListItem>
    > </asp:DropDownList>
    > </td>
    > <td>
    > <asp:Label ID="Label4" runat="server"
    > style="text-align: left"
    > Text=" then select Record ID">
    > </asp:Label>
    > </td>
    > <td>
    > <asp:DropDownList ID="DropDownList2"
    > runat="server"
    > DataTextField="BoxNumber"
    > DataValueField="BoxNumber"
    > >

    > </asp:DropDownList>
    > </td>
    > </tr>
    > </table>
    >
    >
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT [BoxNumber] FROM [Mailboxes]
    > ORDER BY [BoxNumber]">
    > </asp:SqlDataSource>
    >
    > <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT AttendantID FROM Attendant
    > GROUP BY AttendantID ORDER BY AttendantID">
    > </asp:SqlDataSource>
    >
    > <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT WTableType FROM
    > WebAccountTableType ORDER BY WTableType Desc"
    > >

    > </asp:SqlDataSource>
    > </InsertItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label2" runat="server" Text='<%#
    > Bind("TableType") %>'></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    > <asp:CommandField InsertVisible="False" ShowInsertButton="True"
    > >

    > <ControlStyle Font-Names="Verdana" />
    > </asp:CommandField>
    > </Fields>
    >
    > </asp:DetailsView>
    > <br />
    >
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    >
    > DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    > @WebAccountID" InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    > [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    > SelectCommand="SELECT [WebAccountID], [WebAccountName],
    > [TableType], [TypeID] FROM [WebAccount] WHERE ([WebAccountName] =
    > @WebAccountName) ORDER BY [WebAccountName], [TableType], [TypeID]"
    >
    > UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    > @TableType WHERE (WebAccountID = @WebAccountID)"
    > >

    > <DeleteParameters>
    > <asp:parameter Name="WebAccountID" Type="Int32" />
    > </DeleteParameters>
    > <UpdateParameters>
    > <asp:parameter Name="TypeID" Type="String" />
    > <asp:parameter Name="WebAccountID" Type="Int32" />
    > <asp:parameter Name="TableType" />
    > </UpdateParameters>
    > <SelectParameters>
    > <asp:QueryStringParameter Name="WebAccountName"
    > QueryStringField="User"
    > Type="String" />
    > </SelectParameters>
    > <InsertParameters>
    > <asp:ControlParameter ControlID="Label1" Name="WebAccountName"
    > PropertyName="Text" Type="String" />
    > <asp:parameter Name="TypeID" Type="String" />
    > <asp:parameter Name="TableType" />
    > </InsertParameters>
    > </asp:SqlDataSource>
    >
    > <br />
    > <br />
    >
    >
    > &nbs
    > p; &n
    > bsp;
    > &nbs
    > p; &n
    > bsp;
    >
    > <br />
    > <br />
    > </asp:Content>
    >
    >
    >
    >
    > Please let me know if above code is what you need and feel free to ask if
    > you need additional assistance.
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
    >
     
    Morris Neuman, Jan 27, 2009
    #11
  12. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    Sorry for the delay due to my sick leave in the previous days. I've
    performed some tests on the web project you provided. Here are some of my
    findings:

    1. In the "default.aspx", to simplify testing, I change the masterpage to
    my own empty masterpage(to avoid additional error). And then, change some
    DataSource controls' ID since some of them still refer to
    AccessDataSource....

    2. As we discussed earlier, the "infinite loop" problem is due to calling
    "databind" method inside dropdownlist's own "DataBound" event. Therefore, I
    removed the "DataBind" code logic inside "ddl2TypeID_DataBound" event. And
    I use a custom function to populate the Dropdownlist2(instead of using
    databinding), you can check the "Bind_SubDropDown" function I used in the
    modified page. Also, the advantage of using this means is that you can
    still utilize the two-way databinding expression (<% Bind() %>) on the
    dropdownlist.

    Here is the complete version of the modified page. I'll also email you the
    page so that you can test it on your side.

    =================modified default.aspx===================================
    <%@ Page Language="C#" MasterPageFile="~/site.master" Title="Admin-Manage
    Web Account-Mailboxes" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.DataBind();
    }


    protected void DetailsView1_ItemInserted(object sender,
    DetailsViewInsertedEventArgs e)
    {
    }



    protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    {
    Bind_SubDropDown();


    Page.Error += new EventHandler(Page_Error);
    }


    void Bind_SubDropDown()
    {
    DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    DropDownList;
    DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    DropDownList;

    SqlDataSource DS2 = DetailsView1.FindControl("SqlDataSource2") as
    SqlDataSource;
    SqlDataSource DS3 = DetailsView1.FindControl("SqlDataSource3") as
    SqlDataSource;



    string DL4_Value = DL4.SelectedValue;


    DL2.Items.Clear();
    if (DL4_Value == "Mailboxes")
    {

    System.Data.DataView rs2 =
    (System.Data.DataView)DS2.Select(DataSourceSelectArguments.Empty);

    foreach (System.Data.DataRowView r in rs2)
    {
    DL2.Items.Add(new ListItem(r["BoxNumber"].ToString(),
    r["Boxnumber"].ToString()));
    }

    }
    else
    {

    System.Data.DataView rs3 =
    (System.Data.DataView)DS3.Select(DataSourceSelectArguments.Empty);

    foreach (System.Data.DataRowView r in rs3)
    {
    DL2.Items.Add(new ListItem(r["AttendantID"].ToString(),
    r["AttendantID"].ToString()));
    }
    }
    }

    void Page_Error(object sender, EventArgs e)
    {
    Response.Write("Error" + e.ToString());
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    protected void DropDownList4_SelectedIndexChanged(object sender,
    EventArgs e)
    {
    Bind_SubDropDown();
    }
    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <br />
    <span style="color: navy; font-family: Verdana"><strong>Table Types for
    Web Account </strong></span>
    <hr />
    <asp:Label ID="Label1" runat="server" Text="cmadmin"
    Font-Names="Verdana" Font-Size="12pt" Font-Bold="True"
    ForeColor="Black"></asp:Label><br />
    <br />
    <!--</strong></span>-->
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataSourceID="SqlDataSource1"
    DefaultMode="Insert"
    DataKeyNames="WebAccountName,WebAccountID">
    <Fields>
    <asp:BoundField DataField="WebAccountID"
    HeaderText="WebAccountID"
    InsertVisible="False" SortExpression="WebAccountID" />
    <asp:BoundField DataField="WebAccountName"
    HeaderText="WebAccountName" InsertVisible="False"
    SortExpression="WebAccountName" />
    <asp:TemplateField HeaderText="TableType &amp; ID"
    SortExpression="TableType">
    <InsertItemTemplate>
    <table >
    <tr>
    <td >
    <asp:Label ID="Label5" runat="server"
    Text="Select Table Name"></asp:Label>
    </td>
    <td >
    <asp:DropDownList ID="DropDownList4"
    runat="server" AutoPostBack="True"
    DataSourceID="SqlDataSource4"
    DataTextField="WTableType"
    DataValueField="WTableType"
    SelectedValue='<%# Bind("TableType") %>'
    OnDataBound="ddl2TypeID_DataBound"
    onselectedindexchanged="DropDownList4_SelectedIndexChanged"
    >

    <asp:ListItem Selected
    ="True">Mailboxes</asp:ListItem>
    </asp:DropDownList>
    </td>
    <td>
    <asp:Label ID="Label4" runat="server"
    style="text-align: left"
    Text=" then select Record ID">
    </asp:Label>
    </td>
    <td>
    <asp:DropDownList ID="DropDownList2"
    runat="server"

    SelectedValue='<%# Bind("TypeID") %>'>
    </asp:DropDownList>
    </td>
    </tr>
    </table>
    &nbsp;

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT [BoxNumber] FROM [Mailboxes]
    ORDER BY [BoxNumber]">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT AttendantID FROM Attendant
    GROUP BY AttendantID ORDER BY AttendantID">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    ConnectionString="<%$
    ConnectionStrings:CallMasterSQLConnectionString %>"
    SelectCommand="SELECT WTableType FROM
    WebAccountTableType ORDER BY WTableType Desc">
    </asp:SqlDataSource>
    <br />


    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%#
    Bind("TableType") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField InsertVisible="False" ShowInsertButton="True"
    >

    <ControlStyle Font-Names="Verdana" />
    </asp:CommandField>
    </Fields>

    </asp:DetailsView>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
    Text="Button" />
    <br />

    <hr />

    <hr />


    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    @WebAccountID"
    InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    SelectCommand="SELECT [WebAccountID], [WebAccountName],
    [TableType], [TypeID] FROM [WebAccount] WHERE ([WebAccountName] =
    @WebAccountName) ORDER BY [WebAccountName], [TableType], [TypeID]"
    UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    @TableType WHERE (WebAccountID = @WebAccountID)">
    <DeleteParameters>
    <asp:parameter Name="WebAccountID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="WebAccountID" Type="Int32" />
    <asp:parameter Name="TableType" />
    </UpdateParameters>
    <SelectParameters>
    <asp:QueryStringParameter Name="WebAccountName"
    QueryStringField="User"
    Type="String" />
    </SelectParameters>
    <InsertParameters>
    <asp:ControlParameter ControlID="Label1" DefaultValue="cmadmin"
    Name="WebAccountName"
    PropertyName="Text" Type="String" />
    <asp:parameter Name="TypeID" Type="String" />
    <asp:parameter Name="TableType" />
    </InsertParameters>
    </asp:SqlDataSource>

    <br />
    <br />
    &nbsp;&nbsp;&nbsp;

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
    bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
    bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;
    <br />
    <br />
    </asp:Content>
    =====================================================

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    --------------------
    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    >Subject: RE: Dropdownlist datasource dependency
    >Date: Mon, 26 Jan 2009 16:36:01 -0800


    >
    >Hi Allen,
    >
    >Thanks for the info. For the most part it worked (i.e. when I selected

    the
    >value of DDL4, DDL2 bound to the correct datasource).
    >
    >1) However, when I inserted the DetailsView1 record the value selected in
    >DDL2 did not get added to the record as it should be bound to TypeID field
    >per DetailsView1 DataSourceID1. This is what I was attempting per the
    >SelectedValue='<%# Bind("TypeID") %>' from DropDownList2. How do I do

    that?
    >
    >2) Per your code, you initiate DetailsView1_DataBound where you call
    >BindDDL2(); and you also initiate DropDownList4_SelectedIndexChanged where
    >you call BindDDL2(); What event triggers the DetailsView_DataBound?
    >
    >3) You have a blank script SqlDataSource1_Selected? Is that required?
    >
    >Once issue 1 above is addressed then this should work as desired.
    >
    >Look forward to your response.
    >--
    >Thanks
    >Morris
    >
    >
    >"A
     
    Steven Cheng, Jan 28, 2009
    #12
  13. Thanks for the info.

    Since I had the solution proposed 1/22 working except for the binding of the
    dropdownlist2 value when inserting the record, I continued to develop using
    this solution. I just added a detailsview1_iteminserting script to add the
    selected values as the required insert parameter so the datasource could then
    insert the record with these values.

    This worked and I was able to select the proper value and insert the record
    as required.

    Thanks for your help. Hope you are feeling better.
    --
    Morris


    ""Steven Cheng"" wrote:

    > Hi Morris,
    >
    > Sorry for the delay due to my sick leave in the previous days. I've
    > performed some tests on the web project you provided. Here are some of my
    > findings:
    >
    > 1. In the "default.aspx", to simplify testing, I change the masterpage to
    > my own empty masterpage(to avoid additional error). And then, change some
    > DataSource controls' ID since some of them still refer to
    > AccessDataSource....
    >
    > 2. As we discussed earlier, the "infinite loop" problem is due to calling
    > "databind" method inside dropdownlist's own "DataBound" event. Therefore, I
    > removed the "DataBind" code logic inside "ddl2TypeID_DataBound" event. And
    > I use a custom function to populate the Dropdownlist2(instead of using
    > databinding), you can check the "Bind_SubDropDown" function I used in the
    > modified page. Also, the advantage of using this means is that you can
    > still utilize the two-way databinding expression (<% Bind() %>) on the
    > dropdownlist.
    >
    > Here is the complete version of the modified page. I'll also email you the
    > page so that you can test it on your side.
    >
    > =================modified default.aspx===================================
    > <%@ Page Language="C#" MasterPageFile="~/site.master" Title="Admin-Manage
    > Web Account-Mailboxes" %>
    > <%@ Import Namespace="System.Web.UI.WebControls" %>
    >
    > <script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Label1.DataBind();
    > }
    >
    >
    > protected void DetailsView1_ItemInserted(object sender,
    > DetailsViewInsertedEventArgs e)
    > {
    > }
    >
    >
    >
    > protected void ddl2TypeID_DataBound(object sender, EventArgs e)
    > {
    > Bind_SubDropDown();
    >
    >
    > Page.Error += new EventHandler(Page_Error);
    > }
    >
    >
    > void Bind_SubDropDown()
    > {
    > DropDownList DL2 = DetailsView1.FindControl("DropDownList2") as
    > DropDownList;
    > DropDownList DL4 = DetailsView1.FindControl("DropDownList4") as
    > DropDownList;
    >
    > SqlDataSource DS2 = DetailsView1.FindControl("SqlDataSource2") as
    > SqlDataSource;
    > SqlDataSource DS3 = DetailsView1.FindControl("SqlDataSource3") as
    > SqlDataSource;
    >
    >
    >
    > string DL4_Value = DL4.SelectedValue;
    >
    >
    > DL2.Items.Clear();
    > if (DL4_Value == "Mailboxes")
    > {
    >
    > System.Data.DataView rs2 =
    > (System.Data.DataView)DS2.Select(DataSourceSelectArguments.Empty);
    >
    > foreach (System.Data.DataRowView r in rs2)
    > {
    > DL2.Items.Add(new ListItem(r["BoxNumber"].ToString(),
    > r["Boxnumber"].ToString()));
    > }
    >
    > }
    > else
    > {
    >
    > System.Data.DataView rs3 =
    > (System.Data.DataView)DS3.Select(DataSourceSelectArguments.Empty);
    >
    > foreach (System.Data.DataRowView r in rs3)
    > {
    > DL2.Items.Add(new ListItem(r["AttendantID"].ToString(),
    > r["AttendantID"].ToString()));
    > }
    > }
    > }
    >
    > void Page_Error(object sender, EventArgs e)
    > {
    > Response.Write("Error" + e.ToString());
    > }
    >
    >
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    >
    > }
    >
    > protected void DropDownList4_SelectedIndexChanged(object sender,
    > EventArgs e)
    > {
    > Bind_SubDropDown();
    > }
    > </script>
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <br />
    > <span style="color: navy; font-family: Verdana"><strong>Table Types for
    > Web Account </strong></span>
    > <hr />
    > <asp:Label ID="Label1" runat="server" Text="cmadmin"
    > Font-Names="Verdana" Font-Size="12pt" Font-Bold="True"
    > ForeColor="Black"></asp:Label><br />
    > <br />
    > <!--</strong></span>-->
    > <br />
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AutoGenerateInsertButton="True"
    > AutoGenerateRows="False" DataSourceID="SqlDataSource1"
    > DefaultMode="Insert"
    > DataKeyNames="WebAccountName,WebAccountID">
    > <Fields>
    > <asp:BoundField DataField="WebAccountID"
    > HeaderText="WebAccountID"
    > InsertVisible="False" SortExpression="WebAccountID" />
    > <asp:BoundField DataField="WebAccountName"
    > HeaderText="WebAccountName" InsertVisible="False"
    > SortExpression="WebAccountName" />
    > <asp:TemplateField HeaderText="TableType & ID"
    > SortExpression="TableType">
    > <InsertItemTemplate>
    > <table >
    > <tr>
    > <td >
    > <asp:Label ID="Label5" runat="server"
    > Text="Select Table Name"></asp:Label>
    > </td>
    > <td >
    > <asp:DropDownList ID="DropDownList4"
    > runat="server" AutoPostBack="True"
    > DataSourceID="SqlDataSource4"
    > DataTextField="WTableType"
    > DataValueField="WTableType"
    > SelectedValue='<%# Bind("TableType") %>'
    > OnDataBound="ddl2TypeID_DataBound"
    > onselectedindexchanged="DropDownList4_SelectedIndexChanged"
    > >

    > <asp:ListItem Selected
    > ="True">Mailboxes</asp:ListItem>
    > </asp:DropDownList>
    > </td>
    > <td>
    > <asp:Label ID="Label4" runat="server"
    > style="text-align: left"
    > Text=" then select Record ID">
    > </asp:Label>
    > </td>
    > <td>
    > <asp:DropDownList ID="DropDownList2"
    > runat="server"
    >
    > SelectedValue='<%# Bind("TypeID") %>'>
    > </asp:DropDownList>
    > </td>
    > </tr>
    > </table>
    >
    >
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT [BoxNumber] FROM [Mailboxes]
    > ORDER BY [BoxNumber]">
    > </asp:SqlDataSource>
    >
    > <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT AttendantID FROM Attendant
    > GROUP BY AttendantID ORDER BY AttendantID">
    > </asp:SqlDataSource>
    >
    > <asp:SqlDataSource ID="SqlDataSource4" runat="server"
    > ConnectionString="<%$
    > ConnectionStrings:CallMasterSQLConnectionString %>"
    > SelectCommand="SELECT WTableType FROM
    > WebAccountTableType ORDER BY WTableType Desc">
    > </asp:SqlDataSource>
    > <br />
    >
    >
    > </InsertItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label2" runat="server" Text='<%#
    > Bind("TableType") %>'></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    > <asp:CommandField InsertVisible="False" ShowInsertButton="True"
    > >

    > <ControlStyle Font-Names="Verdana" />
    > </asp:CommandField>
    > </Fields>
    >
    > </asp:DetailsView>
    > <br />
    > <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
    > Text="Button" />
    > <br />
    >
    > <hr />
    >
    > <hr />
    >
    >
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:CallMasterSQLConnectionString %>"
    > DeleteCommand="DELETE FROM [WebAccount] WHERE [WebAccountID] =
    > @WebAccountID"
    > InsertCommand="INSERT INTO [WebAccount] ([WebAccountName],
    > [TypeID], [TableType]) VALUES (@WebAccountName, @TypeID, @TableType)"
    > SelectCommand="SELECT [WebAccountID], [WebAccountName],
    > [TableType], [TypeID] FROM [WebAccount] WHERE ([WebAccountName] =
    > @WebAccountName) ORDER BY [WebAccountName], [TableType], [TypeID]"
    > UpdateCommand="UPDATE WebAccount SET TypeID = @TypeID, TableType =
    > @TableType WHERE (WebAccountID = @WebAccountID)">
    > <DeleteParameters>
    > <asp:parameter Name="WebAccountID" Type="Int32" />
    > </DeleteParameters>
    > <UpdateParameters>
    > <asp:parameter Name="TypeID" Type="String" />
    > <asp:parameter Name="WebAccountID" Type="Int32" />
    > <asp:parameter Name="TableType" />
    > </UpdateParameters>
    > <SelectParameters>
    > <asp:QueryStringParameter Name="WebAccountName"
    > QueryStringField="User"
    > Type="String" />
    > </SelectParameters>
    > <InsertParameters>
    > <asp:ControlParameter ControlID="Label1" DefaultValue="cmadmin"
    > Name="WebAccountName"
    > PropertyName="Text" Type="String" />
    > <asp:parameter Name="TypeID" Type="String" />
    > <asp:parameter Name="TableType" />
    > </InsertParameters>
    > </asp:SqlDataSource>
    >
    > <br />
    > <br />
    >
    >
    > &nbs
    > p; &n
    > bsp;
    > &nbs
    > p; &n
    > bsp;
    >
    > <br />
    > <br />
    > </asp:Content>
    > =====================================================
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > --------------------
    > >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>
    > >Subject: RE: Dropdownlist datasource dependency
    > >Date: Mon, 26 Jan 2009 16:36:01 -0800

    >
    > >
    > >Hi Allen,
    > >
    > >Thanks for the info. For the most part it worked (i.e. when I selected
     
    Morris Neuman, Jan 29, 2009
    #13
  14. Morris Neuman

    Steven Cheng Guest

    Thanks for your followup Morris,

    I'm glad that you've also got it working.

    yes, the way you used (which manually extract the parameter values and
    supply it in DetailsView_Inserting event) is another workable approach. My
    solution in previous reply will keep the two-way databinding (<% Bind %>)
    working. Anyway, if you have any further questions or need any help in the
    future, please feel free to post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .


    --------------------

    >From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <>=
    >Subject: RE: Dropdownlist datasource dependency
    >Date: Wed, 28 Jan 2009 16:03:02 -0800


    >Thanks for the info.
    >
    >Since I had the solution proposed 1/22 working except for the binding of

    the
    >dropdownlist2 value when inserting the record, I continued to develop

    using
    >this solution. I just added a detailsview1_iteminserting script to add

    the
    >selected values as the required insert parameter so the datasource could

    then
    >insert the record with these values.
    >
    >This worked and I was able to select the proper value and insert the

    record
    >as required.
    >
    >Thanks for your help. Hope you are feeling better.
    >--
    >Morris
    >
    >
    >""
     
    Steven Cheng, Jan 30, 2009
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DesignerX
    Replies:
    5
    Views:
    19,720
    HemaRitchie
    Dec 29, 2008
  2. Julie Barnet

    Changing datasource of dropdownlist

    Julie Barnet, Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    Julie Barnet
    Aug 20, 2003
  3. VB Programmer
    Replies:
    0
    Views:
    715
    VB Programmer
    Apr 7, 2004
  4. Jim Bancroft
    Replies:
    4
    Views:
    9,251
    Saravana
    Jan 6, 2005
  5. Corobori
    Replies:
    3
    Views:
    480
    Eliyahu Goldin
    Feb 20, 2005
Loading...

Share This Page