I have a page with a gridview control and then a detailsview control that is within a modalpopup.
When the user clicks on a command button in the gridview, the modal is displayed with a detailsview in INSERT mode.
The Gridview RowCommand event is where I am trying to set a literal field which is in the InsertTemplate of the detailsview with a description of the assocated gridview row that was clicked by concatenating various fields from the gridview cells
Problem
This works ONLY AFTER the 1st time I click on a grideview row
The Issue
The DetailsView calls DataBound event AFTER the GridView RowCommand Event, and it is wiping out the literal text value that I just set.
The 2nd and third time, etc that this process happens, the DetailsView DataBound event is not fired, so all works as desired.
Attempted Solutions
Since the detailsview is in a popup, I tried to for the databind event in Page Load in hopes it wouldn't fire again the first time it was displayd in the modal. No luck, it still calls DataBound the first time I display it
Private Sub grdTransactions_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdTransactions.RowCommand
Dim dblCostPerOz As Double
Dim intVendorID As Integer
Select Case e.CommandName
Case "Sell"
'Gets which row we are dealing with by determining the row that the button clicked (source) was in
Dim CurrentGridRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, ImageButton).NamingContainer, GridViewRow)
'Select the current row and then get values from cells in that row
'grdTransactions.SelectedIndex = CurrentGridRow.RowIndex
'Dim intTranID As Integer = grdTransactions.SelectedDataKey.Value
Dim intTranID As Integer = grdTransactions.DataKeys(CurrentGridRow.RowIndex).Value
Dim intMetalTypeIDParent As Integer = CType(CurrentGridRow.FindControl("hidMetalTypeIDParent"), HiddenField).Value
'Set Popup window title
litPopUpTitle.Text = "Sell Metal"
'Set Asset Description on Popup
Dim strTranDate As String = CType(CurrentGridRow.FindControl("lblTranDate"), Label).Text
Dim strMetalTypeDesc As String = CType(CurrentGridRow.FindControl("lblMetalTypeDesc"), Label).Text
Dim strOunces As String = CType(CurrentGridRow.FindControl("lblOunces"), Label).Text
Dim litAssetPop As Literal = dtlEditTrans.FindControl("litAssetDescPop")
litAssetPop.Text = strTranDate + ": " + strMetalTypeDesc + " - " + strOunces + " oz"
'Save Original Row values off in hidden fields to use later when doing the insert.
hidTranID.Value = intTranID
hidMetalTypeID.Value = intMetalTypeIDParent
hidTranTypeID.Value = 2 '1 = buy 2 = sale
'Show popup to input sale data
pnlSellPopup_ModalPopupExtender.Show()
dtlEditTrans.Fields(3).Visible = True 'Asset To Sell
dtlEditTrans.Fields(6).Visible = False 'MetalTypeID
End Select
End Sub
UPDATE: Well I found that it is the hiding of fields (dtlEditTrans.Fields(6).Visible=False) that causes the renewed call to databind, but it doesn't call it right away, it waits until the Grid RowCommand event finishes....so you can't make any changes to the details
view in that routine. You have to either not manipulate the fields or do the field updates in another routine, in this case, I did it in the DetailsView PreRender....but you have to save some needed values first in the GridView RowCommand event into hidden
fields, so you can later retrieve them in PreRender of the Details View.
chadwixk
Member
71 Points
92 Posts
Timing of DetailsView Databinding when in AJAX Modal Popup
Feb 14, 2012 02:56 PM|LINK
Hello,
Background
Problem
The Issue
Attempted Solutions
Any Other Ideas? (Thanks in Advance!)
Here is my code (abbreviated)
<asp:DetailsView ID="dtlEditTrans" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="TransactionID" DataSourceID="SqlTransPopUp" GridLines="Horizontal" Width="350px" DefaultMode="Insert" ClientIDMode="Static"> <EditRowStyle Font-Bold="True" /> <FieldHeaderStyle HorizontalAlign="Left" /> <Fields> <asp:BoundField DataField="TransactionID" HeaderText="TransactionID" InsertVisible="False" ReadOnly="True" SortExpression="TransactionID" Visible="False" /> <asp:BoundField DataField="ModuleID" HeaderText="ModuleID" SortExpression="ModuleID" InsertVisible="False" /> <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" InsertVisible="False" /> <asp:TemplateField HeaderText="Asset To Sell:" SortExpression="ParentTransactionID"> <EditItemTemplate> <asp:Literal ID="litAssetDescPop" runat="server"></asp:Literal> </EditItemTemplate> <InsertItemTemplate> TEST: <asp:Literal ID="litAssetDescPop" runat="server">Buy something</asp:Literal> </InsertItemTemplate> <ItemTemplate> <asp:Literal ID="litAssetDescPop" runat="server"></asp:Literal> </ItemTemplate> </asp:TemplateField>Private Sub grdTransactions_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdTransactions.RowCommand Dim dblCostPerOz As Double Dim intVendorID As Integer Select Case e.CommandName Case "Sell" 'Gets which row we are dealing with by determining the row that the button clicked (source) was in Dim CurrentGridRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, ImageButton).NamingContainer, GridViewRow) 'Select the current row and then get values from cells in that row 'grdTransactions.SelectedIndex = CurrentGridRow.RowIndex 'Dim intTranID As Integer = grdTransactions.SelectedDataKey.Value Dim intTranID As Integer = grdTransactions.DataKeys(CurrentGridRow.RowIndex).Value Dim intMetalTypeIDParent As Integer = CType(CurrentGridRow.FindControl("hidMetalTypeIDParent"), HiddenField).Value 'Set Popup window title litPopUpTitle.Text = "Sell Metal" 'Set Asset Description on Popup Dim strTranDate As String = CType(CurrentGridRow.FindControl("lblTranDate"), Label).Text Dim strMetalTypeDesc As String = CType(CurrentGridRow.FindControl("lblMetalTypeDesc"), Label).Text Dim strOunces As String = CType(CurrentGridRow.FindControl("lblOunces"), Label).Text Dim litAssetPop As Literal = dtlEditTrans.FindControl("litAssetDescPop") litAssetPop.Text = strTranDate + ": " + strMetalTypeDesc + " - " + strOunces + " oz" 'Save Original Row values off in hidden fields to use later when doing the insert. hidTranID.Value = intTranID hidMetalTypeID.Value = intMetalTypeIDParent hidTranTypeID.Value = 2 '1 = buy 2 = sale 'Show popup to input sale data pnlSellPopup_ModalPopupExtender.Show() dtlEditTrans.Fields(3).Visible = True 'Asset To Sell dtlEditTrans.Fields(6).Visible = False 'MetalTypeID End Select End SubUPDATE: Well I found that it is the hiding of fields (dtlEditTrans.Fields(6).Visible = False) that causes the renewed call to databind, but it doesn't call it right away, it waits until the Grid RowCommand event finishes....so you can't make any changes to the details view in that routine. You have to either not manipulate the fields or do the field updates in another routine, in this case, I did it in the DetailsView PreRender....but you have to save some needed values first in the GridView RowCommand event into hidden fields, so you can later retrieve them in PreRender of the Details View.