hello everyone.
can any one tell me how to delete recored from gired view dynamically.
in my program i have one gridview and sqlDatasource in this i have 4 label and one button.Label shows name, date, title and description and button for delete.
all this is done at source view through adding 2 template field and this template field contain label and button. it show recordes as i want.
but the problem is now i want to delete perticuler name and description from the gridview so i had created button in templte field.how to do it can any one sugest me what to do.
thoiis is my query.

Select t1.Id,t1.Name,t1.Date,t1.Description,t2.Body,t2.optionId,t2.AdDate,t2.AdedBy from tabel1 t1 full outer join table2 t2 ON t1.Id=T2.Id

this is my code of gridview

<asp:GridView ID="GridView1"  runat="server" Height="139px" Width="562px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" HorizontalAlign="Justify" BackColor="LightGoldenrodYellow" BorderColor="Black" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" BorderStyle="Solid">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Literal ID="Literal6" runat="server" Text='<%# Eval("UserDt", "{0:D}<br/><br/>{0:T}") %>'></asp:Literal><br />
                                <hr style="width: 100px; height: 1px" />
                                <asp:Literal ID="Literal7" runat="server" Text='<%# Eval("UserAd", "{0}") %>'></asp:Literal><br />
                                <br />
                                <asp:Button ID="Button2" runat="server" Text="Delete" OnClientClick="if (Confirm('Are you sure you want to delet this{0}?'==false) return false;" />
                            </ItemTemplate>
                            <ItemTemplate>
                        <div class="title">
                            &nbsp;<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Title", "{0}") %>'></asp:Literal>
                            <br />
                            <br />
                                <asp:Literal ID="Literal5" runat="server" Text='<%# Eval("Body", "{0}") %>'></asp:Literal><br/>
                            &nbsp;</div>
</ItemTemplate>                  
                      <FooterStyle BackColor="Tan" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                </asp:GridView>

here i'm trying to write some code for delete but i don't know what to write.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim id As Integer
            id = GridView1.Rows.Count
            If id = 1 Then
                Exit Sub
            End If
            Dim btn As Button = e.Row.FindControl("button1")
            btn.OnClientClick=String.Format(btn.OnClientClick,
        End If
    End Sub

sorry for my poor english and for this tuck of code but sgest me what to do.

Dani AI

Generated

Brief summary and practical fixes

The error and symptoms in the replies point to two separate issues: your GridView markup and how the delete argument is produced for postback. the TemplateField snippet in your first post looked malformed (there were two ItemTemplate blocks inside the same TemplateField). Fixing that removes a common source of weird postback/validation problems.

Recommended, robust approach

  • Put the primary key (Id) into the GridView DataKeys (set DataKeyNames). That keeps the key on the server and avoids fragile client-side string formatting.
  • Use a Button or LinkButton with a CommandName (for example "DeleteRow") and a CommandArgument that holds the row index. Handle GridView.RowCommand on the server, read the DataKeys for that row index, and perform a parameterized delete against the database. This avoids relying on client-side arguments that ASP.NET did not register at render time.

Example (VB.NET) showing how to get the id from DataKeys in RowCommand:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "DeleteRow" Then
        Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
        Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(rowIndex).Value)
        ' call your delete routine or SqlDataSource.Delete with this id
    End If
End Sub

About the "invalid postback" message
That message means the postback argument was not one of the values ASP.NET expected when the page was rendered. Common causes: malformed OnClientClick/javascript that changes the postback data, building CommandArgument client‑side, or not recreating dynamic controls exactly the same on postback. Do not immediately turn off event validation. Instead:

  • Use server-generated arguments (DataKeys) or
  • If you must emit additional postback values, register them on the server with ClientScript.RegisterForEventValidation during PreRender.

Quick troubleshooting

  • Inspect the rendered button HTML with browser dev tools to see the actual onclick/__doPostBack call.
  • Simplify OnClientClick to a plain return confirm(...) while debugging.
  • Ensure GridView is rebound appropriately after delete and that TemplateField markup is valid.

Recommended Answers

All 3 Replies

Hi,
Do you want to delete record from DB after pressing the delete button.? If yes then you can try the following.
1 ) Add appropriate buttons in your aspx page.

<asp:TemplateField>
<ItemTemplate>
	<asp:Button ID="btnDelete" Text="Delete" CommandArgument=''<%#DataBinder.Eval(container,"dataitem.ID")%>'' CommandName="DELETECURRENTROW" OnClientClick="return confirm('Are you sure you want to delete this record?')" runat="server" />
</ItemTemplate> 
							                </asp:TemplateField>

2) Inside your .vb file you should now handle the proper action in following way.

Protected Sub gvTest_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTest.RowCommand
            Dim ID As Integer = CInt(e.CommandArgument)
            Select Case e.CommandName
                Case "DELETECURRENTROW"
'Execute your Sql for Delete operation. The Record is deleted based on ID generated above.
                    ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "STATUS_MESSAGE", "alert('Record deleted for ID:" & ID & "');", True)
                    'Function BindYourGridView()
            End Select
        End Sub

tahnk for repaly when i run the page and click on delete button i got this error

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Well, There is a script running in the above code. Either set enableEventValidation=false in your aspx page header or remove the script from file.

Also mark this thread as solved when you have your answer. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.