Gridview - Update event not fire - asp.net

When I click the "Edit" button of the gridview, it will show "Update" and "Cancel" button. But when I click "Update" button, it will not fire any event "RowUpdating", RowUpdated", "RowCommand"... It reload the page and then gridview disappear.
The following is my asp code:
<asp:GridView ID="Gridview1" runat="server" EnableViewState="False"
AutoGenerateColumns = "False" Font-Names = "Arial"
Font-Size = "10pt" AlternatingRowStyle-BackColor = "#C2D69B"
AutoGenerateEditButton="false"
AllowPaging ="True"
PageSize = "20"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"
OnPageIndexChanging="GridView1_PageIndexChanging">
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField = "Name&Post" HeaderText = "Name & Post" ReadOnly ="true" />
<asp:TemplateField HeaderText="Working<br>Time">
<ItemTemplate>
<asp:Label ID="lb1_rosterkey" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Col1_RosterKey")%>' Visible ="false" ></asp:Label>
<asp:DropDownList ID="ddl1_shifttype" runat ="server" Enabled ="false" DataSourceID="SqlDataSource2" DataTextField ="en_name" DataValueField ="shift_type_key"> </asp:DropDownList>
<MKB:TimeSelector ID="Col1_StartTime" runat="server" DisplaySeconds="False" ReadOnly="true" MinuteIncrement="1" AmPm="AM" BorderColor="Silver"
Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve"> </MKB:TimeSelector>
<MKB:TimeSelector ID="Col1_EndTime" runat="server" DisplaySeconds="False" ReadOnly="true" MinuteIncrement="1" AmPm="PM" BorderColor="Silver"
Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve"> </MKB:TimeSelector>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl1_shifttype" runat ="server" DataSourceID="SqlDataSource2" DataTextField ="en_name" DataValueField ="shift_type_key"></asp:DropDownList>
<MKB:TimeSelector ID="Col1_StartTime" runat="server" DisplaySeconds="False" MinuteIncrement="1" AmPm="AM" BorderColor="Silver"
Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve"> </MKB:TimeSelector>
<MKB:TimeSelector ID="Col1_EndTime" runat="server" DisplaySeconds="False" MinuteIncrement="1" AmPm="PM" BorderColor="Silver"
Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve"> </MKB:TimeSelector>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Leave/<br>TOIL">
<ItemTemplate>
<asp:CheckBox
ID="cb1_VL" Enabled="false" Text="VL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_VL")%> />
<asp:CheckBox
ID="cb1_SL" Enabled="false" Text="SL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_SL")%> />
<asp:CheckBox
ID="cb1_ML" Enabled="false" Text="ML"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_ML")%> />
<asp:CheckBox
ID="cb1_PH" Enabled="false" Text="PH"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_PH")%> />
<asp:CheckBox
ID="cb1_APH" Enabled="false" Text="APH"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_APH")%> />
<asp:CheckBox
ID="cb1_TOIL" Enabled="false" Text="TOIL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_TOIL")%> />
<br />
<%#DataBinder.Eval(Container.DataItem, "Col1_Others")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox
ID="cb1_VL" Text="VL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_VL")%> />
<asp:CheckBox
ID="cb1_SL" Text="SL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_SL")%> />
<asp:CheckBox
ID="cb1_ML" Text="ML"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_ML")%> />
<asp:CheckBox
ID="cb1_PH" Text="PH"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_PH")%> />
<asp:CheckBox
ID="cb1_APH" Text="APH"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_APH")%> />
<asp:CheckBox
ID="cb1_TOIL" Text="TOIL"
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_TOIL")%> />
<asp:TextBox ID="tb1_Others" runat="server" Width="50" Text='<%#DataBinder.Eval(Container.DataItem, "Col1_Others") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
......
The following is the VB code:
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'Set the edit index.
Gridview1.EditIndex = e.NewEditIndex
'Bind data to the GridView control.
BindData()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
'Reset the edit index.
Gridview1.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'Retrieve the table from the session object.
Dim dt = CType(Session("dt"), DataTable)
'Update the values.
Dim row = Gridview1.Rows(e.RowIndex)
'Reset the edit index.
Gridview1.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
Private Sub BindData()
Dim StartDateStr As String
StartDateStr = Trim(Request.QueryString("sd"))
StartDateStr = Left(StartDateStr, 10)
'date should be best in ISO format, i.e. yyyy-mm-ddTHH:mm:ss[.mmm] as "Set Dateformat dmy" is not supported by DataSet object
'StartDateStr = Right(StartDateStr, 4) & "-" & Mid(StartDateStr, 4, 2) & "-" & Left(StartDateStr, 2) & " T00:00:00"
Dim StartDate As DateTime
Dim EndDate As DateTime
StartDate = Convert.ToDateTime(StartDateStr)
EndDate = Format(DateAdd(DateInterval.Day, 6, StartDate), "dd/MM/yyyy")
g_header1 = StartDate 'Monday
g_header2 = Format(DateAdd(DateInterval.Day, 1, StartDate), "dd/MM/yyyy")
g_header3 = Format(DateAdd(DateInterval.Day, 2, StartDate), "dd/MM/yyyy")
g_header4 = Format(DateAdd(DateInterval.Day, 3, StartDate), "dd/MM/yyyy")
g_header5 = Format(DateAdd(DateInterval.Day, 4, StartDate), "dd/MM/yyyy")
g_header6 = Format(DateAdd(DateInterval.Day, 5, StartDate), "dd/MM/yyyy")
g_header7 = EndDate 'Sunday
Gridview1.DataSource = Session("dt")
Gridview1.DataBind()
End Sub
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowState = DataControlRowState.Edit Then
'If e.Row.RowType = DataControlRowType.DataRow Then
Dim row = DirectCast(e.Row.DataItem, DataRowView).Row
Dim Col1_StartTime = DirectCast(e.Row.FindControl("Col1_StartTime"), MKB.TimePicker.TimeSelector)
'set the TimePicker's Value here according to the Time-Value in the DataRow'
End If
End Sub
Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
If e.CommandName = "Update" Then
'Reset the edit index.
Gridview1.EditIndex = -1
'Bind data to the GridView control.
BindData()
End If
End Sub

Use buttons in asp:TemplateField with proper CommandName and it will fire the corresponding events.
Example
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btnEdit" runat="server" commandname="Edit" text="Edit" />
<asp:Button id="btnDelete" runat="server" commandname="Delete" text="Delete" />
</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>

I solved my problem.
I need to set CausesValidation="false"

make enableviewstate=true in gridview

did you write this at page load, if not add this
if me.ispostback= false then
'grid fill statement
end if

I'm not sure where your save/cancel buttons are located but you may need to add a command name to them as specified on MSDN
ie. <asp:buttonfield buttontype="Link" commandname="Update" text="Update"/>

I never used separate rowupdate/rowedit commands but i do use the following:
in my grid columns list, i'm adding the commandfield and images (optional for the buttons)
<asp:CommandField HeaderText="Edit" ButtonType="Image" ShowCancelButton="true" ShowEditButton="True"
EditImageUrl="../images/esig.gif" CancelImageUrl="../images/btn_close.gif"
UpdateImageUrl="../images/icn_ok.gif" />
I don't see this in your code.
a sample of a grid-editing as below
In the code behind, you should be able to check for the RowDataBound event as per below (Grida is the name of the sample grid)
Protected Sub Grida_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grida.RowDataBound
'check for row in edit mode
If (e.Row.RowState = DataControlRowState.Edit) then
' create handles for the row being edited
Dim drv As System.Data.DataRowView = CType(e.Row.DataItem, System.Data.DataRowView)
' locate the dropdownbox (in my page)
Dim dllven As DropDownList = CType(e.Row.Cells(3).FindControl("DropDownVendor"), DropDownList)
' check what item was selected in this DDL when in editmode
Dim li As ListItem = dllven.Items.FindByText(drv("Vendor").ToString)
' set the selection upon return
li.Selected = True
end if
End Sub
works perfectly for me.

Related

Drop-Down List inside Update Panel not Triggering SelectedIndexChanged Event

I'm in Asp.net 3.5 (Visual Studio 2008, VB). In a DetailsView, I've got several controls that need to perform hide/display actions, and I'm using an Update Panel. The first control is a radio button ("rblAttemptType") with two options: Phone Call or Letter. If the user chooses Phone Call, a textbox to record the time of the call opens (there's also a textbox to record the date, but it applies to both calls and letters, so it stays visible). Choosing Phone Call also enables/disenables another radio button ("rblSuccess") that records whether the contact attempt was successful. The options for rblSuccess are N/A, No and Yes. The N/A option applies when the user chooses Letter from rblAttemptType, in which case rblSuccess is also disabled (and more fields in the DetailsView become visible).
But if the user chooses Phone Call from rblAttemptType, rblSuccess is selection-cleared and enabled. Choosing no on rblSuccess reveals a drop-down list containing explanatory options (no answer, left voicemail etc.). One of the options is Other, which is supposed to reveal a textbox to record details.
Everything works, right up until that last step. It goes like this: I choose Phone Call, and the Time textbox appears. I type in the date and the time, then select No on rblSuccess, and the drop-down list appears. But when I make a selection on the drop-down list, nothing happens; i.e. the SelectedIndexChanged event assigned to the control isn't firing. And yes, it's set to AutoPostBack="True."
So--again--the SelectedIndexChanged subs for both of the radio buttons are working fine, but the one for the drop-down list isn't being hit.
Here's the code. I'm only including the parts of the DetailsView pertinent to this issue; let me know if you want the whole thing).
From the aspx page:
<asp:DetailsView ID="dvAppointment" runat="server" AutoGenerateRows="False" DataKeyNames="aID"
Visible="False" Caption="Appointment Details" BorderColor="#189AA1" BorderStyle="Solid"
BorderWidth="1px">
<RowStyle BackColor="White" BorderStyle="None" Font-Names="Verdana,Arial,Helvetica"
Font-Size="11px" Font-Strikeout="False" />
<FieldHeaderStyle BackColor="#449c77" CssClass="white" Font-Bold="True" Width="15%"
BorderColor="#189AA1" BorderStyle="Solid" BorderWidth="1px" />
<Fields>
<asp:BoundField DataField="aID" HeaderText="aID" Visible="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Attempt Type" SortExpression="AttemptType" HeaderStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label ID="lblAttemptType" runat="server" Text='<%# Eval("AttemptTypeDetails") %>'></asp:Label>
<br />
<asp:Label ID="lblAttemptMessage" runat="server" Text='<%# ContactAttemptMessage()%>'
HtmlEncode="False" CssClass="FireBrick9" HtmlEncodeFormatString="False" />
</ItemTemplate>
<InsertItemTemplate>
<asp:Label ID="lblAttemptMessage" runat="server" Text='<%# ContactAttemptMessage()%>'
HtmlEncode="False" CssClass="FireBrick9" HtmlEncodeFormatString="False" />
<asp:RadioButtonList ID="rblAttemptType" runat="server" RepeatDirection="Horizontal"
AutoPostBack="true" OnSelectedIndexChanged="rblAttemptTypeChanged" CssClass="Text9B">
<asp:ListItem Value="1">Phone call</asp:ListItem>
<asp:ListItem Value="2">Letter</asp:ListItem>
</asp:RadioButtonList>
</InsertItemTemplate>
<HeaderStyle VerticalAlign="Middle"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attempt Date" SortExpression="AttemptDate">
<ItemTemplate>
<asp:Label ID="lblAttemptDateWithTime" runat="server" Text='<%# AttemptDateDetails() %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txAttemptDate" runat="server" AutoCompleteType="Disabled" AutoPostBack="False"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderAttemptDate" runat="server" TodaysDateFormat="mm/dd/yyyy"
TargetControlID="txAttemptDate">
</asp:CalendarExtender>
<asp:UpdatePanel ID="UpdatePanelforAttempt" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="AttemptTimeLabel" runat="server" Text="Time of phone attempt (hh:mm AM/PM): "
Visible="False"></asp:Label>
<asp:TextBox ID="txAttemptTime" runat="server" ToolTip="Time should be in format XX:XX AM/PM"
Visible="False"></asp:TextBox>
<asp:MaskedEditExtender ID="txAttemptTimeTime_MaskedEditExtender" runat="server"
CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" CultureDateFormat=""
CultureDatePlaceholder="" CultureDecimalPlaceholder="" CultureThousandsPlaceholder=""
CultureTimePlaceholder="" Enabled="True" TargetControlID="txAttemptTime" Mask="99:99 LL"
ClearMaskOnLostFocus="False" MaskType="Number">
</asp:MaskedEditExtender>
<asp:Label ID="LabelSuccess" runat="server" Text="<br /><br />Was the attempt successful?"
CssClass="Text8B" ></asp:Label>
<asp:RadioButtonList ID="rblSuccess" runat="server" OnSelectedIndexChanged="rblSuccessChanged"
RepeatDirection="Horizontal" AutoPostBack="True"
Enabled='<%# IIF(Eval("AttemptType")=1,"true","false") %>'>
<asp:ListItem Value="2">Yes</asp:ListItem>
<asp:ListItem Value="1">No</asp:ListItem>
<asp:ListItem Value="0" Selected="True">N/A</asp:ListItem>
</asp:RadioButtonList>
<asp:Label ID="lblNoContactReason" runat="server" Text="<br />Please clarify the contact attempt result:"
Visible="False" CssClass="smTextItalic"></asp:Label>
<asp:DropDownList ID="ddlrncType" runat="server" DataSourceID="sdsrncTypes" DataValueField="rncTypeID"
DataTextField="rncType" Visible="false" AutoPostBack="True" OnSelectedIndexChanged="ddlrncTypeChanged">
</asp:DropDownList>
<asp:Label ID="lblSpecify" runat="server" Text="<br /><br />Please Explain:<br />"
CssClass="smTextbold" Visible="False"></asp:Label>
<asp:TextBox ID="txrncOther" runat="server" Visible="false" ToolTip="Specific result (350 characters allowed)"
TextMode="MultiLine" CssClass="MultiLineInsert" Width="35%" />
</ContentTemplate>
</asp:UpdatePanel>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
<HeaderStyle BackColor="#449C77" BorderColor="#189AA1" BorderStyle="Solid" BorderWidth="1px" />
<InsertRowStyle Width="80%" BackColor="White" Font-Names="Verdana,Ariel,Helvetica"
Font-Size="11px" />
<EditRowStyle Width="77%" BackColor="White" Font-Names="Verdana,Ariel,Helvetica"
Font-Size="11px" />
<AlternatingRowStyle BackColor="#CFCFCF" Font-Names="Verdana,Ariel,Helvetica" Font-Size="11px" />
</asp:DetailsView>
From the VB code:
Protected Sub rblAttemptTypeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iAttemptTimeLabel As Label = DirectCast(dvAppointment.FindControl("AttemptTimeLabel"), Label)
Dim itxAttemptTime As TextBox = DirectCast(dvAppointment.FindControl("txAttemptTime"), TextBox)
Dim irblSuccess As RadioButtonList = DirectCast(dvAppointment.FindControl("rblSuccess"), RadioButtonList)
Dim iUpdatePanelforAttempt As UpdatePanel = DirectCast(dvAppointment.FindControl("UpdatePanelforAttempt"), UpdatePanel)
Dim AttemptType As Integer = 0
If Not sender.selectedValue = String.Empty Then
AttemptType = CInt(sender.selectedValue)
ViewState("AttemptType") = CInt(sender.selectedValue)
If AttemptType = 1 Then 'Phone
If Not iAttemptTimeLabel Is Nothing Then
iAttemptTimeLabel.Visible = True
End If
If Not itxAttemptTime Is Nothing Then
itxAttemptTime.Visible = True
End If
If Not irblSuccess Is Nothing Then
irblSuccess.Enabled = True
irblSuccess.ClearSelection()
End If
ElseIf AttemptType = 2 Then 'Letter
If Not iAttemptTimeLabel Is Nothing Then
iAttemptTimeLabel.Visible = False
End If
If Not itxAttemptTime Is Nothing Then
itxAttemptTime.Visible = False
End If
If Not irblSuccess Is Nothing Then
irblSuccess.Enabled = False
irblSuccess.SelectedIndex = 2
End If
End If
If Not iUpdatePanelforAttempt Is Nothing Then
iUpdatePanelforAttempt.Update()
End If
End If
End Sub
Protected Sub rblSuccessChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iddlrncType As DropDownList = DirectCast(dvAppointment.FindControl("ddlrncType"), DropDownList)
Dim itxrncOther As TextBox = DirectCast(dvAppointment.FindControl("txrncOther"), TextBox)
Dim ilblNoContactReason As Label = DirectCast(dvAppointment.FindControl("lblNoContactReason"), Label)
Dim iUpdatePanelforAttempt As UpdatePanel = DirectCast(dvAppointment.FindControl("UpdatePanelforAttempt"), UpdatePanel)
Dim Success as Integer = -1
If Not sender.selectedValue = String.Empty Then
Success = CInt(sender.selectedValue)
If Success = 1 Then
If Not iddlrncType Is Nothing Then
iddlrncType.Visible = True
End If
If Not ilblNoContactReason Is Nothing Then
ilblNoContactReason.Visible = True
End If
Else
If Not iddlrncType Is Nothing Then
iddlrncType.Visible = False
End If
If Not ilblNoContactReason Is Nothing Then
ilblNoContactReason.Visible = False
End If
End If
If Not iUpdatePanelforAttempt Is Nothing Then
iUpdatePanelforAttempt.Update()
End If
End If
End Sub
Protected Sub ddlrncTypeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ilblSpecify As Label = CType(dvAppointment.FindControl("lblSpecify"), Label)
Dim itxrncOther As TextBox = CType(dvAppointment.FindControl("txrncOther"), TextBox)
Dim iUpdatePanelforAttempt As UpdatePanel = DirectCast(dvAppointment.FindControl("UpdatePanelforAttempt"), UpdatePanel)
Dim rncTypeID as Integer = 0
If Not sender.selectedValue = String.Empty AndAlso IsNumeric(sender.selectedvalue) Then
rncTypeID = CInt(sender.selectedValue)
If rncTypeID = 5 Then
If Not itxrncOther Is Nothing Then
itxrncOther.Visible = True
itxrncOther.Focus()
If Not ilblSpecify Is Nothing Then
ilblSpecify.Visible = True
End If
End If
Else
If Not itxrncOther Is Nothing Then
itxrncOther.Visible = False
If Not ilblSpecify Is Nothing Then
ilblSpecify.Visible = False
End If
End If
End If
If Not iUpdatePanelforAttempt Is Nothing Then
iUpdatePanelforAttempt.Update()
End If
End If
End Sub
I tried adding a second Update Panel, just around the drop-down list and the "Other" textbox, with the drop-down assigned as the AsyncPostBackTrigger, but it didn't make a difference.

How can my GridView Refresh using two Textbox and a Button Using VB ,Asp and ORACLE

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.

How to find Radio button control within Gridview

I am trying to find a radiobutton control within a gridview but I keep getting the output of 0 for whatever radiobutton I select. I want each radiobutton to be assigned the value of 1-4 and then whatever radiobutton selected, it will be outputted to a label.
VB code
Dim SelectNumber As Integer
For Each row As GridViewRow In MyGridView.Rows
Dim radbtn1 As RadioButton = TryCast(row.FindControl("a1"), RadioButton)
Dim radbtn2 As RadioButton = TryCast(row.FindControl("a2"), RadioButton)
Dim radbtn3 As RadioButton = TryCast(row.FindControl("a3"), RadioButton)
Dim radbtn4 As RadioButton = TryCast(row.FindControl("a4"), RadioButton)
If radbtn1.Checked = True Then
SelectNumber = 1
ElseIf radbtn2.Checked = True Then
SelectNumber = 2
ElseIf radbtn3.Checked = True Then
SelectNumber = 3
ElseIf radbtn4.Checked = True Then
SelectNumber = 4
End If
Next
lblOutput.Text = SelectNumber
Source code
<asp:GridView ShowHeader="false" AutoGenerateColumns="false" ID="MyGridView" runat="server" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="QuestionID" Text='<%# Eval("QuestionID")%>' />
<asp:Label runat="server" ID="Question" Text='<%# Eval("Question")%>' /><br />
<asp:RadioButton GroupName="gnA" Text='<%# Eval("a1")%>' runat="server" ID="ans1" /><br />
<asp:RadioButton GroupName="gnA" Text='<%# Eval("a2")%>' runat="server" ID="ans2" /><br />
<asp:RadioButton GroupName="gnA" Text='<%# Eval("a3")%>' runat="server" ID="ans3" /><br />
<asp:RadioButton GroupName="gnA" Text='<%# Eval("a4")%>' runat="server" ID="ans4" /><hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Complete" />
<asp:Label ID="lblOutput" runat="server" Text="" />
I'm pretty sure that the reason for this issue is that you databind the grid on every postback instead of only If Not Page.IsPostback. Then no one is checked and the SelectNumber keeps it's default value which is 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' DataBind the GridView here or call a method therefore '
End If
End Sub

GridView Edit Button Requires 2 Clicks

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

Get value from a textBox in Datalist linked with event button

This is my datalist:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatLayout="Flow">
<ItemTemplate>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("ID") %>' Visible="False" />
Titre:
<asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("ID", "Handler.ashx?ID={0}") %>' Width="200" Height="200"/>
<br />
comments:
<asp:Label ID="commentsLabel" runat="server" Text='<%# Eval("comments") %>' />
<br />
Ajouter commentaire
<asp:button ID="btnAjouter" runat="server" OnCommand="Button_Command"
CommandName="add" Text="Ajouter" />
<asp:TextBox ID="TextBoxComments" runat="server"></asp:TextBox>
<br/>
<br/>
</ItemTemplate>
</asp:DataList>
This my aspx.vb button event:
Sub Button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("BecsEtMuseauxSQL").ConnectionString
Dim con As SqlConnection = New SqlConnection(connectionString)
con.Open()
Dim cmd As New SqlCommand("updateComments", con)
cmd.CommandType = CommandType.StoredProcedure
//I try this ....
cmd.Parameters.Add("#id", SqlDbType.Int).Value = DataList1.FindControl("IdLabel").ToString()
cmd.Parameters.Add("#Comments", SqlDbType.NVarChar).Value = DataList1.FindControl("TextBoxComments").ToString()
cmd.ExecuteNonQuery()
End Sub
I get an error like this: "The object reference is not definied to an object instance"
How can I catch the value from the label IdLabel and TextBoxComments in this situation?
You should handle the DataList's ItemCommand instead of the Button's Command.
Then you can find your controls with e.Item.FindControl:
Private Sub DataList1_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.ItemCommand
If e.CommandName = "add" Then
Dim IdLabel = DirectCast(e.Item.FindControl("IdLabel"), Label)
Dim TextBoxComments = DirectCast(e.Item.FindControl("TextBoxComments"), TextBox)
' ... '
End If
End Sub
on aspx, remove your redundant Command-handler:
<asp:button ID="btnAjouter" CommandName="add" Text="Ajouter" runat="server" />
If you want to use the Button's Click-Event instead, that's possible also.
Protected Sub btnAjouter_Click(sender As Object, e As EventArgs)
Dim container = DirectCast(DirectCast(sender, Control).NamingContainer, DataListItem)
Dim IdLabel = DirectCast(container.FindControl("IdLabel"), Label)
Dim TextBoxComments = DirectCast(container.FindControl("TextBoxComments"), TextBox)
' ... '
End Sub
on aspx, add the click-event handler to the button:
<asp:button ID="btnAjouter" OnClick="btnAjouter_Click" Text="Ajouter" runat="server" />

Resources