兄弟姐妹们!大家快来帮帮我呀!当我在DataGrid中删除记录时,我应该怎样获得选中删除记录中的某一个栏位的值(不是Key哦)?我在DataGrid_Update 事件中用这种方法就行,可以在DataGrid_Del事件中总是报错,这是怎么回事呀?
string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
解决方案 »
- Socket与Windows认证
- access2007链接问题
- vs调试localhost 解析为了本机ip地址
- C#.NET有没有第三方的高级日历控件?有没朋友可以介绍一下。
- #]//谁能帮我把下面的代码改变串成一个console程序呢?谢谢。
- CodeSmith 在script 中如何得到Property值
- 可以用什么控件来模仿千千静听里面的‘均衡器’的那种滑竿(就是调整音色的那个窗口)
- VS2005 DataGridView应用问题
- 如何用类的关系,建立一目录树,包括节点的添加与删除,等方法
- 在同一页面内显示两个DataGrid是怎么处理分页问题?
- 急:关于发Mail的问题!
- c#下用什么将字符串(整型数或者浮点数 字符串)转化为数字?
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.NullReferenceException: Object reference not set to an instance of an object.Source Error:
Line 303:// string sCategory=(Label1)e.Item.ItemIndex.ToString ();
Line 304:// string sCategory=MyDataGrid.Items[e.Item.ItemIndex].Cells[1].Text ;
Line 305: string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
Line 306:
Line 307:// string sCategory = (string)DataBinder.Eval(e.Item.DataItem, "Ca_Name");
string sCategory=e.Item.ItemIndex.ToString ();
而且Line 304~307都要声明sCategory,去掉其前面的string
<asp:datagrid id="MyDataGrid" runat="server" Width="572px" AutoGenerateColumns="False" AllowPaging="True" AllowCustomPaging="True" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" OnDeleteCommand="MyDataGrid_Del" OnPageIndexChanged="MyDataGrid_Change" OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit" OnUpdateCommand="MyDataGrid_Update" ItemStyle-HorizontalAlign="Center" DataKeyField="ca_Code" AllowSorting="True" PageSize="7">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle HorizontalAlign="Center" ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#6699CC"></HeaderStyle>
<FooterStyle ForeColor="#EEF3FF" BackColor="White"></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<ItemStyle Wrap="False" Width="40px"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="ca_Code" SortExpression="ca_Code" ReadOnly="True" HeaderText="Code">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn SortExpression="Ca_Name" HeaderText="Category">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Ca_Name") %>' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="edit_Ca_Name" Text='<%# DataBinder.Eval(Container.DataItem, "Ca_Name") %>' MaxLength="100">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Del" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#EEF3FF" PageButtonCount="25" Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>我的Line 303,304,307,并且屏避掉了,真正起作用的是 Line 305呀!
你加一句:
if(e.Item.ItemType==ListItemType.EditItem)
{
string sCategory=((TextBox)e.Item.FindControl("edit_Ca_Name")).Text.Trim();
}
string sCategory=((Label)e.Item.FindControl("Ca_Name")).Text.Trim();