asp:ImageButton in Gridview OnClick event not working - asp.net

I've spent quite a while searching this problem, there are some other similar threads online but none have helped me fix it.
I have a GridView with an ImageButton within it, the imagebutton has an OnClick function but that event is never reached when it is clicked, below is my gridview:
<asp:GridView ID="gridBuildings" runat="server" AutoGenerateColumns="False"
GridLines="none" ShowFooter="false" ShowHeader="false"
Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="cmdBuy"
ImageUrl="~/images/button.jpg"
OnClick="ImageButton_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here's the code-behind (I've removed the extra content, the response.redirect is never hit when the ImageButton is clicked)
Protected Sub ImageButton_click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
response.redirect("test.aspx")
Dim row As GridViewRow = Me.gridBuildings.SelectedRow
(etc ... )
End Sub
There is a form runat="server" tag in the master page, as well as an asp:UpdatePanel and an asp:AjaxScriptManager I have a feeling the problem might be something to do with a postback but just can't work it out - any help would be appreciated.
Thanks!

Make sure you are not rebinding the grid every page load. If you rebind, the event will never fire. Wrap the binding statement in an if not ispostback block.

Related

Call Sub Procedure using Gridview Column Imagefield

I have a Gridview column called "Path" and when the user clicks on it I want it to pass that value of the "Path" cell to a sub procedure to execute the following code,
process.start(Path)
Currently the table is filled via SQL query and the "Path" is clickable but it wants to take me to URL which won't work for my situation. I also tried the Hyperlink column as well but was having the same problem. Any guidance would be appreciated! Thanks in advance.
You could use a LinkButton instead of a HyperLink.
Set the CommandArgument on the LinkButton, you could use either the GridView.RowCommand event handler or a LinkButton.OnClick event handler to call your sub.
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" OnRowCommand="gridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Path">
<ItemTemplate>
<asp:LinkButton ID="lbPath" runat="server" CommandArgument="<%# Eval("Path")%>" OnClick="lbPath_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in code behind:
Protected Sub lbPath_Click(sender As Object, e As EventArgs)
Dim linkButton As LinkButton = CType(sender, LinkButton)
Process.Start(linkButton.CommandArgument)
End Sub
For reference
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx

How do I access a hyperlink control that's in a gridview from _RowEditing Sub?

I've run into a wall and could use some advice. I have a gridview that contains a "Details" hyperlink on each row. While I am in edit mode (inline), I want to disable the details link. I thought this would be simple, but I can't seem to make it work. I had assumed (wrongly) that I could do something like:
Dim lnkDetails As HyperLink = CType(e.Row.FindControl("lnkDetails"), HyperLink)
lnkDetails.Enabled = False
The problem here, as I found out, is the "e As GridViewEditEventArgs" part of my Sub doesn't include "Row".
Here is the relevant code:
Protected Sub gvCurRecords_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
' Make the GridView control into edit mode for the selected row.
gvCurRecords.EditIndex = e.NewEditIndex
' Rebind the GridView control to show data in edit mode.
BindGridView()
' Hide the Add button.
lbtnAdd.Visible = False
End Sub
HTML (Only the relevant column):
<asp:GridView ID="gvCurRecords" CellPadding="4" DataSourceId="SqlDataSource1"
Autogeneratecolumns="false" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" OnRowEditing="gvCurRecords_RowEditing"
OnRowCancelingEdit="gvCurRecords_RowCancelingEdit"
OnRowUpdating="gvCurRecords_RowUpdating" DataKeyNames="eventID"
OnRowDataBound="gvCurRecords_RowDataBound"
OnPageIndexChanging="gvCurRecords_PageIndexChanging" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkDetails" runat="server" Text='Details' NavigateUrl='<%#FormatUrl(CInt(Eval("EventID")))%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
Any help would be greatly appreciated. Thanks.
Use the edit template to define a label instead of a hyperlink:
<EditItemTemplate>
<asp:Label ID="lbDetails" runat="server" Text='something else' />
</EditItemTemplate>

Losing update panel trigger from databound gridview in tab container

I'm having some trouble getting a command link in a gridview to maintain it's ability to change tabs after the initial postback. So below you will see the structure of my content (heavily simplified):
<ajaxToolkit:TabContainer runat="server" ID="tabBody">
<ajaxToolkit:TabPanel runat="server" ID="tabPanel1">
<ContentTemplate>
<asp:UpdatePanel runat="server" ID="updPanel1">
<ContentTemplate>
<asp:Gridview runat="server" ID="grd1" OnRowCommand="grd1_RowCommand" OnRowDataBound="grd1_RowDataBound">
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkChangePanels" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="gotopanel2" Text='<%#Eval("FirstName") & " " & Eval("LastName")%>' />
</ItemTemplate>
</asp:TemplateField>
</asp:Gridview>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
<ajaxToolkit:TabPanel runat="server" ID="tabPanel2">
<ContentTemplate>
<asp:UpdatePanel runat="server" ID="updPanel2">
<ContentTemplate>
<asp:Gridview runat="server" ID="grd2">
</asp:Gridview>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
In order to fill the gridview on panel 1, there is a search box which the user types into and I call a function to bind a linq query to it.
Now I add the rowcommand as a postback trigger on rowdatabound:
Protected Sub grd1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lb As LinkButton = CType(e.Row.FindControl("lnkChangePanels"), LinkButton)
If Not lb Is Nothing Then
ToolkitScriptManager1.RegisterPostBackControl(lb)
End If
End If
End Sub
Then here is the code I have to trigger the tab panel to change (and do some other stuff):
Protected Sub grd1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd1.RowCommand
Dim id = e.CommandArgument.ToString()
Select Case e.CommandName
Case "gotopanel2"
eventDetails(id, "C")
tabBody.ActiveTab = tabPanel2
End Select
End Sub
This causes a proper postback and changes the tab, everything works as intended. But if I go back to the first tab and try clicking another row in gridview 1, nothing happens.
Is there a way to structure this so the that either the tab can change without losing the postback trigger or am I going about this all wrong?
Thanks.
Postback trigger is not lost. Problem is caused by individual UpdatePanels in each tab.
Put entire TabContainer within UpdatePanel and you can remove UpdatePanels from tabs (but you don't have to). Make sure that UpdateMode of that new panel is set to "Always".
I think the reason why it does not change in your example is that UpdatePanel only refreshes it's own content and attribute that decides if tab is visible or not is set for div (tabPanel) outside that UpdatePanel. When you go back to tab with grid you do it client-side by clicking on it and that's when it goes wrong.
To get to the bottom of the problem and figure out why it does work during the first postback you would probably have to debug ajax toolkit javascript for TabContainer control.

Placing ASP.Net DetailsView into Edit mode and Update mode

We are working with an ASP.Net DetailsView with a VB.Net code-behind file. We are trying to allow the user to edit and save changes in the DetailsView by allowing the user to click the Edit button and then click the Update button.
Nothing happens when the user clicks the Edit button so we added an OnClick handler for the Edit button. The DetailsView will go into edit mode but only if the user clicks the Edit button twice. (Maybe an ASP.Net bug?)
Once the DetailsView is in Edit mode the Update and Cancel buttons are displayed as expected but nothing happens when the user clicks either of those buttons. We placed an OnClick on the Update button in an attempt to force the DetailsView to Update but the only choices for .ChangeMode(DetailsViewMode. are Edit, Insert, ReadOnly.
I also thought DetailsViews did not need additional OnClicks unless we needed to perform special handling.
Here is the markup for the DetailsView:
<asp:DetailsView
ID="DetailsView"
runat="server"
Height="50px"
Width="218px" AutoGenerateRows="False">
<Fields>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" OnClick="ButtonUpdate_Click" />
<asp:Button ID="ButtonCancelUpdate" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" OnClick="ButtonEdit_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Forename" HeaderText="First Name:" />
</Fields>
</asp:DetailsView>
Here is the coding in the code-behind file:
Public Class StudentDetailsMaintenance
Inherits System.Web.UI.Page
Dim theTableAdapter As New DataSetSingleStudentTableAdapters.StudentsMaintenanceTableAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Load the data from the database into the DetailsView.
'------------------------------------------------------
DetailsView.DataSource = theTableAdapter.GetDataByStudentID(StudentMaintenance.IntStudentID)
DetailsView.DataBind()
End Sub
Protected Sub ButtonEdit_Click(sender As Object, e As EventArgs)
' Place the DetailsView into Edit mode.
'--------------------------------------
DetailsView.ChangeMode(DetailsViewMode.Edit)
End Sub
Protected Sub ButtonUpdate_Click(sender As Object, e As EventArgs)
' Place the DetailsView into Update mode.
'----------------------------------------
DetailsView.ChangeMode(DetailsViewMode.)
End Sub
End Class
The ButtonUpdate_Click routine is incomplete because we don't know how to get the DetailsView to do the update.
Addional Note:
This is the first time we are trying to do a DetailsView by not setting up a DataSource in the markup. Instead of doing that we are using the data from a DataSet TableAdapter created in the DataSet designer.
If we did the DetailsView along with the DataSource all in the markup then the Edit and Update buttons work without any problem. We were also doing this in an attempt to eliminate extra coding if possible.
If you want the DetailsView's automatic behaviour for Edit, you need to use a CommandField to show the Edit button:
<asp:DetailsView id="dvDetailsView" runat="server" DataSourceId="sqlDS" DataKeyNames="primaryKey">
<Fields>
<asp:CommandField ButtonType="Button" ShowEditButton="true" />
</Fields>
</asp:DetailsView>
As noted above you need to include the DataKeyNames property to specify the primary key for the datasource so that ASP.NET knows which record in the data source to update. Also as mentioned above, you need to make sure that the Bind("columnName") statements use the same field names as used in your data store.
Also, make sure that you provide an UpdateCommand in your SqlDataProvider (or equivalent for your data store) so that the update can take place.
Now when you place the DetailsView into Edit mode, the Update and Cancel buttons will automatically be displayed where the is. If you need to do some work with the data before the data is updated in the data store, then handle the DetailView's ItemUpdating event in the code behind.
Well, looking real briefly, try wrapping your Page_Load databind in an If Not Page.IsPostback.
Postback wreaks havoc on databound controls.

Checkbox within Datagrid retaining checked value on postback in VB.NET but not C#

Hey there, I have a project in VB.NET which is working fine which essentially has a Datagrid that has a TemplateColumn included which is a column of Checkboxes. The code to declare the datagrid is here...
<asp:datagrid id="dgDates" OnItemCommand="gridEventHandler" BorderColor="Black" BorderWidth="1px"
CellPadding="3" runat="server" AutoGenerateColumns="False" HorizontalAlign="Left" AllowSorting="True"
OnSortCommand="SortData" OnItemDataBound="gridItemDataBound">
<HeaderStyle Font-Underline="True" Font-Bold="True" HorizontalAlign="Center" ForeColor="Black"
BackColor="#D4D0C8"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="strParameterName" SortExpression="strParameterName" HeaderText="Parameter Name"></asp:BoundColumn>
<asp:BoundColumn DataField="dtParameterValue" SortExpression="dtParameterValue" HeaderText="Parameter Value"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Constant" SortExpression="blnStatic" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbStaticRolling" Checked="False" Runat="server" AutoPostBack="true"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
as you can see the Checkbox has Autopostback="true" but there are other things on the page which produce postbacks as well.
My Page_load has this in it, being called on every load of the page, postbacks included...
Dim strGUID As String
strGUID = Session("strGUID")
dgDates.DataSource = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings(Web. [Global].CfgKeyConnStringADMIN), "dbo.spRptGetSchedulingDates", strGUID)
dgDates.DataBind()
intNumberOfDates = dgDates.Items().Count
as well my code behind has the following code for the gridItemDataBound
Protected Sub gridItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
'hide the intRptSchedulingDatesID for each row in the checkbox's content style variable
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
CType(e.Item.FindControl("cbStaticRolling"), CheckBox).Style("Content") = CType(e.Item.DataItem, System.Data.DataRowView).Item("intRptSchedulingDatesID")
End If
End Sub
everything you see sbove is working perfectly fine...in the sense that when I click one of the checkboxes, the page_load fires, the gridItemDataBound fires on DataBind() and when all is done, the checkbox retains the value that the user clicked the checkbox.
With all this exact same code converted to C#....the events all fire in the same order, but the checkbox selected value always clears...any thoughts??
I'd say the problem is that you're are binding the grid on every postback (I can't explain the difference between VB and C#). This clears your selections. Why are you doing this?
You bind the grid on every page_load, this causes checkbox to be always the same.
Try to attach a checkedchange event on ItemDataBound and when event fires, store checkbox value on a session variable.
On ItemDataBound, check session variable and if its null, run this code, otherwise read checkbox's value from your session variable
And On Page_Load, if not IsPostBack (page is loading first time), set your session value to null

Resources