Programmaticaly get gridview row out of edit mode - asp.net

In the OnRowCommand event of a ASP.NET gridview control, how can I programmaticaly get the row OUT of edit mode?
I set the row to edit mode via a commandfield. After user clicks "Disapprove now" I want to set the row in 'regular' non-edit mode.
I now have:
<asp:TemplateField HeaderStyle-Width="100" HeaderText="Actions" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Literal ID="ltStatus" runat="server" />
<asp:LinkButton CssClass="btn-primary-green" CommandArgument="<%# Container.DataItemIndex %>" ID="btnApprove" CommandName="approve" runat="server" Text="Approve" /><br />
<asp:LinkButton CssClass="btn-primary" CommandArgument="<%# Container.DataItemIndex %>" ID="btnDelete" Visible="false" CommandName="deleteorder" runat="server" Text="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlDisApproveReason" ValidationGroup="disapprove" runat="server" >
<asp:ListItem Text="<%$Resources:disapprovereason %>" Value="0" />
<asp:ListItem Text="<%$Resources:duplicateorder %>" Value="1" />
<asp:ListItem Text="<%$Resources:testorder %>" Value="2" />
<asp:ListItem Text="<%$Resources:retourorder %>" Value="3" />
</asp:DropDownList>
<asp:RequiredFieldValidator CssClass="errortext" ID="rfvDisapprove" InitialValue="0" ValidationGroup="disapprove" ControlToValidate="ddlDisApproveReason" runat="server" ErrorMessage="<%$resources:glossary,required %>" SetFocusOnError="true" Display="Dynamic" />
<asp:LinkButton CssClass="btn-primary" ValidationGroup="disapprove" CommandArgument="<%# Container.DataItemIndex %>" ID="btnDisApprove" CommandName="disapprove" runat="server" Text="Disapprove now" />
<br />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" EditText="<%$Resources:disapprove %>" />
OnRowCommand Event
Protected Sub gvAllOrders_OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvAllOrders.RowCommand
If e.CommandName = "disapprove" Then
index = Convert.ToInt32(e.CommandArgument)
data = gvAllOrders.DataKeys(index)
orderId = data.Values("id")
Dim clickedRow As GridViewRow = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim reason As Integer = CType(clickedRow.FindControl("ddlDisApproveReason"), DropDownList).SelectedValue
If GeneralFunctions.DisapproveOrder(False, reason, Date.Now, orderId) = 1 Then
'disaprove order
'HERE I WANT THE ROW TO GO OUT OF EDIT MODE
gvAllOrders.DataBind()
End If
End If
End Sub

Related

Update panel not working inside Gridview - Asp.Net/VB.Net

When I click the image button 'imgPaymentMethod' I require the code to change the ImageUrl of the Image Button. However, nothing happens when clicked. I have also tried putting the entire gridview inside the update panel but then I get this error:
A control with ID 'imgPaymentMethod' could not be found for the
trigger in UpdatePanel 'upPnlControls'
Front End..
<asp:GridView ID="gdvItems" runat="server" AutoGenerateColumns="False"
DataKeyNames="fileID" DataSourceID="DSUploadedItems" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:TemplateField visible="true" HeaderText="Price">
<ItemTemplate>
<asp:DropDownList ID="ddlBuyPrice" runat="server" AutoPostBack="True" Font-Size="11px" Height="22px" OnSelectedIndexChanged="ddlBuyPrice_SelectedIndexChanged" SelectedValue='<%# Bind("buyPrice")%>'>
<asp:ListItem Value="0.00">Select:</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton visible="false" ID="lnkPaymentMethod" runat="server" CausesValidation="False"
Text='<%# Bind("acceptPaypal")%>' > /></asp:LinkButton>
<br /><br />
<asp:Label ID="lblSoldStatus" visible="false" runat="server" CausesValidation="False" Text='<%# Bind("sold") %>' />
<asp:Image ID="imgSoldStatus" alt="Sold Status" runat="server" width="100px" ImageUrl="~/files/images/icons/iconSold.png" />
<br />
<asp:UpdatePanel ID="upPnlControls" style="margin-top:0px;" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger controlid="imgPaymentMethod" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:ImageButton visible="true" ID="imgPaymentMethod" runat="server" CausesValidation="False" width="50px" onclick="lnkPaymentMethod_Click"></asp:ImageButton>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="lblDateBought" runat="server" CausesValidation="False" Text='<%# Bind("dateBought") %>' />
<asp:LinkButton ID="lnkMarkAsSold" runat="server" OnClick="markAsSold_Click" Text="Mark As Sold" Visible="true" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB Code..
Protected Sub lnkpaymentMethod_Click(sender As Object, e As System.EventArgs)
Dim imgPaymentMethod As ImageButton = CType(sender, ImageButton)
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif" Then
imgPaymentMethod.ImageUrl="~/files/images/icons/cashIcon.gif"
Else
imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif"
End If
End Sub
After discussion we had in comments, if I understood you correctly the problem here is this line:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If you want to get grid view row that you click via imgPaymentMethod button, you need to use NamingContainer instead of Parent as follow:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.NamingContainer, GridViewRow)

DropDown binding in gridview

I have this code:
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server" AppendDataBoundItems="true"
DataSourceID="SqlDataSource3" DataTextField="GestisciIP"
DataValueField="GestisciIP" SelectedValue='<%# Bind("GestisciIP") %>'>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The datasource contain the value of a field selected on the current user (user id) and it can be True or False.
My problem is that I want the dropdown to always shows the 2 options True and False, but it should also have the selected value got from the datasource.
If I remove the 2 lines
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
it only shows the value got from the db, if I leave these lines it shows 3 values: the one got from the db and True, False added by this code.
So, how can I do this? I hope I was clear enough, thanks
IT'S DONE!
Thank you again
ASPX
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
CODE BEHIND
Private Sub GridView2_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
Dim LblddlGestisciIP As Label = DirectCast(e.Row.FindControl("LblddlGestisciIP"), Label)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
Private Sub GridView2_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
Dim ddlGestisciIP As DropDownList = DirectCast(GridView2.Rows(GridView2.EditIndex).FindControl("ddlGestisciIP"), DropDownList)
SqlDataSourceUtenti.UpdateCommand = "UPDATE [Utenti] SET [RfProfilo] = #RfProfilo, [Nome] = #Nome, [Cognome] = #Cognome, [Username] = #Username, [Password] = #Password, [Badge] = #Badge, [IpCorretto] = #IpCorretto, [GestisciIp] = " & ddlGestisciIP.SelectedValue & " WHERE [IdUtente] = #Original_IdUtente"
End Sub
try this,add Onrowdatabound event to your gridview and bind your dropdownlist in that event.
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" CellPadding="4" DataSourceID="SqlDataSourceUtenti"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
DataKeyNames="IdUtente" BorderColor="#E1E1E1" BorderStyle="Solid"
BorderWidth="3px" OnRowDataBound="GridView2_RowDataBound" OnRowCommand="GridView2_RowCommand" OnRowUpdating="GridView2_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RfProfilo" HeaderText="RfProfilo"
SortExpression="RfProfilo" Visible="False" />
<asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
<asp:BoundField DataField="Cognome" HeaderText="Cognome"
SortExpression="Cognome" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:TemplateField HeaderText="Profilo" SortExpression="Profilo">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Nome" DataValueField="idProfilo"
SelectedValue='<%# Bind("RfProfilo") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeProfilo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="Password">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# mascheraPassword(Eval("Password").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Badge" HeaderText="Badge" SortExpression="Badge" />
<asp:TemplateField HeaderText="IP di origine" SortExpression="IpCorretto">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IpCorretto") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblIpCorretto" maxlength="16" runat="server" Text='<%# limitaCaratteri(Eval("IpCorretto").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Aggiorna" ForeColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Annulla" ForeColor="White"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Modifica"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Elimina"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
in code behind
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
First ,With the use of RowCommand Event of the Grid View , Get the value of the Edited Label into the ViewState,then Use viewstate and Handle your RowDataBound
and to upadate your data you can use roeupdate event,like follows
protected void Gridview2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
//add code for updating values in database
}
If i understood correct, you only want the values TRUE and FALSE, so, you don't need a datasource to do this...
Remove this properties DataSourceID, DataTextField, DataValueField and SelectedValue...
Try it please.

Edit template not displaying

I have a gridview that has a item template and an edit template. The problem is, whenever I click the edit button, the edit templates don't show up.
Here is my page code:
<asp:GridView
ID="RoutesGridView"
runat="server"
CssClass="table table-striped table-hover"
GridLines="None"
AutoGenerateColumns="False"
DataKeyNames="RouteId,LocationId,ConcurrencyId"
AllowPaging="true"
EmptyDataText="No Information Retrieved">
<Columns>
<asp:TemplateField Visible="false">
<EditItemTemplate>
<asp:Label id="RouteIdLB" runat="server" Text='<%# Bind("RouteId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="RouteIdLB" runat="server" Text='<%# Bind("RouteId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="NameTB" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="NameLB" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description" >
<EditItemTemplate>
<asp:TextBox ID="DescriptionTB" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="DescriptionLB" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Zone" SortExpression="TimeZoneCode">
<EditItemTemplate>
<asp:DropDownList ID="TimeZoneDDL" runat="server" DataSource="<%# GetTimeZones() %>" AppendDataBoundItems="true" DataTextField="TimeZoneCode" DataValueField="TimeZoneId" SelectedValue='<%# Bind("TimeZoneId") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TimeZoneCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="IsActive" HeaderStyle-CssClass="textCenterAlign" ItemStyle-CssClass="textCenterAlign">
<EditItemTemplate>
<asp:CheckBox ID="IsActiveCB" runat="server" Checked='<%# Bind("IsActive") %>' />
</EditItemTemplate>
<ItemTemplate>
<%# IIf(Eval("IsActive").Equals(True), "<i class='icon-ok'></i>", " ")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Deleted" SortExpression="IsDeleted" HeaderStyle-CssClass="textCenterAlign" ItemStyle-CssClass="textCenterAlign">
<EditItemTemplate>
<asp:CheckBox ID="IsDeletedCB" runat="server" Checked='<%# Bind("IsDeleted") %>' Enabled="False" />
</EditItemTemplate>
<ItemTemplate>
<%# IIf(Eval("IsDeleted").Equals(True), "<i class='icon-ok'></i>", " ")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" ItemStyle-CssClass="actions">
<EditItemTemplate>
<asp:LinkButton ID="SaveButton" runat="server" CssClass="btn btn-mini btn-primary hiddenButton" CommandName="Update" Visible="True" Enabled="True" ToolTip="Save pending changes" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-ok icon-white"></i>Save</asp:LinkButton>
<asp:LinkButton ID="CancelButton" runat="server" CssClass="btn btn-mini btn-danger hiddenButton" CommandName="Cancel" Visible="True" Enabled="True" ToolTip="Cancel pending changes" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-ban-circle icon-white"></i>Cancel</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CssClass="btn btn-mini btn-primary hiddenButton" CommandName="Edit" Visible="True" Enabled="True" ToolTip="Edit this entry" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-edit icon-white"></i>Edit</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CssClass="btn btn-mini btn-danger hiddenButton" CommandName="Delete" Visible="True" Enabled="True" ToolTip="Delete this entry" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' OnClientClick='return confirm("Are you certain that you want to delete this entry?");'><i class="icon-trash icon-white"></i>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB code behind:
Protected Sub RoutesGridView_RowEditing(ByVal sender As System.Object, ByVal e As GridViewEditEventArgs) Handles RoutesGridView.RowEditing
Dim gv As GridView = CType(sender, GridView)
Dim row As GridViewRow = gv.Rows(e.NewEditIndex)
Dim RouteId As Integer = CInt(CType(row.FindControl("RouteIdLB"), Label).Text)
_Route = RouteManager.GetItem(RouteId, _dftIdentity)
gv.EditIndex = e.NewEditIndex
RoutesGridView_DataBind()
End Sub
Protected Sub RoutesGridView_RowCancelingEdit(ByVal sender As System.Object, ByVal e As GridViewCancelEditEventArgs) Handles RoutesGridView.RowCancelingEdit
_Route = Nothing
RoutesGridView.EditIndex = -1
RoutesGridView_DataBind()
End Sub
Protected Sub RoutesGridView_RowUpdating(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles RoutesGridView.RowUpdating
Try
Dim gv As GridView = CType(sender, GridView)
Dim row As GridViewRow = gv.Rows(e.RowIndex)
With _Route
.Name = CType(row.FindControl("NameTB"), TextBox).Text
.TimeZoneId = CType(row.FindControl("TimeZoneDDL"), DropDownList).SelectedValue
.IsActive = CType(row.FindControl("IsActiveCB"), CheckBox).Checked
End With
RouteManager.Update(_Route, _dftIdentity)
_Route = Nothing
gv.EditIndex = -1
RoutesGridView_DataBind()
Catch ex As Exception
ProcessException(ex)
End Try
End Sub
You need to change the Grid edit form property from auto
to template

Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.IConvertible'

I'm creating a student evaluation form in a repeater that should submit the evaluation responses and update the SQL database, but I keep getting the error mentioned in the title. I'm coding in ASP.Net using VB.
Here's my code:
Student.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:Wizard ID="StudentEvaluationWizard" runat="server" ActiveStepIndex="0">
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Course">
<asp:GridView ID="CourseView" runat="server" AutoGenerateColumns="False"
DataKeyNames="SectionID" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="EnrollmentID" HeaderText="EnrollmentID"
InsertVisible="False" ReadOnly="True" SortExpression="EnrollmentID" />
<asp:BoundField DataField="StudentID" HeaderText="StudentID"
SortExpression="StudentID" />
<asp:BoundField DataField="SectionID" HeaderText="SectionID"
SortExpression="SectionID" />
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Section" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Section.Course.Name") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Evaluation">
<asp:Repeater ID="EvalRepeater" runat="server"
DataSourceID="EvaluationQuestions">
<HeaderTemplate><caption><asp:Label ID="EvaluationTitle" runat="server" Text='<%Eval("Evaluation.Evaluation1.") %>' /><caption></HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' Visible="false" />
</td>
<td>
<asp:Label ID="Questions" runat="server" Text='<%# Eval("Question1") %>' />
</td>
<td>
<%--Move outside of table to make edits--%>
<asp:RadioButtonList ID="Question" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="StudentComment" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="SubmitButton" runat="server" Text="Submit Evaluation" />
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
<asp:LinqDataSource ID="CourseSelect" runat="server" ContextTypeName="EvaluationDAL.Model.EvaluationDataDataContext"
EntityTypeName="Enrollment" TableName="Enrollments" Where="StudentID == #StudentID">
<WhereParameters>
<asp:SessionParameter DefaultValue="StudentID" Name="StudentID" SessionField="StudentID"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:LinqDataSource ID="EvaluationQuestions" runat="server" ContextTypeName="EvaluationDAL.Model.EvaluationDataDataContext"
EntityTypeName="" TableName="Questions"
Where="EvaluationID == Convert.ToInt32(#EvaluationID)">
<WhereParameters>
<asp:SessionParameter Name="EvaluationID" SessionField="EvaluationID"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
</asp:Content>
Student.aspx.vb (Submit button with the error)
Protected Sub SubmitButton_Click(sender As Object, e As System.EventArgs) Handles SubmitButton.Click
Dim sr As New StudentEvaluation
sr.SectionID = EvaluationGlobal.SectionID
sr.EvaluationDate = DateAndTime.Now
'Create a new evaluation object
Dim eval As New StudentEvaluation
For Each row As RepeaterItem In EvalRepeater.Items
'Get the question
Dim questionID As Label = CType(row.FindControl("QuestionID"), Label)
Dim question1 As Integer = Convert.ToInt32(question1)
eval.Comments = Convert.ToInt32(row.FindControl("Comments"))
'Find radio button list with the name Question and convert to radio button list
Dim rank As RadioButtonList = CType(row.FindControl("Question"), RadioButtonList)
'Get the infomation if the area is not empty
If Not String.IsNullOrWhiteSpace(rank.SelectedValue) Then
Dim ser As New StudentEvaluationResponse
ser.QuestionID = Convert.ToInt32(row.FindControl("QuestionID"))
ser.Answer = Convert.ToInt16(row.FindControl("Question"))
Dim questionRank As Integer = Convert.ToInt32(rank.SelectedValue)
Dim studentComment As TextBox = CType(row.FindControl("StudentComment"), TextBox)
Dim comment As String = studentComment.Text
eval.StudentEvaluationResponses.Add(ser)
End If
Next
If eval.StudentEvaluationResponses.Count > 0 Then
eval.Insert()
End If
End Sub
I'm completely lost on how to fix this. Any help would be awesome.
This looks wrong to start with:
Dim question1 As Integer = Convert.ToInt32(question1)
I suspect you actually mean:
Dim question1 As Integer = Convert.ToInt32(questionID.Text)
Likewise I suspect these:
ser.QuestionID = Convert.ToInt32(row.FindControl("QuestionID"))
ser.Answer = Convert.ToInt16(row.FindControl("Question"))
should use the Text property:
ser.QuestionID = Convert.ToInt32(CType(row.FindControl("QuestionID"), Label).Text)
ser.Answer = Convert.ToInt16(CType(row.FindControl("Question"), RadioButton).Text)
That way you're trying to convert the text of the control, not the control itself.

DetailsView update - not returning new values

After editing some fields, clicking on Update doesn't show the new values. I have tried two ways to retrieve the new values as you can see from this shortened version of the ItemUpdating event (and both return the old ones) :-
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim txtPassword As TextBox = CType(DetailsView1.Rows(1).Cells(1).FindControl("txtPassword"), TextBox)
Struct_Student.password = txtPassword.Text
Dim PasswordValue As String = e.NewValues("password").ToString()
Struct_Student.password = PasswordValue
End Sub
Here is a shortened version of the aspx :-
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<div id="Controls">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="student_id" DataSourceID="SqlDataSource1"
ForeColor="Blue"
BackColor="#FFF7E6"
AutoPostBack="True"
AutoGenerateEditButton = True
AutoGenerateInsertButton = True
OnModeChanging="StudentDetailView_ModeChanging"
Height=163px
Width=327px
style="left: 400px; top: 1px; position: absolute;">
<Fields>
<asp:TemplateField
HeaderText="Password">
<EditItemTemplate>
<asp:TextBox
id="txtPassword"
Text = '<%# Bind ("password") %>'
runat = "server" />
<asp:RequiredFieldValidator
ID = "reqPassword"
ControlToValidate = "txtPassword"
Text = "(required)"
Display = "Dynamic"
runat = "server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label
id="PasswordLabel"
runat="server"
Text = '<%# Eval("password") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField
datafield="emailaddress"
headertext="Email address"
SortExpression="emailaddress"
/>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:FCLManager %>" ProviderName="MySql.Data.MySqlClient"
SelectCommand="Select * from tblstudentinfo WHERE centre_id = #CentreID and fullname = #FullName"
<SelectParameters>
<asp:Parameter Name="CentreID" Type="Int16" DefaultValue="0" />
<asp:Parameter Name="FullName" Type="String" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div> <%--Controls div--%>
</asp:Content>

Resources