buttoncolumn and templatecolumn in datagrid

windows

Guest
hihi... how do we get the value of databound from templatecolumn? I'm using buttoncolumn for "delete" function, but i don know how to get the value of databound in templatecolumn. I'm using templatecolumn for AncMsg, AdminID and AncDate. As i click the delete button, i want to delete the particular AncMsg, base on the AncDate as its WHERE condition. The problem is i don know how to refer to the Date value in templatecolumn.. :confused:

Codes are below:

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 102; LEFT: 2px; WIDTH: 110px; POSITION: absolute; TOP: 73px; HEIGHT: 461px" cellSpacing="1" cellPadding="1" width="123" border="0">
<TR>
<TD><asp:image id="Image1" runat="server" ImageUrl="file:///D:\My Documents\FYP Images\Category.gif"></asp:image></TD>
</TR>
</TABLE>
<asp:label id="Welcome" style="Z-INDEX: 101; LEFT: 656px; POSITION: absolute; TOP: 82px" runat="server" Font-Size="8.5pt" Font-Names="Verdana"></asp:label>
<TABLE id="Table2" style="Z-INDEX: 103; LEFT: 567px; WIDTH: 212px; POSITION: absolute; TOP: 184px" cellSpacing="1" cellPadding="1" width="300" border="0">
<TR>
<TD><asp:image id="Image2" runat="server" ImageUrl="file:///D:\My Documents\FYP Images\InfoBox.gif" Height="584px" Width="208px"></asp:image></TD>
</TR>
</TABLE>
<TABLE id="Table4" style="Z-INDEX: 104; LEFT: 131px; WIDTH: 419px; POSITION: absolute; TOP: 693px; HEIGHT: 73px" cellSpacing="1" cellPadding="1" width="419" border="0">
<TR>
<TD>
<P><asp:textbox id="AncPost" runat="server" Font-Size="8pt" Font-Names="Verdana" Height="45px" Width="414px" TextMode="MultiLine" Rows="20" Visible="False"></asp:textbox></P>
</TD>
</TR>
<TR>
<TD>
<P>
<asp:imagebutton id="PostAncButton" runat="server" ImageUrl="file:///D:\My Documents\FYP Images\Buttons\Post_button.gif" Visible="False" ToolTip="Post"></asp:imagebutton></P>
</TD>
</TR>
</TABLE>
<uc1:logo id="Logo1" runat="server"></uc1:logo><uc1:header id="Header1" runat="server"></uc1:header><uc1:footer2 id="Footer21" runat="server"></uc1:footer2><uc1:right3 id="Right31" runat="server"></uc1:right3><uc1:userrightmenu id="UserRightMenu1" runat="server"></uc1:userrightmenu><asp:datagrid id="DG1" style="Z-INDEX: 105; LEFT: 136px; POSITION: absolute; TOP: 80px" runat="server" Font-Size="8pt" Font-Names="Verdana" Height="400px" Width="412px" PageSize="5" AllowPaging="True" AllowSorting="True" BorderColor="White" CellSpacing="1" BorderWidth="1px" CellPadding="2" GridLines="Horizontal" AutoGenerateColumns="False">
<AlternatingItemStyle Font-Names="Verdana" BackColor="OldLace"></AlternatingItemStyle>
<ItemStyle Font-Names="Verdana" BackColor="LavenderBlush"></ItemStyle>
<HeaderStyle Font-Size="9pt" Font-Names="Verdana" Font-Bold="True" ForeColor="#FF9933"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemStyle Font-Names="Verdana"></ItemStyle>
<HeaderTemplate>
Announcement Board
</HeaderTemplate>
<ItemTemplate>
<TABLE id="Table9" style="FONT-SIZE: 8pt; COLOR: navy; FONT-FAMILY: Verdana; TEXT-DECORATION: none" cellSpacing="1" cellPadding="3" width="100%" border="0">
<TR>
<TD rowSpan="3"><%# Container.DataItem("AncMsg")%></TD>
<TD vAlign="top" width="25%">Posted by:</TD>
<TD vAlign="top" width="25%"><%# Container.DataItem("AdminID")%></TD>
</TR>
<TR>
<TD vAlign="top" width="25%">Date:</TD>
<TD vAlign="top" width="25%"><%# Container.DataItem("AncDate")%></TD>
</TR>
</TABLE>
</ItemTemplate>
<EditItemTemplate>
<TABLE id="Table5" cellSpacing="1" cellPadding="1" width="100%" border="0">
<TR>
<TD></TD>
<TR>
<TD>
<asp:TextBox id=txtEditAnc runat="server" Text='<%# Container.DataItem("AncMsg")%>'>
</asp:TextBox></TD>
</TR>
<TR>
<TD>
<asp:Label id=txtDate runat="server" Visible="False" Text='<%# Container.DataItem("AncDate")%>'>
</asp:Label></TD>
</TR>
</TABLE>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<ItemStyle Font-Names="Verdana"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="DeleteAnc"></asp:ButtonColumn>
</Columns>
<PagerStyle BorderWidth="2px" BorderColor="White" HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid><asp:label id="SQLStatement" style="Z-INDEX: 106; LEFT: 446px; POSITION: absolute; TOP: 50px" runat="server" Visible="False"></asp:label><asp:label id="Label1" style="Z-INDEX: 107; LEFT: 148px; POSITION: absolute; TOP: 661px" runat="server" Visible="False"></asp:label></form>
</body>




Private Sub DG1_DeleteCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG1.ItemCommand

Dim sameDate As DateTime = DG1.DataKeys(e.Item.ItemIndex)

Dim strDelete As String

If e.CommandName.Equals("DeleteAnc") Then

strDelete = "DELETE FROM Announcement WHERE AncDate = '" & sameDate & "' "
Dim cn As New SqlClient.SqlConnection("data source=CHIONG;initial catalog=

library;integrated security=SSPI;persist security info=False;workstation id=CHIONG;packet size=

4096")
Dim cm As New SqlClient.SqlCommand(strDelete, cn)
cn.Open()
cm.ExecuteNonQuery()

DG1.EditItemIndex = -1
BindData()
cn.Close()
End If


Many many thanks..I've change part of the codes..

I used an invisible label control to get the AncID, which is the primary key of the table, for the WHERE condition.. the problem now is that.. it seems like the delete button cannot work together with my editcommandcolumn and paging. If i cancel the delete function, then the rest are working fine. If I add in the cancel function, then either the editcommandcolumn or paging got problem.. :confused:

The error that i got is "Object reference not set to an instance of an object", referring to the code below >>
Dim sameAncID As String = (CType(e.Item.Cells(0).FindControl("txtAncID"), Label)).Text

Btw, i've only 1 column for my templatecolumn..

Waiting for reply... many thanks!!
 
Back
Top