what's wrong?i need your help.

  • Thread starter ÀÏÆÅ»³ÔÐ5¸öÔÂ
  • Start date
À

ÀÏÆÅ»³ÔÐ5¸öÔÂ

I want use dropdownlist contral in gridview
but have trouble now
mycode here:
i'm very sorry for my poor english

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSourceclass"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="specialityid"
SortExpression="specialityid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:TemplateField HeaderText="masterid"
SortExpression="masterid">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSourceteacher"
DataTextField="realname" DataValueField="id"
SelectedValue='<%# Eval("masterid") %>'>
</asp:DropDownList><asp:SqlDataSource
ID="SqlDataSourceteacher" runat="server" ConnectionString="<%$
ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [id], [realname] FROM
[teacher]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#
Bind("masterid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("masterid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomnum" HeaderText="roomnum"
SortExpression="roomnum" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceclass" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [class] WHERE [id] = @id"
InsertCommand="INSERT INTO [class] ([id], [specialityid], [name],
[masterid], [roomnum]) VALUES (@id, @specialityid, @name, @masterid,
@roomnum)"
SelectCommand="SELECT [id], [specialityid], [name], [masterid],
[roomnum] FROM [class]"
UpdateCommand="UPDATE [class] SET [specialityid] =
@specialityid, [name] = @name, [masterid] = @masterid, [roomnum] = @roomnum
WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
<asp:parameter Name="id" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="String" />
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

error blew

Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a
SelectedValue which is invalid because it does not exist in the list of
items.
Parameter name: value

Source Error:


An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.


Stack Trace:


[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is
invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +976
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3174
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable
data) +31
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.DetailsView.DataBind() +24
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.DetailsView.EnsureDataBound() +198
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
+33
System.Web.UI.WebControls.DetailsView.OnPreRender(EventArgs e) +62
System.Web.UI.Control.PreRenderRecursiveInternal() +148
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint
 
K

Karl Seguin

It simply sounds like the Eval("MasterId") is returning a value not in the
dropdownlist. Is that possible? Could some of your records possibly have a
null value or some other default or unknown value?

You should try to figure out which MasterId is screwing it up. Also, you
might wanna hook some of this in codebehid, it would be easier to debug.

Karl

--

MY ASP.Net tutorials
http://www.openmymind.net/



ÀÏÆÅ»³ÔÐ5¸öÔ said:
I want use dropdownlist contral in gridview
but have trouble now
mycode here:
i'm very sorry for my poor english

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSourceclass"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="specialityid"
SortExpression="specialityid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:TemplateField HeaderText="masterid"
SortExpression="masterid">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSourceteacher"
DataTextField="realname" DataValueField="id"
SelectedValue='<%# Eval("masterid") %>'>
</asp:DropDownList><asp:SqlDataSource
ID="SqlDataSourceteacher" runat="server" ConnectionString="<%$
ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [id], [realname] FROM
[teacher]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#
Bind("masterid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("masterid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomnum" HeaderText="roomnum"
SortExpression="roomnum" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceclass" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [class] WHERE [id] = @id"
InsertCommand="INSERT INTO [class] ([id], [specialityid], [name],
[masterid], [roomnum]) VALUES (@id, @specialityid, @name, @masterid,
@roomnum)"
SelectCommand="SELECT [id], [specialityid], [name], [masterid],
[roomnum] FROM [class]"
UpdateCommand="UPDATE [class] SET [specialityid] =
@specialityid, [name] = @name, [masterid] = @masterid, [roomnum] =
@roomnum WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
<asp:parameter Name="id" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="String" />
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

error blew

Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has
a SelectedValue which is invalid because it does not exist in the list of
items.
Parameter name: value

Source Error:


An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.


Stack Trace:


[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is
invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +976
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3174

System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable
data) +31

System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.DetailsView.DataBind() +24
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.DetailsView.EnsureDataBound() +198
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
+33
System.Web.UI.WebControls.DetailsView.OnPreRender(EventArgs e) +62
System.Web.UI.Control.PreRenderRecursiveInternal() +148
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint
 
À

ÀÏÆÅ»³ÔÐ5¸öÔÂ

thank u,karl
your analyse is very exactly.
but the question is that how can i deel with the unknow value or null value?

thanks again.
Karl Seguin said:
It simply sounds like the Eval("MasterId") is returning a value not in the
dropdownlist. Is that possible? Could some of your records possibly have
a null value or some other default or unknown value?

You should try to figure out which MasterId is screwing it up. Also, you
might wanna hook some of this in codebehid, it would be easier to debug.

Karl

--

MY ASP.Net tutorials
http://www.openmymind.net/



ÀÏÆÅ»³ÔÐ5¸öÔ said:
I want use dropdownlist contral in gridview
but have trouble now
mycode here:
i'm very sorry for my poor english

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSourceclass"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="specialityid"
SortExpression="specialityid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("specialityid") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("specialityid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:TemplateField HeaderText="masterid"
SortExpression="masterid">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" DataSourceID="SqlDataSourceteacher"
DataTextField="realname" DataValueField="id"
SelectedValue='<%# Eval("masterid") %>'>
</asp:DropDownList><asp:SqlDataSource
ID="SqlDataSourceteacher" runat="server" ConnectionString="<%$
ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [id], [realname] FROM
[teacher]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("masterid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("masterid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomnum" HeaderText="roomnum"
SortExpression="roomnum" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceclass" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [class] WHERE [id] = @id"
InsertCommand="INSERT INTO [class] ([id], [specialityid], [name],
[masterid], [roomnum]) VALUES (@id, @specialityid, @name, @masterid,
@roomnum)"
SelectCommand="SELECT [id], [specialityid], [name],
[masterid], [roomnum] FROM [class]"
UpdateCommand="UPDATE [class] SET [specialityid] =
@specialityid, [name] = @name, [masterid] = @masterid, [roomnum] =
@roomnum WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
<asp:parameter Name="id" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="String" />
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

error blew

Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1'
has a SelectedValue which is invalid because it does not exist in the
list of items.
Parameter name: value

Source Error:


An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.


Stack Trace:


[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which
is invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +976
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3174

System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable
data) +31

System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments
arguments, DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.DetailsView.DataBind() +24
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.DetailsView.EnsureDataBound() +198
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
+33
System.Web.UI.WebControls.DetailsView.OnPreRender(EventArgs e) +62
System.Web.UI.Control.PreRenderRecursiveInternal() +148
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint
 
K

Karl Seguin

You need to do conditional binding. Giving your scenario, I think your only
solution is to hook into the OnItemDataBound event. You can learn more at:
http://openmymind.net/index.aspx?documentId=8#4.2

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


ÀÏÆÅ»³ÔÐ5¸öÔ said:
thank u,karl
your analyse is very exactly.
but the question is that how can i deel with the unknow value or null
value?

thanks again.
Karl Seguin said:
It simply sounds like the Eval("MasterId") is returning a value not in
the dropdownlist. Is that possible? Could some of your records possibly
have a null value or some other default or unknown value?

You should try to figure out which MasterId is screwing it up. Also, you
might wanna hook some of this in codebehid, it would be easier to debug.

Karl

--

MY ASP.Net tutorials
http://www.openmymind.net/



ÀÏÆÅ»³ÔÐ5¸öÔ said:
I want use dropdownlist contral in gridview
but have trouble now
mycode here:
i'm very sorry for my poor english

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="SqlDataSourceclass"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="specialityid"
SortExpression="specialityid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("specialityid") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("specialityid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("specialityid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:TemplateField HeaderText="masterid"
SortExpression="masterid">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" DataSourceID="SqlDataSourceteacher"
DataTextField="realname" DataValueField="id"
SelectedValue='<%# Eval("masterid") %>'>
</asp:DropDownList><asp:SqlDataSource
ID="SqlDataSourceteacher" runat="server" ConnectionString="<%$
ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [id], [realname] FROM
[teacher]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("masterid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("masterid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roomnum" HeaderText="roomnum"
SortExpression="roomnum" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceclass" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [class] WHERE [id] = @id"
InsertCommand="INSERT INTO [class] ([id], [specialityid], [name],
[masterid], [roomnum]) VALUES (@id, @specialityid, @name, @masterid,
@roomnum)"
SelectCommand="SELECT [id], [specialityid], [name],
[masterid], [roomnum] FROM [class]"
UpdateCommand="UPDATE [class] SET [specialityid] =
@specialityid, [name] = @name, [masterid] = @masterid, [roomnum] =
@roomnum WHERE [id] = @id">
<DeleteParameters>
<asp:parameter Name="id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
<asp:parameter Name="id" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="id" Type="String" />
<asp:parameter Name="specialityid" Type="String" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="masterid" Type="String" />
<asp:parameter Name="roomnum" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

error blew

Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1'
has a SelectedValue which is invalid because it does not exist in the
list of items.
Parameter name: value

Source Error:


An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.


Stack Trace:


[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which
is invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +976
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3174

System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable
data) +31

System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments
arguments, DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.DetailsView.DataBind() +24
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.DetailsView.EnsureDataBound() +198
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs
e) +33
System.Web.UI.WebControls.DetailsView.OnPreRender(EventArgs e) +62
System.Web.UI.Control.PreRenderRecursiveInternal() +148
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint
 
À

ÀÏÆÅ»³ÔÐ6¸öÔÂ

hi Karl:
the OnItemDataBound event is valid in asp.net 1.1
which event can i hook into when my application is running on asp.net 2.0

thanks
 
À

ÀÏÆÅ»³ÔÐ6¸öÔÂ

thank u,karl
yes this even is work on the plantform .net2.0
but it does not work on gridview contral
it only work on gridview,isn't it?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top