I have some strange behaviour with gridviews while changing the website from HTTP to HTTPS.
On HTTP everything works fine with all browsers.
On HTTPS the SelectedIndexChanged method is called twice when clicking on this gridview. This only happens in Firefox. All other browsers still work fine.
Some code:
<asp:GridView ID="gvLinks" runat="server"
CssClass="mydatagrid"
HeaderStyle-CssClass="header"
OnSelectedIndexChanged="OnSelectedIndexChangedSV"
RowStyle-CssClass="spinner rows"
Width="100%"
Visible ="false">
<Columns>
<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Image ID="Image1"
ImageUrl='<%# ChooseImage(DirectCast(Eval("Status"), Integer)) %>'
ToolTip='<%# ChooseStatus(DirectCast(Eval("Status"), Integer)) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind
Protected Sub OnSelectedIndexChangedSV(sender As Object, e As EventArgs)
Dim id As Integer = CInt(gvLinks.SelectedRow.Cells(1).Text)
.....
End Sub
Does anyone have any idea what may be the cause of this behaviour?
Thanks!!
Related
Apologies for the slightly convoluted title.
Basically, I have a project where I have a database connected to it. I'm displaying some of the contents of the database using a GridView, which is all up and running, but the way I'm designing the page I need to be able to click a button in each row that in essence exports the value of one of the cells in the row to a subroutine.
I've tried googling it quite a bit and trying related things I found here, but nothing I could find would function like I would like it to.
The relevant code is below.
Markup:
<asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" OnRowCommand="Staffgv_RowCommand" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" >
<Columns>
<asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart">
<HeaderStyle Width="70px" CssClass="hdrGvStart"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeStart" runat="server" Text='<%# Bind("DateTimeStart", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd">
<HeaderStyle Width="70px" CssClass="hdrGvFinish"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeEnd" runat="server" Text='<%# Bind("DateTimeEnd", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Forename" SortExpression="Forename">
<HeaderStyle Width="140px" CssClass="hdrGvForename"/>
<ItemTemplate>
<asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderStyle Width="140px" CssClass="hdrGvSurname"/>
<ItemTemplate>
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle CssClass="gvHeaderEdit" />
<ItemTemplate>
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit" CommandName="FillStaffTables" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the relevant VB code:
Private Sub Staffgv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Staffgv.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim row = DirectCast(e.Row.DataItem, DataRowView)
Dim apptid As Integer = Integer.Parse(row("AppointmentID").ToString)
Dim Btnapptid = DirectCast(e.Row.FindControl("Btnapptid"), Button)
'Btnapptid.Text = apptid
Btnapptid.ToolTip = apptid
End Select
End Sub
Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
If (e.CommandName = "FillStaffTables") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
lblTxtTester.Text = "AppointmentID"
TextboxTester.Text = index
End If
End Sub
If anyone would like more code like the code used to fill the GridView I'll post it, just didn't want to post too much irrelevant code.
Thanks
Solution A
When you only want 1 value from the gridview row when the button is clicked.
You could simply utilize the CommandArgument of your button (Btnapptid). This assumes that you don't need the gridview row index for when the button is clicked. If you do need this, then please see Solution B, otherwise continue here.
First, you'd need to modify your button's CommandArgument in the aspx page
ASPX
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit"
CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>'/>
Then you should be able to grab the AppointmentID like this
VB (inside Staffgv_RowCommand())
If (e.CommandName = "FillStaffTables") Then
txtAppointmentID.Text = e.CommandArgument
End If
Solution B
When you need more than 1 value from the gridview row when the button is clicked
Please note that this solution requires a couple changes on your end
Create an additional control (in the gridview) which should hold the value that you want to get when the button is clicked.
Fill said control with the value you want (via Bind() in the UI or in RowDataBound in the codebehind).
Next, in your RowCommand you'd be able to grab the newly created control with the help of the index variable (from your example) like so
Staffgv.Rows(index).FindControl("YOUR_CONTROLS_ID")
Example
Say that you decide to create a HiddenField control in which to store the value from your database
ASPX (hidden field should be somewhere inside the gridview - right under Btnapptid should be fine)
<asp:HiddenField ID="hfMyControl" runat="server" Visible="False"
Value='<%# Bind("SOME_DB_COLUMN") %>'/>
VB (inside Staffgv_RowCommand())
Dim hfMyControl As HiddenField = _
DirectCast(Staffgv.Rows(index).FindControl("hfMyControl"), HiddenField)
Then simply use the value of hfMyControl
hfMyControl.Value
You could also repeat all this using multiple controls in order to potentially access multiple DB values stored in controls in the gridview.
I am trying to obtain the row index on a button click in an asp gridview.
<asp:Panel runat="server" Visible="true" ID="pnlGV">
<asp:GridView ID="gvTasks" runat="server" CssClass="table table-hover table-striped" DataKeyNames="TaskID" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" PageSize="10" SkinID="standard_gv" EmptyDataText="No tasks match your search terms">
<Columns>
<asp:BoundField SortExpression="TaskID" DataField="TaskID" ReadOnly="true" HeaderText="Task ID" />
<asp:BoundField SortExpression="ProjectCode" DataField="ProjectCode" ReadOnly="true" HeaderText="Project Code" />
<asp:BoundField SortExpression="TaskType" DataField="TaskType" ReadOnly="true" HeaderText="Task Type" />
<asp:BoundField SortExpression="TaskDescription" DataField="TaskDescription" ReadOnly="true" HeaderText="Description" />
<asp:BoundField SortExpression="BizArea" DataField="BizArea" ReadOnly="true" HeaderText="Business Area" />
<asp:BoundField SortExpression="TargetDate" DataField="TargetDate" ReadOnly="true" HeaderText="Target" />
<asp:BoundField SortExpression="TaskStatus" DataField="TaskStatus" ReadOnly="true" HeaderText="Status" />
<asp:BoundField SortExpression="TaskPriority" DataField="TaskPriority" ReadOnly="true" HeaderText="Priority" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnNotes" runat="server" CommandName="notes" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Notes" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
and
Protected Sub gvTasks_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
Try
If (e.CommandName = "notes") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gvTasks.Rows(index)
notesAcc.Populate(CInt(gvTasks.Rows(index).Cells(0).Text))
notesAcc.Visible = True
End If
Catch ex As Exception
Throw
End Try
End Sub
This just throws this error message at me:
Invalid postback or callback argument. Event validation is enabled
using 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.
and I have no idea how to fix it.
Would anyone be able to give me some insight?
Thank you in advance.
Tom
Edit:
Thank you for the responses, it is much appreciated.
What is notesAcc.Populate(CInt(gvTasks.Rows(index).Cells(0).Text)) dong? If you comment it out does the problem go away?
That is calling a Sub from an .ascx file, and I wouldn't imagine so as I've breakpointed the event handler and it doesn't even reach it.
Do you get that error when page loads or when you click the button?
When I click the button.
It is probably worth mentioning that I am no longer getting the error. I made a tiny insignificant change - so small I can't even remember what it was - and it's gone away, however, it's still not working as intended. The event handler doesn't trigger and I have no idea why. Again, would anyone be able to give me a nudge in the right direction?
You can do one of 3 things:
Set AutoEventWireUp to true.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Manager.Default" %>
or
Add the event to the GridView
<asp:GridView ID="gvTasks" runat="server" CssClass="table table-hover table-striped" DataKeyNames="TaskID" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" PageSize="10" SkinID="standard_gv" EmptyDataText="No tasks match your search terms" OnRowCommand="gvTasks_RowCommand">
or
Add the handles keyword to the method
Protected Sub gvTasks_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTasks.RowCommand
I hope it helps
I am new to Vb.net.
I am using gridview and binding it a resultset from database. I am trying to use asp button and associate a codebehind function with it.
But since the rows are generating dynamically, ids of button are incrementing accordingly.
Here is the code -
'<asp:GridView ID="grdProjects" runat="server" CssClass="q_acontent" Width="990px"
AutoGenerateColumns="false" CellPadding="2" ClientIDMode="static" ViewStateMode="Enabled">
<asp:TemplateField HeaderText="Document Name">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem, "Data_text")))%>'/>
</ItemTemplate>
<ItemStyle Width="5%" HorizontalAlign="Center" CssClass="conatact_phone breakword" />
</asp:TemplateField>
</asp:GridView>'
Please help me how to associate codebehind function and pass its corresponding button text to it.
Use the OnCommand event of button controls and Set the CommandArgument property of the button to your DataItem value as:
<ItemTemplate>
<asp:Button ID="Button3" runat="server"
Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,"Data_text")))%>'
OnCommand="Button3_Command"
CommandArgument='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,
"Data_text")))%>'
/>
</ItemTemplate>
OnCommand event in your code behind file:
Public Sub Button3_Command(sender As Object, e As CommandEventArgs)
Dim _dataText As String = e.CommandArgument.ToString()
End Sub
Hi all I have a simpe ASP button inside a grid. But the onclick event doesn't seem to fire. Where did I go wrong?
Here's the first line of my aspx page.
<%# Page Title="Trainer Data" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeFile="TrainerData.aspx.vb" Inherits="TrainerData"%>
And the button inside my gridview..
<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate" OnClientClick="btnExecutiveGenerate_Click" />
</ItemTemplate>
</asp:TemplateField>
P.S. I tried even onclick but it doesn't work either.
EDIT: My code for server side.
Protected Sub btnExecutiveGenerate_Click(sender As Object, e As EventArgs)
Dim gvrow As GridViewRow = CType(CType(sender, Control).Parent.Parent, GridViewRow)
Dim lblSchoolId As System.Web.UI.WebControls.Label = gvrow.FindControl("lblSchoolMasterID")
Dim lblFacultyId As System.Web.UI.WebControls.Label = gvrow.FindControl("lblFacultyMasterID")
Dim btnExecutiveGenerate As System.Web.UI.WebControls.Button = gvrow.FindControl("btnExecutiveGenerate")
PDF_Creation_Executive(Val(lblSchoolId.Text), Val(lblFacultyId.Text))
End Sub
Use Command Argumnet,
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
add code page side
Protected Sub GridView1_RowCommand(ByVal sender As Object, _ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddToCart") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GridView1.Rows(index)
' Add code here to add the item to the shopping cart.
End If
End Sub
You need to handle the button click event of Gridview in RowCommand event
NOTE: Please see the CommandName & CommandArgument properties added to the button.
<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" OnRowCommand="gvExecSummary_RowCommand" />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate" CommandName="GenerateExecutive" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
And and RowCommand event will be..
protected void gvExecSummary_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GenerateExecutive")
{
// button click code goes here
}
}
change OnClientClick event to OnClick if btnExecutiveGenerate_Click is vb.net event handler
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"
OnClick="btnExecutiveGenerate_Click"/>
OnClientClick event use to execute client-side script, if you have given OnClientClick event with OnClick event, then if OnClientClick return true only it will call OnClick event.
so make sure you are returning true or false from OnClientClick event if you using it.
note that if you are loading data in page laod, do as below
Sub Page_Load
If Not IsPostBack
LoadGridViewData()
End If
End Sub
This may help someone, but I was experiencing edit buttons not firing the event within rows (and not footer).
The cause was a gridview containing a label, which had the same ID as one elsewhere on the page.
Seemingly this did not cause problems for the footer row, which did not contain any such labels.
I hope this helps someone.
I have a gridview with a button, and when the button is clicked, it fires a rowcommand procedure and adds a new row to the database. Everything works fine until I add a databound drop down list to the gridview.
With a databound dropdown list, the page loads fine, but when I click the button the error shows as "Internet Explorer cannot display the webpage". here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
DataSourceID="SqlDataSource1">
<Columns>
<asp:ButtonField CommandName="insertNew"
Text="Button" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CommandName="insertNew"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
And here is my code behind that runs when the button is pressed;
Protected Sub GridView1_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "insertNew") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
MsgBox(index)
End If
End Sub
I just had to put this into the system.web web.config file
<httpRuntime maxRequestLength="32768" />
The problem is in the MsgBox line. MsgBox(index) is not supported in web applications.
Please remove MsgBox(index), and the issue will be fixed, because that feature is only supported in Windows Applications.