Link to home
Create AccountLog in
Avatar of lezloh
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(object sender, EventArgs e)
    {
        DetailsView myDetails = (DetailsView)LoginView1.FindControl("DetailsView1");

        TextBox signer = (TextBox)myDetails.FindControl("Person");
        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="~/MasterPage.master" AutoEventWireup="true" CodeFile="update_page.aspx.cs" Inherits="view_page" Title="ESignoff - Update Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" 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.aspx" />
                <br /><br />
                        <asp:Label ID="ownerLabel" runat="server" Text="Select the Tower:" Width="124px" Height="27px" Font-Bold="True" ForeColor="Navy"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3"
                    DataTextField="CascadeID" DataValueField="CascadeID" Width="124px" OnSelectedIndexChanged="DetailsView1_DataBound">
                </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:towerConnectionString %>"
                    SelectCommand="SELECT [CascadeID] FROM [TowerInfo]"></asp:SqlDataSource>
                &nbsp;&nbsp;<br />
                &nbsp;<asp:TextBox ID="person" runat="server"></asp:TextBox>
                <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
                    CellPadding="4" DataSourceID="SqlDataSource1" 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_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_ID" />
                        <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                        <asp:BoundField DataField="Date_Signed_Off" HeaderText="Date_Signed_Off" SortExpression="Date_Signed_Off" />
                        <asp:BoundField DataField="Person_Signing_Off" HeaderText="Person_Signing_Off" SortExpression="Person_Signing_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:towerConnectionString %>"
                    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" OldValuesParameterFormatString="original_{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="SelectedValue"
                        Type="String" />
                </SelectParameters>
                </asp:SqlDataSource>
               
            <br />
                &nbsp; &nbsp;&nbsp;&nbsp;
                <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.aspx" />
            </AnonymousTemplate>
        </asp:LoginView>
    </form>
</asp:Content>

Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Hi there,

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].Controls[0];
            }


Avatar of lezloh
lezloh

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.ArgumentOutOfRangeException: 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].Controls[0];
Line 36:                 if (signer.Text == "")
Line 37:                 {
 
maybe because you are looking for the wrong ID

TextBox signer = (TextBox)myDetails.FindControl("Person");

should be

TextBox signer = (TextBox)myDetails.FindControl("Person_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;
        }
}
Avatar of lezloh

ASKER

THIRD:

Here is my new code:
    TextBox signer = (TextBox)myDetails.FindControl("Person_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.NullReferenceException: 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";
 
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.FindControl("Person_Signing_Off");
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.FindControl("DetailsView1");
if(myDetails != null)
{
TextBox signer = (TextBox)myDetails.FindControl("Person");
if(signer != null)
{
        if (signer.Text == "")
        {
            signer.Text = "test";
        }
}
}
Avatar of lezloh

ASKER

Third:

Sorry, here is the code:

        DetailsView myDetails = (DetailsView)LoginView1.FindControl("DetailsView1");

        TextBox signer = (TextBox)myDetails.FindControl("Person_Signing_Off");
        if (signer != null)
        {
            if (signer.Text == "")
            {
                signer.Text = "test";
            }
        }

With this code, it does nothing even though "myDetails.FindControl("Person_Signing_Off")" is empty or null.

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.FindControl("Person_Signing_Off");
TextBox signer = (TextBox)myDetails.FindControl("Person");

What will work is this:

TextBox signer = (TextBox)dvr.Cells[12].Controls[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.



Avatar of lezloh

ASKER

Juan_Barrera:;

Is it possible the textbox is read only? If so, how do I make it not read only?

 
ASKER CERTIFIED SOLUTION
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of lezloh

ASKER

I used the following code to complete this:

            TextBox signer = (TextBox)myDetails.Rows[12].Cells[1].Controls[0];