2014-01-16 83 views
-2

幫助:.....與此空引用糾結。以下是代碼的任何想法?System.NullReferenceException由用戶代碼未處理對象變量或未設置塊變量

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> 
    <table class="auto-style1"> 
     <tr> 
      <td> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="PID" DataSourceID="DetailsSqlDataSource" Height="50px" HorizontalAlign="Center" Width="589px" CellPadding="4" ForeColor="#333333" GridLines="None"> 
       <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
       <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /> 
       <EditRowStyle BackColor="#999999" /> 
       <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" /> 
     <Fields> 
      <asp:BoundField DataField="PID" HeaderText="PID" InsertVisible="False" ReadOnly="True" SortExpression="PID" Visible="False" /> 
      <asp:TemplateField HeaderText="Address" SortExpression="PAddress"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PAddress") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PAddress") %>'></asp:TextBox> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("PAddress") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> 
      <asp:BoundField DataField="St" HeaderText="St" SortExpression="St" /> 
      <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" /> 
      <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" /> 
      <asp:BoundField DataField="Beds" HeaderText="Beds" SortExpression="Beds" /> 
      <asp:BoundField DataField="Bath" HeaderText="Bath" SortExpression="Bath" /> 
      <asp:BoundField DataField="SqFt" HeaderText="SqFt" SortExpression="SqFt" /> 
      <asp:BoundField DataField="MLS" HeaderText="MLS" SortExpression="MLS" /> 
      <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> 
      <asp:BoundField DataField="AgentName" HeaderText="AgentName" SortExpression="AgentName" /> 
      <asp:BoundField DataField="AgentEmail" HeaderText="AgentEmail" SortExpression="AgentEmail" /> 
      <asp:TemplateField> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("PhotoPath") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("PhotoPath") %>'></asp:TextBox> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Image ID="Image1" runat="server" Height="175px" ImageUrl='<%# Eval("PhotoPath") %>' Width="175px" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Fields> 
       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    </asp:DetailsView> 
      </td> 
      <td valign="top"> 
       <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="RequestInfoSqlDataSource" DefaultMode="Insert"> 
        <EditItemTemplate> 
         FromAddress: 
         <asp:TextBox ID="FromAddressTextBox" runat="server" Text='<%# Bind("FromAddress") %>' /> 
         <br /> 
         PropertyAddress: 
         <asp:TextBox ID="PropertyAddressTextBox" runat="server" Text='<%# Bind("PropertyAddress") %>' /> 
         <br /> 
         Phone: 
         <asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' /> 
         <br /> 
         Message: 
         <asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' /> 
         <br /> 
         FName: 
         <asp:TextBox ID="FNameTextBox" runat="server" Text='<%# Bind("FName") %>' /> 
         <br /> 
         LName: 
         <asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("LName") %>' /> 
         <br /> 
         Id: 
         <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' /> 
         <br /> 
         <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> 
         &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
        </EditItemTemplate> 
        <InsertItemTemplate> 
         <table style="width:100%;"> 
          <tr> 
           <td colspan="2" style="text-align: center; font-weight: 700">Request Information:</td> 
          </tr> 
          <tr> 
           <td>First Name:</td> 
           <td> 
            <asp:TextBox ID="FNameTextBox" runat="server" Text='<%# Bind("FName") %>' Width="128px" /> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" controltovalidate="FNameTextBox" runat="server" ErrorMessage="Please enter your first name" style="color: #FF0000; font-weight: 700"></asp:RequiredFieldValidator> 
           </td> 
          </tr> 
          <tr> 
           <td>Last Name:</td> 
           <td> 
            <asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("LName") %>' /> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" controltovalidate="LNameTextBox" runat="server" ErrorMessage="Please enter your last name" style="font-weight: 700; color: #FF0000"></asp:RequiredFieldValidator> 
           </td> 
          </tr> 
          <tr> 
           <td>Email:</td> 
           <td> 
            <asp:TextBox ID="FromAddressTextBox" runat="server" Text='<%# Bind("FromAddress") %>' /> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="FromAddressTextBox" runat="server" ErrorMessage="Please enter your email address" style="font-weight: 700; color: #FF0000"></asp:RequiredFieldValidator> 
           </td> 
          </tr> 
          <tr> 
           <td>Phone:</td> 
           <td> 
            <asp:TextBox ID="PhoneTextBox" runat="server" style="text-align: left" Text='<%# Bind("Phone") %>' /> 
           </td> 
          </tr> 
          <tr> 
           <td>Message:</td> 
           <td> 
            <asp:TextBox ID="MessageTextBox" runat="server" Height="189px" Text='<%# "Please Send me Information about: "+Detailsview1.DataItem("paddress") + " " +Detailsview1.DataItem("City") %>' TextMode="MultiLine" Width="198px" /> 
           </td> 
          </tr> 
          <tr> 
           <td>&nbsp;</td> 
           <td> 
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Send Request" /> 
           </td> 
          </tr> 
          <tr> 
           <td>&nbsp;</td> 
           <td> 
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("PropertyAddress") %>'></asp:TextBox> 
           </td> 
          </tr> 
         </table> 
         <br /> 
         <br /> 
         <br /> 

         &nbsp; 
        </InsertItemTemplate> 
        <ItemTemplate> 
         FromAddress: 
         <asp:Label ID="FromAddressLabel" runat="server" Text='<%# Bind("FromAddress") %>' /> 
         <br /> 
         PropertyAddress: 
         <asp:Label ID="PropertyAddressLabel" runat="server" Text='<%# Bind("PropertyAddress") %>' /> 
         <br /> 
         Phone: 
         <asp:Label ID="PhoneLabel" runat="server" Text='<%# Bind("Phone") %>' /> 
         <br /> 
         Message: 
         <asp:Label ID="MessageLabel" runat="server" Text='<%# Bind("Message") %>' /> 
         <br /> 
         FName: 
         <asp:Label ID="FNameLabel" runat="server" Text='<%# Bind("FName") %>' /> 
         <br /> 
         LName: 
         <asp:Label ID="LNameLabel" runat="server" Text='<%# Bind("LName") %>' /> 
         <br /> 
         Id: 
         <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> 
         <br /> 
         <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> 
         &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" /> 
         &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> 
        </ItemTemplate> 
       </asp:FormView> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
       <asp:SqlDataSource ID="RequestInfoSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GretnaRealtyConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [FromAddress], [PropertyAddress], [Phone], [Message], [FName], [LName] FROM [RequestInfo] WHERE (([PropertyAddress] = @PropertyAddress) AND ([PropertyAddress] = @PropertyAddress2))" InsertCommand="INSERT INTO [RequestInfo] ([FName], [LName], [FromAddress], [Phone], [Message],[PropertyAddress]) VALUES (@FName, @LName, @FromAddress, @Phone, @Message, @Hiddenfield1)"> 
        <InsertParameters> 
         <asp:Parameter Name="FName" /> 
         <asp:Parameter Name="LName" /> 
         <asp:Parameter Name="FromAddress" /> 
         <asp:Parameter Name="Phone" /> 
         <asp:Parameter Name="Message" /> 
         <asp:Parameter Name="Hiddenfield1" /> 
        </InsertParameters> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="DetailsView1" Name="PropertyAddress" PropertyName="SelectedValue" Type="String" DefaultValue="0" /> 
         <asp:QueryStringParameter Name="PropertyAddress2" QueryStringField="Paddress" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 
      </td> 
     </tr> 
    </table> 
    <asp:SqlDataSource ID="DetailsSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GretnaRealtyConnectionString %>" SelectCommand="SELECT [PID], [PAddress], [City], [St], [Zip], [Price], [Beds], [Bath], [SqFt], [MLS], [Description], [AgentName], [AgentEmail], [Status], [PhotoPath] FROM [GretnaRealty] WHERE ([PID] = @PID)"> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="PID" QueryStringField="PID" Type="Int32" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
</asp:Content> 

代碼隱藏:

Imports System.Web.UI.WebControls.GridView 
Imports System.Web.UI.WebControls.FormView 

Partial Class Details 
    Inherits System.Web.UI.Page 


Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted 
    Dim MessageTextBox As TextBox = CType(Me.DetailsView1.FindControl("Paddress"), TextBox) 
    e.Values("MessageTextBox") = DetailsView1.DataItem 
    Dim Paddresstextbox As TextBox = CType(Me.DetailsView1.FindControl("Paddress"), TextBox) 
    e.Values("Textbox3") = DetailsView1.DataItem 
End Sub 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'Automatically select the first row for 
    'the GridView control to cause the 
    'FormView control to display data. 
    If Not IsPostBack Then 
     GridView1.SelectedIndex = 0 
    End If 
End Sub 
+1

請在您的問題中添加更多詳細信息。你什麼時候收到'NullReferenceException'? – MattC

+0

錯誤提示:''TextMode =」MultiLine「Width =」198px「/>' – TomL

回答

0

我可能失去了一些東西,但它看起來像你實際上並沒有一個名爲「PAddress」控制。您將控件綁定到字段PAddress,但不是具有ID「PAddress」的控件。這可能是你問題的根源。

當您使用控件的FindControl方法時,您需要查找內部控件的ID,而不是它所綁定的字段。

同樣,您正在尋找e.Values集合中名爲「Textbox3」的值。這就是您應該在哪裏查找字段名稱的地方,即e.Values("PropertyAddress")

+0

嗨,Ann。我在這一行上得到一個錯誤:''TextMode =」MultiLine「Width =」198px「/>' 這適用,直到我點擊我的插入鏈接。 – TomL

+0

如果FormView的綁定發生在DetailsView綁定之前,您可能會遇到麻煩。 Per [this page](http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.detailsview.dataitem(v = vs.110).aspx),「此屬性僅適用於數據綁定發生之後,在數據綁定發生之前,該屬性返回null。「 –

+0

靠近: 對象變量或未設置塊變量。''TextMode =「MultiLine」Width =「198px」/>' – TomL

相關問題