Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dropdownlist datasource dependency

Reply
Thread Tools

Dropdownlist datasource dependency

 
 
Morris Neuman
Guest
Posts: n/a
 
      01-13-2009
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\ww wroot\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>
<aspropDownList ID="DropDownList3" runat="server"
AutoPostBack="True"
DataSourceID="AccessDataSource4"
DataTextField="TableType"
DataValueField="TableType" SelectedValue='<%#
Bind("TableType") %>' >
</aspropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("TableType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type ID" SortExpression="TypeID">
<EditItemTemplate>
<aspropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("TypeID") %>'
Width="109px">
</aspropDownList>
</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 />
<aspetailsView 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>
<aspropDownList ID="DropDownList4" runat="server"
DataSourceID="AccessDataSource4"
DataTextField="TableType"
DataValueField="TableType" SelectedValue='<%#
Bind("TableType") %>'
OnSelectedIndexChanged="DropDownList4_TableTypeSel ectedIndexChanged">
</aspropDownList>
</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>
<aspropDownList ID="DropDownList2" runat="server"
DataSourceID="AccessDataSource2"
DataTextField="BoxNumber"
DataValueField="BoxNumber" SelectedValue='<%#
Bind("BoxNumber") %>'
Width="150px">
</aspropDownList>
</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" />
</aspetailsView>
<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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="TableType" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
<asparameter Name="TableType" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
</SelectParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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\ww wroot\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
 
Reply With Quote
 
 
 
 
Steven Cheng
Guest
Posts: n/a
 
      01-13-2009
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>
<asparameter Name="t1_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="t1_name" Type="String" />
<asparameter Name="t1_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asparameter Name="t1_name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
<aspetailsView 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>
</aspetailsView>
<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/subs...#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/subs.../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\&applicationUr l=/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>
>


 
Reply With Quote
 
 
 
 
Morris Neuman
Guest
Posts: n/a
 
      01-14-2009
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 />

<aspetailsView 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>
<aspropDownList ID="DropDownList4" runat="server"
DataSourceID="AccessDataSource4"
DataTextField="WTableType" AutoPostBack="True"
DataValueField="WTableType" SelectedValue='<%#
Bind("TableType") %>'

OnSelectedIndexChanged="DropDownList4_TableTypeSel ectedIndexChanged">
</aspropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("TableType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TypeID" SortExpression="TypeID">
<InsertItemTemplate>
<aspropDownList ID="DropDownList2" runat="server"
DataSourceID="AccessDataSource3"
DataTextField="AttendantID"
DataValueField="AttendantID" SelectedValue='<%#
Bind("TypeID") %>'
Width="150px">
</aspropDownList>
</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" />
</aspetailsView>
<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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="TableType" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
<asparameter Name="TableType" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
</SelectParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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>
> <asparameter Name="t1_id" Type="Int32" />
> </DeleteParameters>
> <UpdateParameters>
> <asparameter Name="t1_name" Type="String" />
> <asparameter Name="t1_id" Type="Int32" />
> </UpdateParameters>
> <InsertParameters>
> <asparameter Name="t1_name" Type="String" />
> </InsertParameters>
> </asp:SqlDataSource>
>
> </div>
> <aspetailsView 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>
> </aspetailsView>
> <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/subs...#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/subs.../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"

 
Reply With Quote
 
Steven Cheng
Guest
Posts: n/a
 
      01-15-2009
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/...s/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: <52048295-FB13-4AB9-9F9E->

<>
>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;
>


 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-16-2009
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 />
<aspetailsView 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">
<aspropDownList ID="DropDownList4"
runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource4"
DataTextField="WTableType"
DataValueField="WTableType"
SelectedValue='<%# Bind("TableType") %>'
Width="122px" >
<asp:ListItem Selected
="True">Mailboxes</asp:ListItem>
</aspropDownList>
</td>
<td>
<asp:Label ID="Label4" runat="server"
style="text-align: left"
Text=" then select Record ID">
</asp:Label>
</td>
<td>
<aspropDownList ID="DropDownList2"
runat="server"
DataSourceID="AccessDataSource2"
DataTextField="BoxNumber"
DataValueField="BoxNumber"
OnDataBound="ddl2TypeID_DataBound"
SelectedValue='<%# Bind("TypeID") %>'
Width="110px">
</aspropDownList>
</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" />
</aspetailsView>
<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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="TableType" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
<asparameter Name="TableType" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
</SelectParameters>
<InsertParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter 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/...s/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: <52048295-FB13-4AB9-9F9E->

> <>
> >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;
> >

>
>

 
Reply With Quote
 
Steven Cheng
Guest
Posts: n/a
 
      01-19-2009
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/subs...#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;
>


 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-19-2009
Hi,

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

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


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/subs...#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;
> >

>
>

 
Reply With Quote
 
Steven Cheng
Guest
Posts: n/a
 
      01-22-2009
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
> <aspropDownList ID="DropDownList2" runat="server"
> DataSourceID="AccessDataSource2"
> DataTextField="BoxNumber"
> DataValueField="BoxNumber"
> OnDataBound="ddl2TypeID_DataBound"
> Width="110px">
> </aspropDownList>
>
>DropDownList4 is defined as follows:
> <aspropDownList ID="DropDownList4" runat="server"

AutoPostBack="True"
> DataSourceID="AccessDataSource4"
> DataTextField="WTableType"
> DataValueField="WTableType" SelectedValue='<%#
>Bind("TableType") %>'
> Width="122px" >
> <asp:ListItem Selected ="True">Mailboxes</asp:ListItem>
> </aspropDownList>
>
>
>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/subs...#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;
>> >

>>
>>

>


 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-23-2009
Hi Steven,

I have uploaded the project and the database to our FTP site. You can
download it at
http://www.speechsoft.com/guestuploa...tevenCheng.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
> > <aspropDownList ID="DropDownList2" runat="server"
> > DataSourceID="AccessDataSource2"
> > DataTextField="BoxNumber"
> > DataValueField="BoxNumber"
> > OnDataBound="ddl2TypeID_DataBound"
> > Width="110px">
> > </aspropDownList>
> >
> >DropDownList4 is defined as follows:
> > <aspropDownList ID="DropDownList4" runat="server"

> AutoPostBack="True"
> > DataSourceID="AccessDataSource4"
> > DataTextField="WTableType"
> > DataValueField="WTableType" SelectedValue='<%#
> >Bind("TableType") %>'
> > Width="122px" >
> > <asp:ListItem Selected ="True">Mailboxes</asp:ListItem>
> > </aspropDownList>
> >
> >
> >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/subs...#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;
> >> >
> >>
> >>

> >

>
>

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-23-2009
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 />
<aspetailsView 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 >
<aspropDownList
OnSelectedIndexChanged="DropDownList4_SelectedInde xChanged"
ID="DropDownList4" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource4"
DataTextField="WTableType"
DataValueField="WTableType"
SelectedValue='<%# Bind("TableType") %>'>
<asp:ListItem Selected
="True">Mailboxes</asp:ListItem>
</aspropDownList>
</td>
<td>
<asp:Label ID="Label4" runat="server"
style="text-align: left"
Text=" then select Record ID">
</asp:Label>
</td>
<td>
<aspropDownList ID="DropDownList2"
runat="server"
DataTextField="BoxNumber"
DataValueField="BoxNumber"
>

</aspropDownList>
</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>

</aspetailsView>
<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>
<asparameter Name="WebAccountID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="TypeID" Type="String" />
<asparameter Name="WebAccountID" Type="Int32" />
<asparameter Name="TableType" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="WebAccountName"
QueryStringField="User"
Type="String" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter ControlID="Label1" Name="WebAccountName"
PropertyName="Text" Type="String" />
<asparameter Name="TypeID" Type="String" />
<asparameter Name="TableType" />
</InsertParameters>
</asp:SqlDataSource>

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&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;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&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;&nbs p;&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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
binding datasource to dropdownlist control DesignerX ASP .Net 5 12-29-2008 10:57 PM
Building the datasource for DropDownList Corobori ASP .Net 3 02-20-2005 02:32 PM
DropDownList: possible to add new rows after the DataSource has been set? Jim Bancroft ASP .Net 4 01-06-2005 08:11 AM
How dynamically set DropDownList datasource when in DataGrid? VB Programmer ASP .Net 0 04-07-2004 02:43 PM
Changing datasource of dropdownlist Julie Barnet ASP .Net 0 08-20-2003 05:09 PM



Advertisments