lezloh
asked on
ASP.NET Set a TextBox Equal to LoginName in a DetailsVIews Inside a LoginVIew
All,
I am using C#, ASP.NET 2.0, Visual Web Developer 2005.
I have been trying for a while to set the value of a textbox (Person_Signing_Off) inside a DetailsView (DetailsView1) inside a LoginVIew(LoginView1) to the value of the LoginName(LoginName1). I keep getting a null exception error, which I believe to be because it can not find the TextBox inside the DetailsView. I have tried several fixes with no luck.
I am trying to set the Person_Signing_Off as the LogInName. My code has it just setting it to "test" right now. It errors out on the  "if (signer.Text == "")" in the code behind page.
Code Behind Page:
 protected void DetailsView1_DataBound(obj ect sender, EventArgs e)
  {
    DetailsView myDetails = (DetailsView)LoginView1.Fi ndControl( "DetailsVi ew1");
    TextBox signer = (TextBox)myDetails.FindCon trol("Pers on");
    if (signer.Text == "")
    {
      signer.Text = "test";
    }
    if (myDetails.CurrentMode == DetailsViewMode.Edit)
    {
      foreach (DetailsViewRow dvr in myDetails.Rows)
      {
        if (dvr.Cells.Count.Equals(2) && dvr.Cells[1].HasControls() )
        {
          Control ctrl = dvr.Cells[1].Controls[0];
          if (ctrl is TextBox)
          {
            TextBox txt = ctrl as TextBox;
            txt.Width = 400;
          Â
          }
        }
      }
    }
  }
}
Here is the aspx page:
<%@ Page Language="C#" MasterPageFile="~/MasterPa ge.master" AutoEventWireup="true" CodeFile="update_page.aspx .cs" Inherits="view_page" Title="ESignoff - Update Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Cont entPlaceHo lder1" Runat="Server">
  <form runat="server">
   <asp:LoginView ID="LoginView1" runat="server">
      <LoggedInTemplate>
        <span style="color: #000099"><strong>Welcome</ strong>,</ span>
        <asp:LoginName ID="LoginName1" runat="server" Font-Bold="True" ForeColor="Navy" />
        . <span style="color: #000099"><strong>Click here to</strong></span>
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/login.asp x" />
        <br /><br />
            <asp:Label ID="ownerLabel" runat="server" Text="Select the Tower:" Width="124px" Height="27px" Font-Bold="True" ForeColor="Navy"></asp:Lab el>
   Â
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourc e3"
          DataTextField="CascadeID" DataValueField="CascadeID" Width="124px" OnSelectedIndexChanged="De tailsView1 _DataBound ">
        </asp:DropDownList><asp:Sq lDataSourc e ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:towerCon nectionStr ing %>"
          SelectCommand="SELECT [CascadeID] FROM [TowerInfo]"></asp:SqlData Source>
        <br />
        <asp:TextBox ID="person" runat="server"></asp:TextB ox>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
          CellPadding="4" DataSourceID="SqlDataSourc e1" ForeColor="#333333" GridLines="None"
          Height="50px" Width="438px" DataKeyNames="CascadeID" OnDataBound="DetailsView1_ DataBound" >
          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
          <EditRowStyle BackColor="#999999" />
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
          <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
          <Fields>
            <asp:BoundField DataField="CascadeID" HeaderText="CascadeID" ReadOnly="True" SortExpression="CascadeID" />
            <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Street_Address" HeaderText="Street_Address " SortExpression="Street_Add ress" />
            <asp:BoundField DataField="Lit_Towers" HeaderText="Lit_Towers" SortExpression="Lit_Towers " />
            <asp:BoundField DataField="FCC_ASR" HeaderText="FCC_ASR" SortExpression="FCC_ASR" />
            <asp:BoundField DataField="Zip_Code" HeaderText="Zip_Code" SortExpression="Zip_Code" />
            <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="Licensee_ID" HeaderText="Licensee_ID" SortExpression="Licensee_I D" />
            <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
            <asp:BoundField DataField="Date_Signed_Off " HeaderText="Date_Signed_Of f" SortExpression="Date_Signe d_Off" />
            <asp:BoundField DataField="Person_Signing_ Off" HeaderText="Person_Signing _Off" SortExpression="Person_Sig ning_Off" />
            <asp:BoundField DataField="FRN" HeaderText="FRN" SortExpression="FRN" />
            <asp:BoundField DataField="FAA_ASN" HeaderText="FAA_ASN" SortExpression="FAA_ASN" />
            <asp:BoundField DataField="FCC_Link" HeaderText="FCC_Link" SortExpression="FCC_Link" />
            <asp:BoundField DataField="FAA_Link" HeaderText="FAA_Link" SortExpression="FAA_Link" />
            <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            <asp:TemplateField>
              <EditItemTemplate>
              </EditItemTemplate>
            </asp:TemplateField>
          </Fields>
        </asp:DetailsView>
       Â
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:towerCon nectionStr ing %>"
          SelectCommand="SELECT [CascadeID], [Owner], [Name], [Street Address] AS Street_Address, [City], [State], [Zip Code] AS Zip_Code, [FCC ASR] AS FCC_ASR, [Lit Towers] AS Lit_Towers, [Licensee ID] AS Licensee_ID, [FRN], [FAA ASN] AS FAA_ASN, [FCC Link] AS FCC_Link, [FAA Link] AS FAA_Link, [Notes], [Person Signing Off] AS Person_Signing_Off, [Date Signed Off] AS Date_Signed_Off FROM [TowerInfo] WHERE ([CascadeID] = @CascadeID)" DeleteCommand="DELETE FROM [TowerInfo] WHERE [CascadeID] = @original_CascadeID" InsertCommand="INSERT INTO [TowerInfo] ([CascadeID], [Owner], [Name], [Street Address], [City], [State], [Zip Code], [FCC ASR], [Lit Towers], [Licensee ID], [FRN], [FAA ASN], [FCC Link], [FAA Link], [Notes], [Person Signing Off], [Date Signed Off]) VALUES (@CascadeID, @Owner, @Name, @Street_Address, @City, @State, @Zip_Code, @FCC_ASR, @Lit_Towers, @Licensee_ID, @FRN, @FAA_ASN, @FCC_Link, @FAA_Link, @Notes, @Person_Signing_Off, @Date_Signed_Off)" UpdateCommand="UPDATE [TowerInfo] SET [Owner] = @Owner, [Name] = @Name, [Street Address] = @Street_Address, [City] = @City, [State] = @State, [Zip Code] = @Zip_Code, [FCC ASR] = @FCC_ASR, [Lit Towers] = @Lit_Towers, [Licensee ID] = @Licensee_ID, [FRN] = @FRN, [FAA ASN] = @FAA_ASN, [FCC Link] = @FCC_Link, [FAA Link] = @FAA_Link, [Notes] = @Notes, [Person Signing Off] = @Person_Signing_Off, [Date Signed Off] = @Date_Signed_Off WHERE [CascadeID] = @original_CascadeID" OldValuesParameterFormatSt ring="orig inal_{0}">
        <DeleteParameters>
          <asp:Parameter Name="original_CascadeID" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
          <asp:Parameter Name="Owner" Type="String" />
          <asp:Parameter Name="Name" Type="String" />
          <asp:Parameter Name="Street_Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="State" Type="String" />
          <asp:Parameter Name="Zip_Code" Type="String" />
          <asp:Parameter Name="FCC_ASR" Type="String" />
          <asp:Parameter Name="Lit_Towers" Type="String" />
          <asp:Parameter Name="Licensee_ID" Type="String" />
          <asp:Parameter Name="FRN" Type="String" />
          <asp:Parameter Name="FAA_ASN" Type="String" />
          <asp:Parameter Name="FCC_Link" Type="String" />
          <asp:Parameter Name="FAA_Link" Type="String" />
          <asp:Parameter Name="Notes" Type="String" />
          <asp:Parameter Name="Person_Signing_Off" Type="String" />
          <asp:Parameter Name="Date_Signed_Off" Type="String" />
          <asp:Parameter Name="original_CascadeID" Type="String" />
        </UpdateParameters>
        <InsertParameters>
          <asp:Parameter Name="CascadeID" Type="String" />
          <asp:Parameter Name="Owner" Type="String" />
          <asp:Parameter Name="Name" Type="String" />
          <asp:Parameter Name="Street_Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="State" Type="String" />
          <asp:Parameter Name="Zip_Code" Type="String" />
          <asp:Parameter Name="FCC_ASR" Type="String" />
          <asp:Parameter Name="Lit_Towers" Type="String" />
          <asp:Parameter Name="Licensee_ID" Type="String" />
          <asp:Parameter Name="FRN" Type="String" />
          <asp:Parameter Name="FAA_ASN" Type="String" />
          <asp:Parameter Name="FCC_Link" Type="String" />
          <asp:Parameter Name="FAA_Link" Type="String" />
          <asp:Parameter Name="Notes" Type="String" />
          <asp:Parameter Name="Person_Signing_Off" Type="String" />
          <asp:Parameter Name="Date_Signed_Off" Type="String" />
        </InsertParameters>
        <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="CascadeID" PropertyName="SelectedValu e"
            Type="String" />
        </SelectParameters>
        </asp:SqlDataSource>
       Â
      <br />
       Â
        <br />
       Â
      </LoggedInTemplate>
      <AnonymousTemplate>
        <strong><span style="color: #990000">
        You must be logged in to view this page, Click here to</span></strong>
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/login.asp x" />
      </AnonymousTemplate>
    </asp:LoginView>
  </form>
</asp:Content>
I am using C#, ASP.NET 2.0, Visual Web Developer 2005.
I have been trying for a while to set the value of a textbox (Person_Signing_Off) inside a DetailsView (DetailsView1) inside a LoginVIew(LoginView1) to the value of the LoginName(LoginName1). I keep getting a null exception error, which I believe to be because it can not find the TextBox inside the DetailsView. I have tried several fixes with no luck.
I am trying to set the Person_Signing_Off as the LogInName. My code has it just setting it to "test" right now. It errors out on the  "if (signer.Text == "")" in the code behind page.
Code Behind Page:
 protected void DetailsView1_DataBound(obj
  {
    DetailsView myDetails = (DetailsView)LoginView1.Fi
    TextBox signer = (TextBox)myDetails.FindCon
    if (signer.Text == "")
    {
      signer.Text = "test";
    }
    if (myDetails.CurrentMode == DetailsViewMode.Edit)
    {
      foreach (DetailsViewRow dvr in myDetails.Rows)
      {
        if (dvr.Cells.Count.Equals(2)
        {
          Control ctrl = dvr.Cells[1].Controls[0];
          if (ctrl is TextBox)
          {
            TextBox txt = ctrl as TextBox;
            txt.Width = 400;
          Â
          }
        }
      }
    }
  }
}
Here is the aspx page:
<%@ Page Language="C#" MasterPageFile="~/MasterPa
<asp:Content ID="Content1" ContentPlaceHolderID="Cont
  <form runat="server">
   <asp:LoginView ID="LoginView1" runat="server">
      <LoggedInTemplate>
        <span style="color: #000099"><strong>Welcome</
        <asp:LoginName ID="LoginName1" runat="server" Font-Bold="True" ForeColor="Navy" />
        . <span style="color: #000099"><strong>Click here to</strong></span>
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/login.asp
        <br /><br />
            <asp:Label ID="ownerLabel" runat="server" Text="Select the Tower:" Width="124px" Height="27px" Font-Bold="True" ForeColor="Navy"></asp:Lab
   Â
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourc
          DataTextField="CascadeID" DataValueField="CascadeID"
        </asp:DropDownList><asp:Sq
          SelectCommand="SELECT [CascadeID] FROM [TowerInfo]"></asp:SqlData
        <br />
        <asp:TextBox ID="person" runat="server"></asp:TextB
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
          CellPadding="4" DataSourceID="SqlDataSourc
          Height="50px" Width="438px" DataKeyNames="CascadeID" OnDataBound="DetailsView1_
          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
          <EditRowStyle BackColor="#999999" />
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
          <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
          <Fields>
            <asp:BoundField DataField="CascadeID" HeaderText="CascadeID" ReadOnly="True" SortExpression="CascadeID"
            <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Street_Address"
            <asp:BoundField DataField="Lit_Towers" HeaderText="Lit_Towers" SortExpression="Lit_Towers
            <asp:BoundField DataField="FCC_ASR" HeaderText="FCC_ASR" SortExpression="FCC_ASR" />
            <asp:BoundField DataField="Zip_Code" HeaderText="Zip_Code" SortExpression="Zip_Code" />
            <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="Licensee_ID" HeaderText="Licensee_ID" SortExpression="Licensee_I
            <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
            <asp:BoundField DataField="Date_Signed_Off
            <asp:BoundField DataField="Person_Signing_
            <asp:BoundField DataField="FRN" HeaderText="FRN" SortExpression="FRN" />
            <asp:BoundField DataField="FAA_ASN" HeaderText="FAA_ASN" SortExpression="FAA_ASN" />
            <asp:BoundField DataField="FCC_Link" HeaderText="FCC_Link" SortExpression="FCC_Link" />
            <asp:BoundField DataField="FAA_Link" HeaderText="FAA_Link" SortExpression="FAA_Link" />
            <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            <asp:TemplateField>
              <EditItemTemplate>
              </EditItemTemplate>
            </asp:TemplateField>
          </Fields>
        </asp:DetailsView>
       Â
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:towerCon
          SelectCommand="SELECT [CascadeID], [Owner], [Name], [Street Address] AS Street_Address, [City], [State], [Zip Code] AS Zip_Code, [FCC ASR] AS FCC_ASR, [Lit Towers] AS Lit_Towers, [Licensee ID] AS Licensee_ID, [FRN], [FAA ASN] AS FAA_ASN, [FCC Link] AS FCC_Link, [FAA Link] AS FAA_Link, [Notes], [Person Signing Off] AS Person_Signing_Off, [Date Signed Off] AS Date_Signed_Off FROM [TowerInfo] WHERE ([CascadeID] = @CascadeID)" DeleteCommand="DELETE FROM [TowerInfo] WHERE [CascadeID] = @original_CascadeID" InsertCommand="INSERT INTO [TowerInfo] ([CascadeID], [Owner], [Name], [Street Address], [City], [State], [Zip Code], [FCC ASR], [Lit Towers], [Licensee ID], [FRN], [FAA ASN], [FCC Link], [FAA Link], [Notes], [Person Signing Off], [Date Signed Off]) VALUES (@CascadeID, @Owner, @Name, @Street_Address, @City, @State, @Zip_Code, @FCC_ASR, @Lit_Towers, @Licensee_ID, @FRN, @FAA_ASN, @FCC_Link, @FAA_Link, @Notes, @Person_Signing_Off, @Date_Signed_Off)" UpdateCommand="UPDATE [TowerInfo] SET [Owner] = @Owner, [Name] = @Name, [Street Address] = @Street_Address, [City] = @City, [State] = @State, [Zip Code] = @Zip_Code, [FCC ASR] = @FCC_ASR, [Lit Towers] = @Lit_Towers, [Licensee ID] = @Licensee_ID, [FRN] = @FRN, [FAA ASN] = @FAA_ASN, [FCC Link] = @FCC_Link, [FAA Link] = @FAA_Link, [Notes] = @Notes, [Person Signing Off] = @Person_Signing_Off, [Date Signed Off] = @Date_Signed_Off WHERE [CascadeID] = @original_CascadeID" OldValuesParameterFormatSt
        <DeleteParameters>
          <asp:Parameter Name="original_CascadeID" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
          <asp:Parameter Name="Owner" Type="String" />
          <asp:Parameter Name="Name" Type="String" />
          <asp:Parameter Name="Street_Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="State" Type="String" />
          <asp:Parameter Name="Zip_Code" Type="String" />
          <asp:Parameter Name="FCC_ASR" Type="String" />
          <asp:Parameter Name="Lit_Towers" Type="String" />
          <asp:Parameter Name="Licensee_ID" Type="String" />
          <asp:Parameter Name="FRN" Type="String" />
          <asp:Parameter Name="FAA_ASN" Type="String" />
          <asp:Parameter Name="FCC_Link" Type="String" />
          <asp:Parameter Name="FAA_Link" Type="String" />
          <asp:Parameter Name="Notes" Type="String" />
          <asp:Parameter Name="Person_Signing_Off" Type="String" />
          <asp:Parameter Name="Date_Signed_Off" Type="String" />
          <asp:Parameter Name="original_CascadeID" Type="String" />
        </UpdateParameters>
        <InsertParameters>
          <asp:Parameter Name="CascadeID" Type="String" />
          <asp:Parameter Name="Owner" Type="String" />
          <asp:Parameter Name="Name" Type="String" />
          <asp:Parameter Name="Street_Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="State" Type="String" />
          <asp:Parameter Name="Zip_Code" Type="String" />
          <asp:Parameter Name="FCC_ASR" Type="String" />
          <asp:Parameter Name="Lit_Towers" Type="String" />
          <asp:Parameter Name="Licensee_ID" Type="String" />
          <asp:Parameter Name="FRN" Type="String" />
          <asp:Parameter Name="FAA_ASN" Type="String" />
          <asp:Parameter Name="FCC_Link" Type="String" />
          <asp:Parameter Name="FAA_Link" Type="String" />
          <asp:Parameter Name="Notes" Type="String" />
          <asp:Parameter Name="Person_Signing_Off" Type="String" />
          <asp:Parameter Name="Date_Signed_Off" Type="String" />
        </InsertParameters>
        <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="CascadeID" PropertyName="SelectedValu
            Type="String" />
        </SelectParameters>
        </asp:SqlDataSource>
       Â
      <br />
       Â
        <br />
       Â
      </LoggedInTemplate>
      <AnonymousTemplate>
        <strong><span style="color: #990000">
        You must be logged in to view this page, Click here to</span></strong>
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/login.asp
      </AnonymousTemplate>
    </asp:LoginView>
  </form>
</asp:Content>
ASKER
Juan Barrera:
I keep getting this error:
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE xception: Specified argument was out of the range of valid values.
Parameter name: index
Source Error:
Line 33: Â Â Â Â Â Â foreach (DetailsViewRow dvr in myDetails.Rows)
Line 34: Â Â Â Â Â Â {
Line 35: Â Â Â Â Â Â Â Â TextBox signer = (TextBox)dvr.Cells[12].Con trols[0];
Line 36: Â Â Â Â Â Â Â Â if (signer.Text == "")
Line 37: Â Â Â Â Â Â Â Â {
Â
I keep getting this error:
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE
Parameter name: index
Source Error:
Line 33: Â Â Â Â Â Â foreach (DetailsViewRow dvr in myDetails.Rows)
Line 34: Â Â Â Â Â Â {
Line 35: Â Â Â Â Â Â Â Â TextBox signer = (TextBox)dvr.Cells[12].Con
Line 36: Â Â Â Â Â Â Â Â if (signer.Text == "")
Line 37: Â Â Â Â Â Â Â Â {
Â
maybe because you are looking for the wrong ID
TextBox signer = (TextBox)myDetails.FindCon trol("Pers on");
should be
TextBox signer = (TextBox)myDetails.FindCon trol("Pers on_Signing _Off");
Person text field is outside of the details view on your posted code above.
also add checking if the field exists the assign the login name by using User.Identity.Name. from the top of my head, this is what it should look like
if(signer != null)
{
 if (signer.Text == "")
    {
      signer.Text = User.Identity.Name;
    }
}
TextBox signer = (TextBox)myDetails.FindCon
should be
TextBox signer = (TextBox)myDetails.FindCon
Person text field is outside of the details view on your posted code above.
also add checking if the field exists the assign the login name by using User.Identity.Name. from the top of my head, this is what it should look like
if(signer != null)
{
 if (signer.Text == "")
    {
      signer.Text = User.Identity.Name;
    }
}
ASKER
THIRD:
Here is my new code:
  TextBox signer = (TextBox)myDetails.FindCon trol("Pers on_Signing _Off");
    if (signer == null)
    {
      if (signer.Text == "")
      {
        signer.Text = "test";
      }
    }
Here is the error I get:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 33: Â Â Â Â if (signer == null)
Line 34: Â Â Â Â {
Line 35: Â Â Â Â Â Â if (signer.Text == "") -------This is the line that errors
Line 36: Â Â Â Â Â Â {
Line 37: Â Â Â Â Â Â Â Â signer.Text = "test";
Â
Here is my new code:
  TextBox signer = (TextBox)myDetails.FindCon
    if (signer == null)
    {
      if (signer.Text == "")
      {
        signer.Text = "test";
      }
    }
Here is the error I get:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
Line 33: Â Â Â Â if (signer == null)
Line 34: Â Â Â Â {
Line 35: Â Â Â Â Â Â if (signer.Text == "") -------This is the line that errors
Line 36: Â Â Â Â Â Â {
Line 37: Â Â Â Â Â Â Â Â signer.Text = "test";
Â
you are checking for null it should be the reverse.
if (signer == null)
should be
if (signer != null)
also, from your code the detailsview is named 'DetailsView1' so
TextBox signer = (TextBox)DetailsView1.Find Control("P erson_Sign ing_Off");
if(signer != null)
{
 if (signer.Text == "")
    {
      signer.Text = User.Identity.Name;
    }
}
if (signer == null)
should be
if (signer != null)
also, from your code the detailsview is named 'DetailsView1' so
TextBox signer = (TextBox)DetailsView1.Find
if(signer != null)
{
 if (signer.Text == "")
    {
      signer.Text = User.Identity.Name;
    }
}
sorry i just saw that the detailsview is inside the loginview. i added checking if the myDetails is also null
DetailsView myDetails = (DetailsView)LoginView1.Fi ndControl( "DetailsVi ew1");
if(myDetails != null)
{
TextBox signer = (TextBox)myDetails.FindCon trol("Pers on");
if(signer != null)
{
    if (signer.Text == "")
    {
      signer.Text = "test";
    }
}
}
DetailsView myDetails = (DetailsView)LoginView1.Fi
if(myDetails != null)
{
TextBox signer = (TextBox)myDetails.FindCon
if(signer != null)
{
    if (signer.Text == "")
    {
      signer.Text = "test";
    }
}
}
ASKER
Third:
Sorry, here is the code:
    DetailsView myDetails = (DetailsView)LoginView1.Fi ndControl( "DetailsVi ew1");
    TextBox signer = (TextBox)myDetails.FindCon trol("Pers on_Signing _Off");
    if (signer != null)
    {
      if (signer.Text == "")
      {
        signer.Text = "test";
      }
    }
With this code, it does nothing even though "myDetails.FindControl("Pe rson_Signi ng_Off")" is empty or null.
I appreciate all your help.
Sorry, here is the code:
    DetailsView myDetails = (DetailsView)LoginView1.Fi
    TextBox signer = (TextBox)myDetails.FindCon
    if (signer != null)
    {
      if (signer.Text == "")
      {
        signer.Text = "test";
      }
    }
With this code, it does nothing even though "myDetails.FindControl("Pe
I appreciate all your help.
hmm I guess maybe because we are in readonly mode and thus no textbox exists. if you need this on readonly mode, you need to make the field as template field and replace the label with a textbox instead. let me know.
The code I sent you  to look for the textbox is right, you can't look for ID's using FindControls() in BoundFields (http://www.velocityreviews.com/forums/t112792-use-findcontrol-to-find-detailsview-boundfield-control-vb.html), so the following won't work:
TextBox signer = (TextBox)myDetails.FindCon trol("Pers on_Signing _Off");
TextBox signer = (TextBox)myDetails.FindCon trol("Pers on");
What will work is this:
TextBox signer = (TextBox)dvr.Cells[12].Con trols[0];
provided there is a Cell at position 12 and a control inside that cell.
The thing is that you are getting the following error
Specified argument was out of the range of valid values.Parameter name: index
because, for some reason, there is no Cell at position 12 (I've picked that number looking at your markup code) or Control in the cell at position 0.
Please check the cell count, and, if necessary, the cell's control count.
TextBox signer = (TextBox)myDetails.FindCon
TextBox signer = (TextBox)myDetails.FindCon
What will work is this:
TextBox signer = (TextBox)dvr.Cells[12].Con
provided there is a Cell at position 12 and a control inside that cell.
The thing is that you are getting the following error
Specified argument was out of the range of valid values.Parameter name: index
because, for some reason, there is no Cell at position 12 (I've picked that number looking at your markup code) or Control in the cell at position 0.
Please check the cell count, and, if necessary, the cell's control count.
ASKER
Juan_Barrera:;
Is it possible the textbox is read only? If so, how do I make it not read only?
Â
Is it possible the textbox is read only? If so, how do I make it not read only?
Â
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I used the following code to complete this:
      TextBox signer = (TextBox)myDetails.Rows[12 ].Cells[1] .Controls[ 0];
      TextBox signer = (TextBox)myDetails.Rows[12
To find a control inside DetailsView that is a BoundField, you could use the controls collection of the cell corresponding to the BoundField Index.
That would be (I think that your field is index 12, just check it out):
 foreach (DetailsViewRow dvr in myDetails.Rows)
      {
       TextBox txt = (TextBox)dvr.Cells[12].Con
      }