icyer() 老大快来看看

baitianhai 2002-05-29 01:40:27
我用你的方法还是有问题
------------------------aspx程序
<asp:DropDownList id="DropDownList1" Runat="server"></asp:DropDownList>
------------------------vb程序
Private Sub DataList1_EditCommand()
Dim drop As DropDownList
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
drop.Items.Add("asdfasd")
------------------------------------
报错:


未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 67: drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
行 68: drop.Items.Add("asdfasd")
行 69: DataList1.EditItemIndex = e.Item.ItemIndex


68行错误



...全文
31 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
老大,你真是厉害,我解决了,太感谢了,能和你交个朋友吗?
你的QQ,mail能留给我吗?
我的是22329109
baichenhong@the9.com
icyer 2002-05-29
  • 打赏
  • 举报
回复
Sorry,刚才我没测试,你可以这样写:

DataList1.EditItemIndex = e.Item.ItemIndex
data_grid() '数据梆定的函数
Dim drop As DropDownList
drop = CType(DataList1.Items(e.Item.ItemIndex).FindControl("DropDownList1"), DropDownList)
drop.Items.Add("asdfasd")

以上代码我测试通过。
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
icyer() 真的很感谢,可是我按照你的去做,还是提示
drop.Items.Add("asdfasd")这行
System.NullReferenceException: 未将对象引用设置到对象的实例。
我不知道该说什么好了
------------------------------aspx文件
<asp:DataList id="DataList1" runat="server">
<EditItemStyle></EditItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<EditItemTemplate>
<table>
<tr width="300px">
<td>
<asp:LinkButton id="Linkbutton2" style="FONT-SIZE: 12px" Text="保存" CommandName="update" runat="server" /></asp:LinkButton></td>
<td>
<asp:LinkButton id="Linkbutton1" style="FONT-SIZE: 12px" Text="取消" CommandName="cancel" runat="server" /></asp:LinkButton></td>
</tr>
<tr width="300px" style="FONT-SIZE: 12px">
<td width="150px">
公司:
<asp:DropDownList id="DropDownList1" Runat="server"></asp:DropDownList><br>
<asp:TextBox id="Text1" Text='<%# DataBinder.Eval(Container, "DataItem.公司") %>' Runat=server Width="80px">
</asp:TextBox></td>
----------------------------------vb文件
Private Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
data_grid() '数据梆定的函数
Dim drop As DropDownList
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
drop.Items.Add("asdfasd")
End Sub


icyer 2002-05-29
  • 打赏
  • 举报
回复
DataList1.EditItemIndex = e.Item.ItemIndex
DataList1.DataBind()
Dim drop As DropDownList
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
drop.Items.Add("asdfasd")
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
我按你们说的做了还是有问题的,我该怎么办那????????
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
两位老大我还是有问题
我已经在EditCommand中写了
DataList1.EditItemIndex = e.Item.ItemIndex
可是错误还是一样的,我该怎么办那?
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
楼上的2位老大,真是万分感谢,可是你们看
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

Private Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
行 67: DataList1.EditItemIndex = e.Item.ItemIndex
行 68: drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
行 69: drop.Items.Add("asdfasd")
行 70: 'dropdownlist2 = e.Item.FindControl("DropDownList1")


yxrj 2002-05-29
  • 打赏
  • 举报
回复
Private Sub DataList1_EditCommand()
Dim drop As DropDownList
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
drop.Items.Add("asdfasd")

虽然你没完全写清楚
但一般人的做法是在edit模板里放dropdownlist
EditCommand刚执行的时候,dropdownlist应该是没有的
所以应该先设EditIndex
再绑定
最后再找dropdownlist
icyer 2002-05-29
  • 打赏
  • 举报
回复
那你先要把这一行设置成Edit行。
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
然后再取DropDownList控件。
icyer 2002-05-29
  • 打赏
  • 举报
回复
那你先要把这一行设置成Edit行。
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
然后再取DropDownList控件。
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
e有Items这个属性
我是放在这里的
<asp:DataList id="DataList1" runat="server">
<EditItemStyle></EditItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<EditItemTemplate>
<table>
<tr width="300px">
<td>
<asp:LinkButton id="Linkbutton2" style="FONT-SIZE: 12px" Text="保存" CommandName="update" runat="server" /></asp:LinkButton></td>
<td>
<asp:LinkButton id="Linkbutton1" style="FONT-SIZE: 12px" Text="取消" CommandName="cancel" runat="server" /></asp:LinkButton></td>
</tr>
<tr width="300px" style="FONT-SIZE: 12px">
<td width="150px">
公司:
<asp:DropDownList id="DropDownList1" Runat="server"></asp:DropDownList><br>
icyer 2002-05-29
  • 打赏
  • 举报
回复
e有Items这个属性吗?
你的DropDownList1是放在<ItemTemplate>...</ItemTemplate>中还是在<EditItemTemplate>..</EditItemTemplate>中?当前行是Edit行吗?
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
那我怎么才能找到这个i那?帮忙了各位!
triout 2002-05-29
  • 打赏
  • 举报
回复
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
修改为:
drop = CType(e.Items[i].FindControl("DropDownList1"), DropDownList)
这里的i值需要根据你触发事件的E来获取。
baitianhai 2002-05-29
  • 打赏
  • 举报
回复
老大,我还是有问题,你能给一个相应的例子吗?万分感谢!
icyer 2002-05-29
  • 打赏
  • 举报
回复
Dim drop As DropDownList
drop = CType(e.Item.FindControl("DropDownList1"), DropDownList)
If Not drop Is Nothing Then
drop.Items.Add("asdfasd")
End If
另外,如果当前行是Edit状态的话,就只显示<EditItemTemplate>..</EditItemTemplate>中的内容,否则就显示<ItemTemplate>...</ItemTemplate>中的内容。所以,你需要确认当前行的状态以及DropDownList在那种Template里面。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧