code sample aspx vb behind code:
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each row As GridViewRow In GridView1.Rows
Dim checkbox As CheckBox = CType(row.FindControl("chkdelete"), CheckBox)
If checkbox.Checked Then
Dim ID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
SqlDataSource1.DeleteParameters("ID").DefaultValue = ID.ToString()
SqlDataSource1.Delete()
End If
Next row
front code :
`
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="No"
SortExpression="ID"/>
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name"/>
<asp:CommandField Headertext="Edit" ShowEditButton="True" />
<asp:CommandField Headertext="Delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click"/>`
my question is, why my multiple delete still wont work? whats wrong with my code? anyone pls help..
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each row As GridViewRow In GridView1.Rows
Dim checkbox As CheckBox = CType(row.FindControl("chkdelete"), CheckBox)
If checkbox.Checked Then
Dim ID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
Delete(ID)
End If
Next row
End Sub
Public Function Delete(ByRef ID as Integer)
' Your Connection Coding comes here
SqlDataSource1.DeleteParameters("ID").DefaultValue = ID.ToString()
SqlDataSource1.Delete()
End Function
Try This Way..!!
Related
I'm having this issue on displaying data into gridview. I want to display data from BindData into a custom made gridview as below. I have already tried
write the code on same page as html > working(but my senior engineer does not want that), so now I need to redo it by use back-end code and pass those (ID,Name,Age) into those label ID using dataset. how can I accomplished this?
<asp:GridView ID="MyGridView" runat="server" CellPadding="5" CssClass="ControlStyle"
ForeColor="#333333" Font-Size="8px" GridLines="Vertical" width="40%" CaptionAlign="Left"
PageSize="15" AllowPaging="True" OnPageIndexChanging="MyData_OnPageIndexChanging"
AutoGenerateColumns="False" enableEventValidation="false">
<Columns >
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="img" runat="server" ImageUrl="~/Images/arrow-2a.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lbl_Age" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
and I want to display below data.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindData()
End If
End Sub
Private Sub BindData()
Dim sCon As String = "MyConnectionString"
Using con As New SqlConnection(sCon)
Using cmd As New SqlCommand(" select * from users ")
Dim sda As New SqlDataAdapter()
Try
cmd.Connection = con : con.Open()
sda.SelectCommand = cmd
Dim dt As New DataTable
sda.Fill(dt)
'BIND DATABASE WITH THE GRIDVIEW.
MyGridView.DataSource = dt
MyGridView.DataBind()
Catch ex As Exception
txtMsg.Text = ex.Message
End Try
End Using
End Using
End Sub
with above code, my page show nothing, just white canvas, where did I do wrong? please guide.
thanks
Thanks for your kind effort, I've manage to solve this issue by modified my code from this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindData()
End If
End Sub
into this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindData()
End If
End Sub
I still don't know whats the different between those two but I've manage to get my results as expected.
I have a gridView with a textBox inside a templateField. I wan to extract the text of the textBox if a checkbox is marked in the row.
I have the gridView defined as follows
<asp:GridView ID="GV_Comments" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SQL_Comments">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="Comment_Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:TemplateField HeaderText="comment" SortExpression="comment">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("comment") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="CommentForPeriod" runat="server" Text='<%# Bind("comment") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="B_Load" runat="server" Text="Transfer Selection" onclick="B_Load_Click" />
<br />
<asp:TextBox ID="CompiledText" runat="server" Width="662px" Rows="10"
TextMode="MultiLine"></asp:TextBox>
And the code as follows
Protected Sub B_Load_Click(ByVal sender As Object, ByVal e As EventArgs) '(sender As Object, e As System.EventArgs) Handles B_Load.Click
Dim FullText As String = ""
For Each row As GridViewRow In GV_Comments.Rows
Dim CB_Control As CheckBox = CType(row.FindControl("Comment_Select"), CheckBox)
Dim Txt_Control As TextBox = CType(row.FindControl("CommentForPeriod"), TextBox)
If CB_Control IsNot Nothing AndAlso CB_Control.Checked AndAlso Txt_Control IsNot Nothing Then
FullText = FullText & Txt_Control.Text & "<br/>"
End If
Next row
CompiledText.Text = FullText.ToString
End Sub
When I debug the code I can see that the Checkbox control is found but not the TextBox control. Would anyone see why?
You can't do like this. When you click the button: B_Load, then GridView is NOT in Edit mode. And this is why you can't get the TextBox, which is in EditItemTemplate.
You can only get the controls inside <ItemTemplate> in your button click as gridview is in Normal display Mode. <EditItemTemplate> controls are rendered only when GridView enters Edit mode.
So, you need to get the value of the Label: Label1 here actually, which has the same value and is inside <ItemTemplate> .
Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)
// button click as usual, just get and check the value of Label control, rather than TextBox control.
Protected Sub B_Load_Click(ByVal sender As Object, ByVal e As EventArgs) '(sender As
Object, e As System.EventArgs) Handles B_Load.Click
Dim FullText As String = ""
For Each row As GridViewRow In GV_Comments.Rows
Dim CB_Control As CheckBox = CType(row.FindControl("Comment_Select"),
CheckBox)
Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)
If CB_Control IsNot Nothing AndAlso CB_Control.Checked AndAlso Lbl_Control
IsNot Nothing Then
FullText = FullText & Lbl_Control.Text & "<br/>"
End If
Next row
CompiledText.Text = FullText.ToString
End Sub
When pressing edit button in gridview, edit template is displayed only after 2 clicks.
And another problem: Value of the field to edit is displayed in gridview initially, but not in edit template.
Asp code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow"
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">
<Columns>
<asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True"
SortExpression="AREA" />
<asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
<ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit"
ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
vb code:
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
'If Not IsPostBack Then
'GridView1.DataSourceID = SqlDataSource1.ID
'GridView1.DataBind()
'End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SqlDataSource1.SelectCommand = "SQL"
SqlDataSource1.ConnectionString = "My conn string"
If Not IsPostBack Then
'GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
End If
End Sub
I think you need to call DataBind to rebind the data source. You've commented it out, but it looks like you had it in a IsPostBack block, which would only execute on the initial page load.
Try:
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
End Sub
i am trying to total the price and quantity of all the products added to the gridview and i can't seem to figure out why the total is not showing in the footer. the code that i have for the vb should multiply the quantity with the price and put it into the footer of the gridview. the gridview footer is visible so i know that's not the problem. any help would be appreciated.
asp:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="Cart" AllowSorting="True" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Vertical" ShowFooter="True" AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True" DataKeyNames="cartID">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:BoundField DataField="cartID" HeaderText="cartID" SortExpression="cartID"
InsertVisible="False" ReadOnly="True" Visible="False"></asp:BoundField>
<asp:BoundField DataField="cartNO" HeaderText="cartNO" SortExpression="cartNO"
Visible="False" />
<asp:BoundField DataField="productID" HeaderText="productID"
SortExpression="productID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="productName" HeaderText="productName"
SortExpression="productName" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="price" HeaderText="price"
SortExpression="price" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="quantity" HeaderText="quantity"
SortExpression="quantity" />
<asp:TemplateField HeaderText="SubTotal" SortExpression="subTotal" >
<ItemTemplate>
<%# Eval("price") * Eval("quantity")%>
</ItemTemplate>
<%-- <FooterTemplate>
<asp:Label ID="sum" runat="server"/>
</FooterTemplate>--%>
</asp:TemplateField>
VB
Public Class MyCart
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strcartNO As String = ""
Dim cookieBack As HttpCookie
cookieBack = HttpContext.Current.Request.Cookies("cartNO")
strcartNO = cookieBack.Value
'sqldscartLine.selectCommand = "Select * from cartLine where cartNO = '" & strcartNO & "'"
GridView1.DataBind()
End Sub
Public Shared Sub DeleteMethod(ByVal original_OrderID As Integer, _
ByVal original_ProductID As Integer)
End Sub
Dim priceTotal As Decimal = 0
Dim quantityTotal As Integer = 0
Sub GridView1_RowDataBound(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' add the UnitPrice and QuantityTotal to the running total variables
priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
"price"))
quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"quantity"))
ElseIf e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(2).Text = "Totals:"
' for the Footer, display the running totals
e.Row.Cells(3).Text = priceTotal.ToString("c")
e.Row.Cells(4).Text = quantityTotal.ToString("d")
e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
e.Row.Font.Bold = True
End If
End Sub
Not sure if anybody saw the comment, so will put it in an answer instead - This might help others out (if I'm correct..)
Your code has the following in
<%-- <FooterTemplate>
<asp:Label ID="sum" runat="server"/>
</FooterTemplate>--%>
The <%-- and --%> are comments, so your footer is being commented out.
Change it to
<FooterTemplate>
<asp:Label ID="sum" runat="server"/>
</FooterTemplate>
And it should display.
Databinding creates data rows. The footer isn't a data row, so the event RowDataBound is not called when it's created. Thus, your event handler GridView1_RowDataBound will never execute the code that generates the totals, since the expression
e.Row.RowType = DataControlRowType.Footer
... will never be true during any execution of that method.
Try handling the RowCreated event instead, like so:
Sub GridView1_RowCreated(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(2).Text = "Totals:"
' for the Footer, display the running totals
e.Row.Cells(3).Text = priceTotal.ToString("c")
e.Row.Cells(4).Text = quantityTotal.ToString("d")
e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
e.Row.Font.Bold = True
End If
End Sub
We have a GridView with a 2 buttons. One of the buttons is a select button and the other is a one without a command. It is supposed to activate an OnClick sub routine. The sub routine is not executing.
Here is the markup of the GridView with the buttons:
<asp:GridView
ID="GridViewParentsSummary"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
>
<Columns>
<asp:BoundField
DataField="ID"
HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False" />
<asp:BoundField
DataField="FatherName"
HeaderText="FatherName"
SortExpression="FatherName" />
<asp:BoundField DataField="MotherName" HeaderText="MotherName"
SortExpression="MotherName" />
<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="Select Details" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button
ID="ButtonNewPersonToReleaseChildren"
runat="server"
CausesValidation="false"
Text="New Person To Release Children"
CommandArgument='<%# Eval("ID") %>'
OnClick="NewPersonToReleaseChildren" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is the VB.Net code-behind coding with the sub routines for the buttons:
Protected Sub GridViewParentsSummary_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewParentsSummary.SelectedIndexChanged
IntParentsID = GridViewParentsSummary.DataKeys(GridViewParentsSummary.SelectedIndex).Value
Response.Redirect("AuthorizationForChildReleaseDetails.aspx")
End Sub
Protected Sub NewPersonToReleaseChildren(sender As Object, e As EventArgs)
blnAddModeIsSelected = True
MsgBox("The button was clicked.")
Response.Redirect("AuthorizationForChildReleaseDetails.aspx")
End Sub
I'm sure I am missing some coding but don't know what that could be because the sub routine for the Select button works, but not the sub routine for NewPersonToReleaseChildren.
in gridview:
<asp:TemplateField HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ImgBtnDel" runat="server" ImageUrl="~/Images/icon-delete.gif" CommandName="del"
CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
use aspButton or Imagebutton in gridview,in code behind:
Protected Sub gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridview1.RowCommand
Dim myId As String = e.CommandArgument.ToString
If e.CommandName = "del" Then
ElseIf e.CommandName = "upd" Then
End If
End Sub
Add Handles Handles NewPersonToReleaseChildren.Click to your handler
Protected Sub NewPersonToReleaseChildren(sender As Object, e As EventArgs) Handles NewPersonToReleaseChildren.Click
blnAddModeIsSelected = True
//MsgBox("The button was clicked.")
Response.Redirect("AuthorizationForChildReleaseDetails.aspx")
End Sub
I don't think there is anything like MsgBox in asp.net. If the Handles does not work, replace that line with Throw New Exception("My Button was called") or put a break point on that line and press F5