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
Related
I have this GridView:
<asp:GridView CssClass="table table-hover mt-5" ID="OrderTable" runat="server" AutoGenerateColumns="False" DataKeyNames="Identificativo" DataSourceID="DBGestioneOrdini" OnPreRender="GridView_PreRender">
<Columns>
<asp:BoundField DataField="Identificativo" HeaderText="Identificativo" InsertVisible="False" ReadOnly="True" SortExpression="Identificativo" />
<asp:TemplateField HeaderText="Genera Fattura" HeaderStyle-CssClass="text-center" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton type="button" ID="generateInvoiace" runat="server" OnClick="generateInvoiace_Click" ForeColor="Black" CommandName="SelectRow" CommandArgument='<%# Eval("Identificativo") %>'>
<i class="fa-solid fa-file-invoice-dollar fa-lg>
</i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It takes the data from an Order Table. The problem here is that I need to disable the clickable icon for every record which have a specific value on a field of the db ... I can't actually realize how can i modify only certain icons since the table is rendered by asp.net and not manually
Ok, so say we have this grid view:
<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server" Checked='<%# Eval("Active") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hotel Information" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="cmdView" runat="server" Text="Info" CssClass="btn"
OnClick="cmdView_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And our code to load the grid view could be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid
End If
End Sub
Sub LoadGrid()
Dim rstData As DataTable
rstData = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
GHotels.DataSource = rstData
GHotels.DataBind()
End Sub
Public Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
rstData.TableName = strSQL
End Using
End Using
Return rstData
End Function
So no loops, just send a data table to the grid, and we now have this:
However, lets say we ONLY want the info button to show for when the hotel is active.
so, for things like totals, time formatting, color formatting, or whatever?
You use the row data bound event.
So, we can add to above this event that will "hide" the info button for any row in which the hotel is not active.
So, this code:
Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chkActive As CheckBox = e.Row.FindControl("chkActive")
Dim cmdView As Button = e.Row.FindControl("cmdView")
If chkActive.Checked = False Then
' hide the info button
cmdView.Style.Add("display", "none")
End If
End If
End Sub
So, as noted, for running totals, highlght a row, color a row - or in this simple case hide/show the info button based on active column?
(and note you are NOT limited to just the controls on that row - you have full use of the whole database row if you wish durng the binding).
And we now see/get this:
And note once again, no looping or for next code.
I've been trying to refresh a GridView when two textbox set me the range then click Search, and returns me the columns in that range.
But the thing is, in first load the GridView fills up with all the columns, that works fine but when I click search button doesn't refresh at all, it just stays the same.
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView CssClass="table" AllowSorting="true" HeaderStyle-HorizontalAlign="Center" EmptyDataText="No hay pendientes." CellPadding="10" HeaderStyle-CssClass="bg-primary p-2 text-dark bg-opacity-25" runat="server" ID="gvBitacora" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="cvebitacora" HeaderText="No" ItemStyle-Font-Bold="true" ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Formulario No." ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# String.Format("{0}-{1}", Eval("cveano"), Eval("cvenumero"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cveusuario" HeaderText="Usuario" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="cveestado" HeaderText="Estado" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="observacion" HeaderText="Observación" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="fechaaccion" HeaderText="Fecha" DataFormatString="{0:dd/ MM/ yyyy}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Revisión" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="btnVer" CssClass="btn btn-primary" runat="server" CommandName="btnVer" CommandArgument="<%# Container.DataItemIndex %>">Ver <i class='far fa-eye'></i></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And here the Button Click in VB
Private Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click
Dim user As User = CType(Session.Item("user"), User)
Dim oBLBitacora As New BLBitacora
Dim lBitacora, lBitacora2 As New List(Of Bitacora)
If user.cverol = 2 Then
If panelsStayOpen_headingOne.Checked = True Then
lBitacora = oBLBitacora.BitacorasGetDate(dateFechaInicio.Text, dateFechaFinal.Text)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
ElseIf user.cverol = 3 Then
lBitacora = oBLBitacora.BitacorasGet(2)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
End Sub
GridView on first load
PAGE LOAD
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim user As User = CType(Session.Item("user"), User)
Dim oBLBitacora As New BLBitacora
Dim lBitacora, lBitacora2 As New List(Of Bitacora)
If Not IsPostBack Then
If user.cverol = 2 Then
lBitacora = oBLBitacora.BitacorasGet(1)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
ElseIf user.cverol = 3 Then
lBitacora = oBLBitacora.BitacorasGet(2)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
Else
If panelsStayOpen_headingOne.Checked = True Then
panelsStayOpen_headingOne.Attributes("class") = "form-check-input"
panelsStayOpen_headingOne.Attributes("aria-expanded") = "true"
panelsStayOpen_collapseOne.Attributes("class") = "accordion-collapse collapse show"
Else
panelsStayOpen_headingOne.Attributes("class") = "collapsed form-check-input"
panelsStayOpen_headingOne.Attributes("aria-expanded") = "false"
panelsStayOpen_collapseOne.Attributes("class") = "accordion-collapse collapse"
End If
End If
where is your page load event showing the initial bind? You'll also want to be sure you call Updatepanel.Update() after the search click.
I've managed to fix it ,the Panel Update needed to be in the initial Form, not only the GridView.
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..!!
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