Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, suppose I have a TextBox (let's call it TextBox1) outside a DetailsView and another TextBox (TextBox2) inside the DetailsView. An user inputs in the TextBox1 and wants to pass the text to the TextBox2 and show the text in it after clicking on Button1. How can this be done?

ASPX:
ASP.NET
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False">
    <Fields>
        <asp:TemplateField HeaderText="Acquisition Name">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" ReadOnly="True" />
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>


I have already tried this below in code behind but for some reasons, I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

C#
protected void Button1_Click(object sender, EventArgs e)
{
    TextBox tb2 = this.DetailsView1.FindControl("TextBox2") as TextBox;
    tb2.Text = this.TextBox1.Text;
}


As a result of putting a breakpoint, I figured "this.TextBox1.Text" is empty.

So I got rid of "this." as follows:

C#
protected void Button1_Click(object sender, EventArgs e)
{
    TextBox tb2 = DetailsView1.FindControl("TextBox2") as TextBox;
    tb2.Text = TextBox1.Text;
}


Now TextBox1.Text returns the text that the user inputs however it is still showing the same error. Seemingly, nothing is getting assigned into "tb2.Text" even though TextBox1.Text is returning the user input. Could someone please explain this issue and show me the proper way to do this?

Thank you in advance.


ASPX
ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Main/MasterPage.master" AutoEventWireup="true" CodeFile="AddStock.aspx.cs" Inherits="PosStock" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="headContent" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Maincontent" Runat="Server">

    <div class="contentboxh">
        <h2 class="hpos">Add New Products</h2>
        <br />
        <h2 class="hpos1" style="text-align: left">New stock entry form</h2>
        <br />
        <br />
        <asp:Label ID="lblRequiredField" runat="server" Text="* Required Field" Width="190" Height="30" ForeColor="Red" Font-Bold="True" style="text-align: right" />
        <br />
        <asp:Label ID="lblCategory" runat="server" Text="* Category: " Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:DropDownList ID="ddlCategory" runat="server" AppendDataBoundItems="true" DataSourceID="SqlDataSource1" DataTextField="stock_category" DataValueField="stock_category" Height="30px" Width="190px" OnSelectedIndexChanged="DdlCategory_SelectedIndexChanged" OnDataBound="DdlCategory_DataBound" AutoPostBack="True">
            <asp:ListItem Text="Select Category" Value="Select Category"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MJENTAConnectionString %>" SelectCommand="SELECT DISTINCT [stock_category] FROM [Stock]"></asp:SqlDataSource>
        <asp:Label ID="lblNewCategory" runat="server" Text="Please specify the category:" Visible="False" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbNewCategory" runat="server" Height="30px" Visible="False" Width="190px" Placeholder="Enter a new category"></asp:TextBox>
        <br />
        <asp:Label ID="lblBrand" runat="server" Text="Brand:" Width="190" Height="30" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbBrand" runat="server" Height="30px" Width="190px" Placeholder="Enter brand"></asp:TextBox>
        <br />
        <asp:Label ID="lblName" runat="server" Text="* Product Name:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbName" runat="server" Height="30px" Width="190px" Placeholder="Enter product name"></asp:TextBox>
        <br />
        <asp:Label ID="lblRange" runat="server" Text="Product Range:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbRange" runat="server" Height="30px" Width="190px" Placeholder="Enter product range"></asp:TextBox>
        <br />
        <asp:Label ID="lblLine" runat="server" Text="Product Line:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbLine" runat="server" Height="30px" Width="190px" Placeholder="Enter product line"></asp:TextBox>
        <br />
        <asp:Label ID="lblYear" runat="server" Text="Year:" Width="190px" Height="30px" style="text-align: right" />
        <asp:TextBox ID="tbYear" runat="server" Width="190px" Height="30px" Placeholder="Enter model year" />
        <br />
        <div style="padding-removed 0px; float:left;">
        <asp:Label ID="lblDescription" runat="server" Text="Product Description:" Width="194px" Height="60px" style="text-align: right"></asp:Label>
        </div>
        <asp:TextBox ID="tbDescription" runat="server" Width="570" Height="60" TextMode="multiline" Placeholder="Enter product description"></asp:TextBox>
        <br />
        <asp:Label ID="lblSize" runat="server" Text="Product Size:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbSize" runat="server" Height="30px" Width="95px"></asp:TextBox>
        <br />
        <asp:Label ID="lblWeight" runat="server" Text="Product Weight:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbWeight" runat="server" Height="30px" Width="95px"></asp:TextBox>
        <asp:DropDownList ID="ddlWeight" runat="server" Height="30px" Width="95px">
            <asp:ListItem Value="0">g</asp:ListItem>
            <asp:ListItem Value="1">kg</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Label ID="lblColour" runat="server" Text="Product Colour:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbColour" runat="server" Width="190" Height="30" Placeholder="Enter product colour"></asp:TextBox>
        <br />
        <asp:Label ID="lblStrung" runat="server" Text="Strung: " Width="190px" Height="30px" style="text-align: right" />
        <asp:DropDownList ID="ddlStrung" runat="server" Width="95px" Height="30px">
            <asp:ListItem Selected="True" Value="N/A">N/A</asp:ListItem>
            <asp:ListItem Value="Yes">Yes</asp:ListItem>
            <asp:ListItem Value="No">No</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Label ID="lblTax" runat="server" Text="* Tax: " Width="190px" Height="30px" style="text-align: right" />
        <asp:TextBox ID="tbTax" runat="server" Width="95px" Height="30px" Text="10" Font-Size="Medium" />
        <asp:Label ID="lblPercent" runat="server" Text="%" Width="30px" Height="30px" style="text-align: left" />
        <asp:RangeValidator ID="rvTax" runat="server" ControlToValidate="tbTax" MaximumValue="100" MinimumValue="0" Type="Integer"
            ErrorMessage="Invalid input!" Visible="False" />
        <asp:Label ID="lblDefaultTax" runat="server" Text="10% as default" Width="190px" Height="30px" style="text-align: left" ForeColor="Red" />
        <br />
        <asp:Label ID="lblCost" runat="server" Text="* Wholesale Price:" Width="190px" Height="30px" style="text-align: right" />
        <asp:TextBox ID="tbCost" runat="server" Width="95" Height="30" Placeholder="Enter price" />
        <asp:RangeValidator ID="rvCost" runat="server" ControlToValidate="tbCost" Type="Double"
            ErrorMessage="Invalid input!" />
        <br />
        <asp:Label ID="lblRetail" runat="server" Text="* Retail Price:" Width="190px" Height="30px" style="text-align: right" />
        <asp:TextBox ID="tbRetail" runat="server" Width="95" Height="30" Placeholder="Enter price" />
        <asp:RangeValidator ID="rvRetail" runat="server" ControlToValidate="tbRetail" Type="Double"
            ErrorMessage="Invalid input!" />
        <br />
        <asp:Label ID="lblBarcode" runat="server" Text="* Barcode:" Width="190px" Height="30px" style="text-align: right" />
        <asp:TextBox ID="tbBarcode" runat="server" Width="190" Height="30" Placeholder="Barcode" />
        <asp:Label ID="lblBarcodeMsg" runat="server" Text="Scan the product barcode with a barcode scanner" Width="380px" Height="30px" style="text-align: left" ForeColor="Red" />
        <br />
        <asp:Label ID="lblDistributor" runat="server" Text="* Distributor:" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:DropDownList ID="ddlDistributor" runat="server" AppendDataBoundItems="True" Width="190px" Height="30px" DataSourceID="SqlDataSource2" DataTextField="distributor_name" DataValueField="distributor_name" OnSelectedIndexChanged="DdlDistributor_SelectedIndexChanged" OnDataBound="DdlDistributor_DataBound" AutoPostBack="True">
            <asp:ListItem Text="Select Distributor" Value="Select Distributor"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MJENTAConnectionString %>" SelectCommand="SELECT DISTINCT [distributor_name] FROM [Distributor]"></asp:SqlDataSource>
        <asp:Label ID="lblNewDistributor" runat="server" Text="Please add a new distributor:" Visible="False" Width="190px" Height="30px" style="text-align: right"></asp:Label>
        <asp:TextBox ID="tbNewDistributor" runat="server" AutoPostBack="True" Height="30px" Visible="False" Width="190px" Placeholder="Enter a new distributor"></asp:TextBox>
        <br />
        <asp:Label ID="lblListingType" runat="server" Text="Listing Type: " Width="190px" Height="30px" style="text-align: right" />
        <asp:DropDownList ID="ddlListingType" runat="server" Width="190px" Height="30px">
            <asp:ListItem>Select Listing Type</asp:ListItem>
            <asp:ListItem Value="Public">Public</asp:ListItem>
            <asp:ListItem Value="Private">Private</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Label ID="lblRequiredFieldWarning" runat="server" Text="* Please fill in the highlighted required field(s)" Width="480px" Height="30px" Visible="False" ForeColor="Red" CssClass="warninglabel" Font-Bold="True" Font-Size="X-Large"></asp:Label>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit Entry" BackColor="#7AB800" CssClass="submitbutton" Width="120px" OnClick="BtnSubmit_Click"  />
        <asp:Button ID="btnCancel1" runat="server" Text="Cancel" BackColor="#FF9900" CssClass="cancelbutton" Width="120px" OnClick="BtnCancel1_Click" />
    </div>


    <div class="contentboxh">

        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />

        <asp:Button ID="btnShowPopup" runat="server" style="display:none" />
        <asp:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlPopup"
                            CancelControlID="btnCancel" BackgroundCssClass="modalBackground" />
        <asp:Panel ID="pnlPopup" runat="server" CssClass="detail" Width="500px">
            <asp:UpdatePanel ID="updPnlConfirm" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Label ID="lblConfirm" runat="server" Text="Confirm the following details of the Stock:" BackColor="#7AB800" Width="95%" />
                    <asp:DetailsView ID="dvConfirmNewStock" runat="server" AutoGenerateRows="False" BackColor="White" Width="95%" FieldHeaderStyle-Width="200px">

                        <FieldHeaderStyle Width="200px" />
                        <Fields>
                            <asp:TemplateField HeaderText="Category">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvCategory" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Brand">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvBrand" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Name">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvName" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Range">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvRange" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Line">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvLine" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Year">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvYear" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvDescription" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Size">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvSize" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Weight">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvWeight" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Colour">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvColour" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Strung">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvStrung" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Tax">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvTax" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Wholesale Price">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvCost" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Retail Price">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvRetail" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Barcode">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvBarcode" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Distributor">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvDistributor" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Listing Type">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbDvListingType" runat="server" With="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Fields>
                    </asp:DetailsView>
                </ContentTemplate>
            </asp:UpdatePanel>
            <div align="right" style="width:95%">
                <asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="BtnConfirm_OnClick" Height="26px" Width="80px" BackColor="#7AB800" CssClass="viewdetailbutton" />
                <asp:Button ID="btnCancel2" runat="server" Text="Cancel" Height="26px" Width="80px" BackColor="#FF9900" CssClass="viewdetailbutton" />
            </div>
        </asp:Panel>
    </div>
</asp:Content>


Code behind
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

public partial class PosStock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // Category DropDownList
    protected void DdlCategory_DataBound(object sender, EventArgs e)
    {
        ddlCategory.Items.Add("Other");
    }
    protected void DdlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCategory.SelectedItem.Text == "Other")
        {
            lblNewCategory.Visible = true;
            tbNewCategory.Visible = true;
        }
        else
        {
            lblNewCategory.Visible = false;
            tbNewCategory.Visible = false;
        }
    }
    // Distributor DropDownList
    protected void DdlDistributor_DataBound(object sender, EventArgs e)
    {
        ddlDistributor.Items.Add("Other");
    }
    protected void DdlDistributor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDistributor.SelectedItem.Text == "Other")
        {
            lblNewDistributor.Visible = true;
            tbNewDistributor.Visible = true;
        }
        else
        {
            lblNewDistributor.Visible = false;
            tbNewDistributor.Visible = false;
        }
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        // Category error check
        if (ddlCategory.SelectedIndex == 0)
        {
            lblCategory.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (ddlCategory.SelectedIndex > 0)
        {
            lblCategory.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        if (ddlCategory.SelectedItem.Text == "Other")
        {
            if (tbNewCategory.Text == String.Empty)
            {
                lblNewCategory.ForeColor = System.Drawing.Color.Red;
                lblRequiredFieldWarning.Visible = true;
            }
            else if (tbNewCategory.Text != String.Empty)
            {
                lblNewCategory.ForeColor = System.Drawing.Color.Black;
                lblRequiredFieldWarning.Visible = false;
            }
        }
        // Name error check
        if (tbName.Text == String.Empty)
        {
            lblName.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (tbName.Text != String.Empty)
        {
            lblName.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        // Tax error check
        if (tbTax.Text == String.Empty)
        {
            lblTax.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (tbTax.Text != String.Empty)
        {
            lblTax.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        // Wholesale Price error check
        if (tbCost.Text == String.Empty)
        {
            lblCost.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (tbCost.Text != String.Empty)
        {
            lblCost.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        // Retail Price error check
        if (tbRetail.Text == String.Empty)
        {
            lblRetail.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (tbRetail.Text != String.Empty)
        {
            lblRetail.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        // Barcode error check
        if (tbBarcode.Text == String.Empty)
        {
            lblBarcode.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (tbBarcode.Text == String.Empty)
        {
            lblBarcode.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }
        // Distributor error check
        if (ddlDistributor.SelectedIndex == 0)
        {
            lblDistributor.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (ddlDistributor.SelectedIndex > 0)
        {
            if (tbNewDistributor.Text == String.Empty)
            {
                lblNewDistributor.ForeColor = System.Drawing.Color.Red;
                lblRequiredFieldWarning.Visible = true;
            }
            else if (tbNewDistributor.Text != String.Empty)
            {
                lblNewDistributor.ForeColor = System.Drawing.Color.Black;
                lblRequiredFieldWarning.Visible = false;
            }
        }
        // Listing Type error check
        if (ddlListingType.SelectedIndex == 0)
        {
            lblListingType.ForeColor = System.Drawing.Color.Red;
            lblRequiredFieldWarning.Visible = true;
        }
        else if (ddlListingType.SelectedIndex > 0)
        {
            lblListingType.ForeColor = System.Drawing.Color.Black;
            lblRequiredFieldWarning.Visible = false;
        }

        if (ddlCategory.SelectedIndex > 0 && tbName.Text != String.Empty && tbTax.Text != String.Empty && tbCost.Text != String.Empty &&
            tbRetail.Text != String.Empty && tbBarcode.Text != String.Empty && ddlDistributor.SelectedIndex > 0 && ddlListingType.SelectedIndex > 0
            && ddlListingType.SelectedIndex > 0)
        {
            TextBox tbDvCategory = dvConfirmNewStock.Rows[0].FindControl("tbDvCategory") as TextBox;
            tbDvCategory.Text = ddlCategory.SelectedItem.Text;
            TextBox tbDvBrand = dvConfirmNewStock.Rows[1].FindControl("tbDvBrand") as TextBox;
            tbDvBrand.Text = tbBrand.Text;
            TextBox tbDvName = dvConfirmNewStock.Rows[2].FindControl("tbDvName") as TextBox;
            tbDvName.Text = tbName.Text;
            TextBox tbDvRange = dvConfirmNewStock.Rows[3].FindControl("tbDvRange") as TextBox;
            tbDvRange.Text = tbRange.Text;
            TextBox tbDvLine = dvConfirmNewStock.Rows[4].FindControl("tbDvLine") as TextBox;
            tbDvLine.Text = tbLine.Text;
            TextBox tbDvYear = dvConfirmNewStock.Rows[5].FindControl("tbDvYear") as TextBox;
            tbDvYear.Text = tbYear.Text;
            TextBox tbDvDescription = dvConfirmNewStock.Rows[6].FindControl("tbDvDescription") as TextBox;
            tbDvDescription.Text = tbDescription.Text;
            TextBox tbDvSize = dvConfirmNewStock.Rows[7].FindControl("tbDvSize") as TextBox;
            tbDvSize.Text = tbSize.Text;
            TextBox tbDvWeight = dvConfirmNewStock.Rows[8].FindControl("tbDvWeight") as TextBox;
            tbDvWeight.Text = tbWeight.Text;
            TextBox tbDvColour = dvConfirmNewStock.Rows[9].FindControl("tbDvColour") as TextBox;
            tbDvColour.Text = tbColour.Text;
            TextBox tbDvStrung = dvConfirmNewStock.Rows[10].FindControl("tbDvStrung") as TextBox;
            tbDvStrung.Text = ddlStrung.SelectedItem.Text;
            TextBox tbDvTax = dvConfirmNewStock.Rows[11].FindControl("tbDvTax") as TextBox;
            tbDvTax.Text = tbTax.Text;
            TextBox tbDvCost = dvConfirmNewStock.Rows[12].FindControl("tbDvCost") as TextBox;
            tbDvCost.Text = tbCost.Text;
            TextBox tbDvRetail = dvConfirmNewStock.Rows[13].FindControl("tbDvRetail") as TextBox;
            tbDvRetail.Text = tbRetail.Text;
            TextBox tbDvBarcode = dvConfirmNewStock.Rows[14].FindControl("tbDvBarcode") as TextBox;
            tbDvBarcode.Text = tbBarcode.Text;
            TextBox tbDvDistributor = dvConfirmNewStock.Rows[15].FindControl("tbDvDistributor") as TextBox;
            tbDvDistributor.Text = ddlDistributor.SelectedItem.Text;
            TextBox tbDvListingType = dvConfirmNewStock.Rows[16].FindControl("tbDvListingType") as TextBox;
            tbDvListingType.Text = ddlListingType.SelectedItem.Text;

            updPnlConfirm.Update();
            mdlPopup.Show();
        }
    }
    protected void BtnCancel1_Click(object sender, EventArgs e)
    {
        Response.Redirect("InvStock.aspx");
    }
    protected void BtnConfirm_OnClick(object sender, EventArgs e)
    {

    }
}
Posted
Updated 24-Oct-14 16:22pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Oct-14 11:25am    
System.NullReferenceException is exception, not "error". (Do you know the difference.)
So, the debugger will show you where and what was null.
—SA
ThatGuy7 23-Oct-14 11:39am    
OK so how is it not assigning anything into tb2.Text?
Sergey Alexandrovich Kryukov 23-Oct-14 12:07pm    
Use the debugger...
—SA
[no name] 23-Oct-14 12:30pm    
Because it can't find tb2 to begin with, that's why it's null.

C#
TextBox tb2 = DetailsView1.FindControl("TextBox2") as TextBox;

Which TextBox from which row? That's why the error. You should get the TextBox of certain row like...
C#
TextBox tb2 = DetailsView1.Rows[0].FindControl("TextBox2") as TextBox;
This is to find TextBox present on first row.
 
Share this answer
 
v2
Comments
ThatGuy7 24-Oct-14 3:59am    
Hi Tadit thanks for your advice again!
I tried this and now I'm getting the ArgumentOutOfRangeException.
I have a screenshot of a part of my code. Could you please have a look at it and tell me what is wrong?

http://i.imgur.com/MjOEmSR.png

Thanks
That means it cannot find the Row 0. Is it really present? Use Debugger and hover on dvConfirmNewStock. See the Rows property.
ThatGuy7 24-Oct-14 4:21am    
Just checked the RowCount and it was 0. Could it be because the DetailsView dvConfirmNewStock is inside a panel and the panel is a ModalPopupExtender therefore it's not present? In which case, how can I get this right?
How do you load this Panel? Inside Page Load? Are you check for !IsPostBack?
ThatGuy7 24-Oct-14 22:36pm    
Ok Tadit, I wasn't gonna post the entire code but I thought it'd be better to put it up. As you can see the panel "pnlPopup" consists of an UpdatePanel and the DetailsView. Ps. the code is still unfinished.

This is the screenshot of what the current output looks like.

http://i.imgur.com/ugIeaW5.png

First, the user fills in the form and after clicking on "Submit" it should bring up the DetailsView in ModalPopupExtender. I want to pass all the data from the form into the TextBoxes in the DetailsView. And my answer is no to your question about checking for the postback. Could you please kindly identify the issue? Thank you so much for your help.
It is all because of setting ReadOnly="True" for TextBox2. And we have work around for this issue. Please look the solution at following links:

http://codedisplay.com/how-to-get-or-read-value-from-readonly-textbox-using-asp-net-c-vb-net/[^]

ViewState and Readonly Property of Textbox[^]

Hope those will be helpful. Feel free to put further question if any. Thanks.
 
Share this answer
 
v2
Comments
ThatGuy7 24-Oct-14 4:07am    
Hi Snesh thanks for your comment!
I got rid of ReadOnly="True" but the situation is still the same unfortunately..
But that is a good lesson I didn't know we can't get value from readonly textbox. I am a newbie just started learning ASP.NET C# last week.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900