Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
cindy #1
datagrid can't get value to do insert
I am trying to get the value out of a datagrid please help
Getting error
Object reference not set to an instance of an object.
at line
newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
data binds and displays fine
I enter value in textbox for comment
click the insert button
it goes to item command fine starts the foreach and errors
here is code below is grid html
private void dgComments_ItemCommand(object source, private void
dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
SearchDAL dal= new SearchDAL();
foreach(DataGridItem item in dgComments.Items)
{
if(!item.ItemType.ToString().Equals("EditItem"))
{
newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
dal.InsertComment(newComment);
}
dgComments.DataBind();
}
}
}
datagrid html
<asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="CommentID">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.CommentID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="LinkButton1" runat="server"
CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
<EditItemTemplate>
<asp:Label id=lblCommentID runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.CommentID") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Comment">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.comment") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtComment" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
--
cindy
cindy Guest
-
Insert progress bar into datagrid?
Hi, I have problem in displaying each progress bar value on it. I create the progress bar by using table. When i run, table just like... -
How to insert a row in a DataGrid in code?
I thought I could insert a new row in a DataGrid in code by calling CurrencyManager.AddNew; but it does not work. I want to open a new row that... -
Insert row in DataGrid
Hi I would like to know how to insert a row into a datagrid? i allready know how to edit, delete and update a row... -
Extending datagrid with new/insert button
Hi, I try to develop a server control which inherits from DataGrid and adds a new/insert button to this grid. This is no problem, but I can't get... -
Add (INSERT) a New Row Using the ASP.Net Datagrid Control...
I found some good information On "Add (INSERT) a New Row Using the ASP.Net Datagrid Control..." at this site... -
Steven Cheng[MSFT] #2
RE: datagrid can't get value to do insert
Hi Cindy,
Welcome to ASPNET newsgroup.
Regarding on the problem you mentioned, from the code you provided, I think
the cause is the DataGrid.Items collection which dosn't contains the Footer
Item. The DataGrid.Items collection ony contains the
Item/AlternateItem/Edit.. rows.
For your scenario, you'd like to retrieve the control instance from the
Footer template, you can make use of the
DAtaGridCommandArgs paramter passed in the ItemCommand event and use its
"Item" property to get the FooterRow.
For example, the following code just retrieve a textbox instance from
footer template in itemcommand event:
=====================
private void dgFooter_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName != "Insert")
return;
if(e.Item.ItemType == ListItemType.Footer)
{
TextBox txt = e.Item.FindControl("txtComment") as TextBox;
.....
}
}
===========================
Hope helps. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! [url]www.microsoft.com/security[/url]
--------------------
| Thread-Topic: datagrid can't get value to do insert
| thread-index: AcXOvn/4cY+arEbkRUuWvyIvOAQ9TQ==
| X-WBNR-Posting-Host: 71.136.160.120
| From: "=?Utf-8?B?Y2luZHk=?=" <cmello@nospam.nospam>
| Subject: datagrid can't get value to do insert
| Date: Tue, 11 Oct 2005 16:50:03 -0700
| Lines: 70
| Message-ID: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14477
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| I am trying to get the value out of a datagrid please help
| Getting error
| Object reference not set to an instance of an object.
| at line
| newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
|
|
| data binds and displays fine
| I enter value in textbox for comment
| click the insert button
| it goes to item command fine starts the foreach and errors
|
| here is code below is grid html
|
| private void dgComments_ItemCommand(object source, private void
| dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
| {
| if (e.CommandName == "Insert")
| {
| SearchDAL dal= new SearchDAL();
| foreach(DataGridItem item in dgComments.Items)
| {
| if(!item.ItemType.ToString().Equals("EditItem"))
| {
| newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
| dal.InsertComment(newComment);
| }
| dgComments.DataBind();
| }
| }
| }
|
|
| datagrid html
| <asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
| AutoGenerateColumns="False">
| <Columns>
| <asp:TemplateColumn HeaderText="CommentID">
| <ItemTemplate>
| <asp:Label id=Label3 runat="server" Text='<%#
DataBinder.Eval(Container,
| "DataItem.CommentID") %>'>
| </asp:Label>
| </ItemTemplate>
| <FooterTemplate>
| <asp:LinkButton id="LinkButton1" runat="server"
| CommandName="Insert">Insert</asp:LinkButton>
| </FooterTemplate>
| <EditItemTemplate>
| <asp:Label id=lblCommentID runat="server" Text='<%#
| DataBinder.Eval(Container, "DataItem.CommentID") %>'>
| </asp:Label>
| </EditItemTemplate>
| </asp:TemplateColumn>
| <asp:TemplateColumn HeaderText="Comment">
| <ItemTemplate>
| <asp:Label id=Label2 runat="server" Text='<%#
DataBinder.Eval(Container,
| "DataItem.comment") %>'>
| </asp:Label>
| </ItemTemplate>
| <FooterTemplate>
| <asp:TextBox id="txtComment" runat="server"></asp:TextBox>
| </FooterTemplate>
| <EditItemTemplate>
| <asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
| </EditItemTemplate>
| </asp:TemplateColumn>
| </Columns>
| </asp:DataGrid>
| --
| cindy
|
Steven Cheng[MSFT] Guest
-
Steven Cheng[MSFT] #3
RE: datagrid can't get value to do insert
Hi Cindy,
How are you doing on this issue, does the suggestions in my last reply
helps a little? If there're anything else we can help, please feel free to
post here.
Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! [url]www.microsoft.com/security[/url]
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 65148547
| References: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: [email]stcheng@online.microsoft.com[/email] (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Wed, 12 Oct 2005 03:23:46 GMT
| Subject: RE: datagrid can't get value to do insert
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| Message-ID: <LPvqlxtzFHA.3712@TK2MSFTNGXA02.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| Lines: 109
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14478
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Cindy,
|
| Welcome to ASPNET newsgroup.
| Regarding on the problem you mentioned, from the code you provided, I
think
| the cause is the DataGrid.Items collection which dosn't contains the
Footer
| Item. The DataGrid.Items collection ony contains the
| Item/AlternateItem/Edit.. rows.
| For your scenario, you'd like to retrieve the control instance from the
| Footer template, you can make use of the
| DAtaGridCommandArgs paramter passed in the ItemCommand event and use its
| "Item" property to get the FooterRow.
| For example, the following code just retrieve a textbox instance from
| footer template in itemcommand event:
|
| =====================
| private void dgFooter_ItemCommand(object source,
| System.Web.UI.WebControls.DataGridCommandEventArgs e)
| {
| if(e.CommandName != "Insert")
| return;
| if(e.Item.ItemType == ListItemType.Footer)
| {
| TextBox txt = e.Item.FindControl("txtComment") as TextBox;
|
| .....
| }
| }
| ===========================
|
| Hope helps. Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! [url]www.microsoft.com/security[/url]
|
| --------------------
| | Thread-Topic: datagrid can't get value to do insert
| | thread-index: AcXOvn/4cY+arEbkRUuWvyIvOAQ9TQ==
| | X-WBNR-Posting-Host: 71.136.160.120
| | From: "=?Utf-8?B?Y2luZHk=?=" <cmello@nospam.nospam>
| | Subject: datagrid can't get value to do insert
| | Date: Tue, 11 Oct 2005 16:50:03 -0700
| | Lines: 70
| | Message-ID: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
| | MIME-Version: 1.0
| | Content-Type: text/plain;
| | charset="Utf-8"
| | Content-Transfer-Encoding: 7bit
| | X-Newsreader: Microsoft CDO for Windows 2000
| | Content-Class: urn:content-classes:message
| | Importance: normal
| | Priority: normal
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TNGXA03.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14477
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| |
| | I am trying to get the value out of a datagrid please help
| | Getting error
| | Object reference not set to an instance of an object.
| | at line
| | newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
| |
| |
| | data binds and displays fine
| | I enter value in textbox for comment
| | click the insert button
| | it goes to item command fine starts the foreach and errors
| |
| | here is code below is grid html
| |
| | private void dgComments_ItemCommand(object source, private void
| | dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
| | {
| | if (e.CommandName == "Insert")
| | {
| | SearchDAL dal= new SearchDAL();
| | foreach(DataGridItem item in dgComments.Items)
| | {
| | if(!item.ItemType.ToString().Equals("EditItem"))
| | {
| | newComment =
((TextBox)item.Cells[1].FindControl("txtComment")).Text;
| | dal.InsertComment(newComment);
| | }
| | dgComments.DataBind();
| | }
| | }
| | }
| |
| |
| | datagrid html
| | <asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
| | AutoGenerateColumns="False">
| | <Columns>
| | <asp:TemplateColumn HeaderText="CommentID">
| | <ItemTemplate>
| | <asp:Label id=Label3 runat="server" Text='<%#
| DataBinder.Eval(Container,
| | "DataItem.CommentID") %>'>
| | </asp:Label>
| | </ItemTemplate>
| | <FooterTemplate>
| | <asp:LinkButton id="LinkButton1" runat="server"
| | CommandName="Insert">Insert</asp:LinkButton>
| | </FooterTemplate>
| | <EditItemTemplate>
| | <asp:Label id=lblCommentID runat="server" Text='<%#
| | DataBinder.Eval(Container, "DataItem.CommentID") %>'>
| | </asp:Label>
| | </EditItemTemplate>
| | </asp:TemplateColumn>
| | <asp:TemplateColumn HeaderText="Comment">
| | <ItemTemplate>
| | <asp:Label id=Label2 runat="server" Text='<%#
| DataBinder.Eval(Container,
| | "DataItem.comment") %>'>
| | </asp:Label>
| | </ItemTemplate>
| | <FooterTemplate>
| | <asp:TextBox id="txtComment" runat="server"></asp:TextBox>
| | </FooterTemplate>
| | <EditItemTemplate>
| | <asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
| | </EditItemTemplate>
| | </asp:TemplateColumn>
| | </Columns>
| | </asp:DataGrid>
| | --
| | cindy
| |
|
|
Steven Cheng[MSFT] Guest



Reply With Quote

