I am having problems accessing a control in a formview and all I get is the error 'Object reference not set to an instance of an object' I have tried the code in differenc events but the result is the same.
<asp:FormView ID="frmvDetails" runat="server" DataSourceID="odsDetails" DefaultMode="ReadOnly" OnItemInserted="frmvDetails_ItemInserted" OnItemUpdated="frmvDetails_ItemUpdated" OnItemInserting="frmvDetails_ItemInserting" OnItemUpdating="frmvDetails_ItemUpdating" OnItemCreated="frmvDetails_ItemCreated" ><ItemTemplate> RefNo: <asp:Label ID="RefNoLabel" runat="server" Text='<%# Eval("RefNo") %>'></asp:Label><br /> ........ Notes: <asp:Label ID="NotesLabel" runat="server" Text='<%# Bind("Notes") %>'></asp:Label><br /> <asp:Button ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"> </asp:Button> <asp:Button ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New"> </asp:Button> </ItemTemplate>
code in page load is if (frmvDetails.DefaultMode == FormViewMode.ReadOnly) { Button test = (Button)frmvDetails.FindControl("EditButton"); test.Enabled = false; }Since I first put this on I have got this working but I am just as confused. The way I got this working was by changing the datasource to an SQLdatasource rather than an objectdatasource.
I have noticed that if you create a formview and set to an objectdatasource it does not build the code inside the formview i.e. the templates, whereas if you link to an sqldatasource the template data is created. That is ok just a little coding but not being able to reference controls inside is a problem.
<asp:ObjectDataSource ID="odsDetails" runat="server" DeleteMethod="DeleteDetails" InsertMethod="InsertDetails" SelectMethod="GetDetails" TypeName="ChangeNoteData" UpdateMethod="UpdateDetails" OnUpdated="odsDetails_Updated"> <DeleteParameters> <asp:Parameter Name="intRefNo" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="RefNo" Type="Int32" /> <asp:Parameter Name="ContainerType" Type="Int32" /> <asp:Parameter Name="Container" Type="String" /> <asp:Parameter Name="ObjectType" Type="Int32" /> <asp:Parameter Name="Object" Type="String" /> <asp:Parameter Name="Action" Type="Int32" /> <asp:Parameter Name="Notes" Type="String" /> </UpdateParameters> <SelectParameters> <asp:ControlParameter ControlID="grdvDetails" Name="intRefNo" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="ContainerType" Type="Int32" /> <asp:Parameter Name="Container" Type="String" /> <asp:Parameter Name="ObjectType" Type="Int32" /> <asp:Parameter Name="Object" Type="String" /> <asp:Parameter Name="Action" Type="Int32" /> <asp:Parameter Name="Notes" Type="String" /> <asp:Parameter Name="ChangeNoteRefNo" Type="Int16" /> </InsertParameters> </asp:ObjectDataSource>
using System;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.Web.Security;
/// <summary>/// Summary description for ChangeNoteData/// </summary>public class ChangeNoteData{ public DataSet GetDetails(int intRefNo) { DataHelper helper; SqlParameter[] par = new SqlParameter[1];
helper = new DataHelper();
par[0] = new SqlParameter(); par[0].ParameterName = "@intRefNo"; par[0].SqlDbType = SqlDbType.Int; par[0].Value = intRefNo;
return helper.ExecuteDataset("TaskManager", "p_SelectSingleChangeNoteDetails", par); if (helper.Status == -1) { throw new ArgumentException("Selecting Details: " + helper.ErrorMessage); } }
public void UpdateDetails(int RefNo, int ContainerType, string Container, int ObjectType, string Object, int Action, string Notes) { DataHelper helper; SqlParameter[] par = new SqlParameter[8]; MembershipUser muCurrent; helper = new DataHelper();
//get user's details muCurrent = Membership.GetUser();
par[0] = new SqlParameter(); par[0].ParameterName = "@intRefNo"; par[0].SqlDbType = SqlDbType.Int; par[0].Value = RefNo;
par[1] = new SqlParameter(); par[1].ParameterName = "@intContainerType"; par[1].SqlDbType = SqlDbType.Int; par[1].Value = ContainerType;
par[2] = new SqlParameter(); par[2].ParameterName = "@nvcContainer"; par[2].SqlDbType = SqlDbType.NVarChar; par[2].Size = 150; par[2].Value = Container;
par[3] = new SqlParameter(); par[3].ParameterName = "@intObjectType"; par[3].SqlDbType = SqlDbType.Int; par[3].Value = ObjectType;
par[4] = new SqlParameter(); par[4].ParameterName = "@nvcObject"; par[4].SqlDbType = SqlDbType.NVarChar; par[4].Size = 150; par[4].Value = Object;
par[5] = new SqlParameter(); par[5].ParameterName = "@intAction"; par[5].SqlDbType = SqlDbType.Int; par[5].Value = Action;
par[6] = new SqlParameter(); par[6].ParameterName = "@nvcNotes"; par[6].SqlDbType = SqlDbType.NVarChar; par[6].Size = 2000; par[6].Value = Notes;
par[7] = new SqlParameter(); par[7].ParameterName = "@uiUserID"; par[7].SqlDbType = SqlDbType.UniqueIdentifier; par[7].Value = muCurrent.ProviderUserKey;
int intReturn = helper.ExecuteNonQuery("TaskManager", "p_UpdateSingleChangeNOteDetails", par); if (helper.Status == -1) { throw new ArgumentException("Updating Details: " + helper.ErrorMessage); } }
public void InsertDetails(int ContainerType, string Container, int ObjectType, string Object, int Action, string Notes , int ChangeNoteRefNo) { DataHelper helper; SqlParameter[] par = new SqlParameter[8]; MembershipUser muCurrent;
par[0] = new SqlParameter(); par[0].ParameterName = "@intChangeNoteRefNo"; par[0].SqlDbType = SqlDbType.Int; par[0].Value = ChangeNoteRefNo;
int intReturn = helper.ExecuteNonQuery("TaskManager", "p_InsertSingleChangeNOteDetails", par); if (helper.Status == -1) { throw new ArgumentException("Inserting Details: " + helper.ErrorMessage); } }
public void DeleteDetails(int RefNo) { DataHelper helper; SqlParameter[] par = new SqlParameter[1];
int intReturn = helper.ExecuteNonQuery("TaskManager", "p_DeleteSingleChangeNOteDetails", par); if (helper.Status == -1) { throw new ArgumentException("Deleting Details: " + helper.ErrorMessage); } }
}