何将TextBox文本传递给DetailsView中的TextB

何将TextBox文本传递给DetailsView中的TextB

本文介绍了如何将TextBox文本传递给DetailsView中的TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我在DetailsView外面有一个TextBox(我们称之为TextBox1),而在DetailsView中有另一个TextBox(TextBox2)。用户在TextBox1中输入并希望将文本传递给TextBox2,并在单击Button1后显示其中的文本。怎么办呢? ASPX: < asp:TextBox ID = TextBox1 runat = server / > < asp:按钮 ID = Button1 runat = server 文本 = 按钮 OnClick = Button1_Click / > < asp:DetailsView ID = DetailsView1 runat = server AutoGenerateRows = 错误 > < 字段 > < asp:TemplateField HeaderText = 获取名称 > < ItemTemplate > < asp:TextBox ID = TextBox2 runat = server ReadOnly = True / > < / ItemTemplate > < / asp:TemplateField > < / Fields > < / asp:DetailsView > 我已经在下面的代码中尝试了这个,但由于某些原因,我得到以下e rror: System.NullReferenceException:对象引用未设置为对象的实例。 protected void Button1_Click( object sender,EventArgs e) { TextBox tb2 = this .DetailsView1.FindControl( TextBox2) as TextBox; tb2.Text = this .TextBox1.Text; } 由于放置了一个断点,我认为this.TextBox1.Text是空的。 /> 所以我摆脱了这个。如下: protected void Button1_Click( object sender,EventArgs e) { TextBox tb2 = DetailsView1.FindControl( TextBox2) as TextBox; tb2.Text = TextBox1.Text; } 现在TextBox1.Text返回用户输入的文本,但它仍然显示相同的错误。看起来,即使TextBox1.Text返回用户输入,也没有任何内容被分配到tb2.Text。有人可以解释这个问题并告诉我正确的方法吗? 提前谢谢。 ASPX <%@ Page 标题 = 语言 = C# MasterPageFile = 〜/ Main / MasterPage.master AutoEventWireup = true CodeFile = AddStock.aspx.cs 继承 = PosStock %> <%@ 注册 程序集 = AjaxControlToolkit 命名空间 = AjaxControlToolkit TagPrefix = asp %> < asp:内容 ID = Content1 ContentPlaceHolderID = headContent Runat = 服务器 > < / asp:内容 > < asp:内容 ID = Content2 ContentPlaceHolderID = 主要内容 Runat = 服务器 > < div class = contentboxh > < h2 class = hpos > 添加新产品< / h2 > < br / > < h2 class = hpos1 style = text-align:left > 新的股票报名表< / h2 > < br / > < br / > < asp:标签 ID = lblRequiredField runat = server 文本 = *必填字段 宽度 = 190 高度 = 30 ForeColor = 红色 Font-Bold = True style = text-align:right / > < br / > < asp:标签 ID = lblCategory runat = server 文字 = *类别: 宽度 = 190px 高度 = 30px style = text-align:right > < / asp:Label > < asp:DropDownList ID = ddlCategory runat = server AppendDataBoundItems = true DataSourceID = SqlDataSource1 DataTextField = stock_category DataValueField = stock_category 高度 = 30px 宽度 = 190px OnSelectedIndexChanged = DdlCategory_SelectedIndexChanged OnDataBound = DdlCategory_DataBound AutoPostBack = True > < asp:ListItem 文本 = 选择类别 值 = 选择类别 > < / asp:ListItem > < / asp:DropDownList > < asp:SqlDataSource ID = SqlDataSource1 runat = server ConnectionString = <% $ ConnectionStrings:MJENTAConnectionString %> SelectCommand = SELECT DISTINCT [stock_category] ​​FROM [Stock] > < / asp:SqlDataSource > < asp:标签 ID = lblNewCategory runat = server 文本 = 请指定类别: 可见 = 错误 宽度 = 190p x 高度 = 30px style = text-align:right > < / asp:标签 > < asp:TextBox ID = tbNewCategory runat = server 高度 = 30px 可见 = 错误 宽度 = 190px 占位符 = 输入新类别 > < / asp:TextBox > < br / > < asp:标签 ID = lblBrand runat = server 文字 = 品牌: 宽度 = 190 高度 = 30 style = text-align:right > < / asp:标签 > < asp:TextBox ID = tbBrand runat = server 高度 = 30px 宽度 = 190px 占位符 = 输入品牌 > < / asp:TextBox > < br / > < asp:标签 ID = lblName runat = server 文本 = *产品名称: 宽度 = 190px 高度 = 30px style = text-align:right > < / asp:标签 > < asp:TextBox ID = tbName runat = server 高度 = 30px 宽度 = 190px 占位符 = 输入产品名称 > < / asp:TextBox > < br / > < asp:Label ID = lblRange runat = server 文本 = 产品范围: 宽度 = 190px 高度 = 30px style = text-align:right > < / a sp:标签 > < asp:TextBox ID = tbRange runat = server 高度 = 30px 宽度 = 190px 占位符 = 输入产品系列 > < / asp:TextBox > < br / > < asp:Label ID = lblLine runat = server 文字 = 产品系列: 宽度 = 190px 高度 = 30px style = text-align:right > < / asp:Label > < asp:TextBox ID = tbLine runat = server 高度 = 30px 宽度 = 190px 占位符 = 输入产品系列 > < / asp:TextBox > < br / > < asp:标签 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\"></a sp: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\" He ight=\"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\" r unat=\"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=\"Sql DataSource2\" 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=\"t ext-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\" /> $ b$b <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=\"updPnlCon firm\" 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=\"tbDvCateg ory\" 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:TemplateFiel d 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&g t; <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> <d iv 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 using System; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.Security; 使用 System.Web.UI.HtmlControls; 使用 System.Web.UI.WebControls; 使用 System.Web.UI.WebControls.WebParts; 使用 System.Configuration; 使用 System.Data.SqlClient; using System.Data.SqlTypes; 使用 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 = dvConfirmNewStoc k.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) { } } 解决方案 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=\"1 90px\" 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=\"tbBr and\" runat=\"server\" Height=\"30px\" Width=\"190px\" Placeholder=\"Enter brand\"></asp:TextBox> <br /& gt; <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\"></a sp: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\" He ight=\"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\" r unat=\"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=\"Sql DataSource2\" 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:Dro pDownList> <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\" /& gt; <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> $ b$b <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> $ b$b </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 using System; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.Security; 使用 System.Web.UI.HtmlControls; 使用 System.Web.UI.WebControls; 使用 System.Web.UI.WebControls.WebParts; 使用 System.Configuration; 使用 System.Data.SqlClient; using System.Data.SqlTypes; 使用 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 = dvConfirmNewStoc k.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) { } }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... TextBox tb2 = DetailsView1.Rows[0].FindControl(\"TextBox2\") as TextBox;This is to find TextBox present on first row.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: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.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: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<%@ 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 behindusing 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) { }} 解决方案 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 behindusing 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) { }}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...TextBox tb2 = DetailsView1.Rows[0].FindControl("TextBox2") as TextBox;This is to find TextBox present on first row. 这篇关于如何将TextBox文本传递给DetailsView中的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 14:01