本文介绍了在c#中创建和访问动态控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据表的行数创建动态文本框。单击提交按钮后,动态文本框中输入的值将在进一步处理中使用。



运行时出现问题文本框已创建,但文本框无法进行处理。 TextBox创建代码





I am creating dynamic textboxes based on table's row count. After Click on submit button the entered values in dynamic textboxes will be used in further process.

Problem is on runtime Textbox are created but the Textbox are not accessible for process. TextBox Creation code


protected void btnSearch_Click(object sender, EventArgs e)
   {
       DataTable clientSeries = new DataTable();
       DataTable dtexistseries = new DataTable();
       int clientID = 0;
       if (!string.IsNullOrEmpty(absClientDetail.SelectedValue))
       {

           if (int.TryParse(absClientDetail.SelectedValue, out clientID))
           {

               clientSeries = DiscStructureAdvances.LoadClientRelatedDiscountStru(clientID);
               clientSeries.DefaultView.Sort = "seriesid ASC";

               dtexistseries = Serieses.LoadSeriesIdAndName();
               dtexistseries.DefaultView.Sort = "seriesid ASC";

               int seriescount = dtexistseries.Rows.Count;
               int clientSeriescount =  clientSeries.Rows.Count;
               txtnames = string.Empty;
               for (int i = 0; i < seriescount; i++)
               {
                   TextBox TxtBoxU = new TextBox();
                   Label lblU = new Label();
                   string txtboxname = "txt_" + Convert.ToString(dtexistseries.Rows[i]["seriesid"]);
                   TxtBoxU.ID = txtboxname;

                   txtnames += txtboxname;
                   txtnames += ",";

                   lblU.ID = "LabelU" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]);
                   lblU.Text = "Series:" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]); ;

                   DataView dv = new DataView(clientSeries);
                   dv.RowFilter = "seriesname = '" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]) + "' AND seriesid = " + Convert.ToInt16(dtexistseries.Rows[i]["seriesid"]) + "";

                   DataTable dt = dv.ToTable();
                   if (dt.Rows.Count > 0)
                   {
                       TxtBoxU.Text = Convert.ToDecimal(dt.Rows[0]["discount"]).ToString();
                   }
                   else
                   {
                       TxtBoxU.Text = string.Empty;
                   }

                   TxtBoxU.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                   TxtBoxU.ReadOnly = true;


                   PlaceHolder1.Controls.Add(lblU);
                   PlaceHolder1.Controls.Add(TxtBoxU);

               }
           }
       }
   }











现在已经保存了textBox Id,字符串。










Now the textBox Id are already saved ,in string.


protected void btnSave_Click(object sender, EventArgs e)
 {
     string[] individualseries = txtnames.Split(',');

     string valuetoupdate;

     TextBox t1 = PlaceHolder1.FindControl(individualseries[1]) as TextBox;

     //for placeholder
     // Get a reference to the master page
     MasterPage ctl00 = FindControl("ctl00") as MasterPage;
     // Get a reference to the ContentPlaceHolder
     ContentPlaceHolder MainContent = ctl00.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

     //now pointing to the panel
     PlaceHolder tempPanel = MainContent.FindControl("PlaceHolder1") as PlaceHolder;

     TextBox AgeTextBox = tempPanel.FindControl(individualseries[0]) as TextBox;

     //TextBox textbox = this.Master.FindControl("ContentPlaceHolder1").FindControl("Panel1").FindControl(individualseries[0]) as TextBox;// not working
     foreach (string series in individualseries)
     {
         //TextBox txt1 = (TextBox)Panel1.FindControl(series);
         TextBox txt = (TextBox)tempPanel.FindControl(series);
         decimal discount = 0;
         if ((txt.Text != string.Empty) && (string.IsNullOrEmpty(txt.Text)) && (string.IsNullOrWhiteSpace(txt.Text)))
         {
             if(decimal.TryParse(txt.Text, out discount))
             {
                 valuetoupdate += txt.Text;
             }
         }
     }
 }







请建议一些解决方案

谢谢




Please suggest some solution
thank you

推荐答案

string[] individualseries = txtnames.Split(',');

PlaceHolder tempPanel = MainContent.FindControl("PlaceHolder1") as PlaceHolder;

TextBox AgeTextBox = tempPanel.FindControl(individualseries[0]) as TextBox;



这不浪费时间:


This is not a waste of time:

Dictionary<string,TextBox> SeriesToTextBox = new DictionaryDictionary<string,TextBox>

// add the instances of TextBoxU to the Dictionary in the for loop:
SeriesToTextBoxes.Add(txtboxname, TxtBoxU);

// then, later, when you need to access the TextBox instances' Text Property:
foreach (string series in individualseries)
{
    string txt = SeriesToTextBox[series].Text;
}


<pre lang="c#">

TextBox TxtBoxU = new TextBox();

string txtboxname =txt_+ Convert.ToString(dtexistseries.Rows [i] [seriesid]);

TxtBoxU.ID = txtboxname;



现在输入的文本框值用编码输入

TxtBoxU.Text = Convert.ToDecimal(dt.Rows [0] [discount])。 ToString();



现在添加到代码命名的面板中

PlaceHolder1.Controls.Add(TxtBoxU);

之前输入的值显示在dymanic创建的文本框中。代码工作直到创建

动态TextBox和值。



这显示输入或空值。

现在主要的问题是找到动态创建的文本框并重新获取值,以便在表格中进一步插入

结构为(clientid,seriesid,Discount )

建议代码插入数据

TextBox TxtBoxU = new TextBox();
string txtboxname = "txt_" + Convert.ToString(dtexistseries.Rows[i]["seriesid"]);
TxtBoxU.ID = txtboxname;

Now for the textbox values are entered with coding
TxtBoxU.Text = Convert.ToDecimal(dt.Rows[0]["discount"]).ToString();

Now added to the panel named by the code
PlaceHolder1.Controls.Add(TxtBoxU);
The previous entered valued are shown in dymanic created textbox. Code is working till creating
dynamic TextBox and the Values.

this shows the entered or empty values.
Now the main problem is to locate the dynamic created textbox and value retrived, for further inserting
in table the structure is ("clientid","seriesid","Discount"")
proposed code to insert the data

foreach (string series in individualseries)
       {
           TextBox txt1 = (TextBox)Panel1.FindControl(series);
           decimal discount =0;
           decimal.TryParse(txt1.Text.Trim(),out discount);
           tbinsertdiscount.Append("("+ Convert.ToInt16(cd.ClientID) +","+ Convert.ToInt16(series.Split('_')[1]) + "," + discount + ")");
       }



主要问题是如何检索文本框ID和可能更改或未更改的值。



由用户BillWoodruff提出的解决方案,实现为

// outode页面的功能包括page_load和按钮点击以及用户定义的功能

// to使其可以访问整个页面

public static Dictionary< string,TextBox> SeriesToTextBox = null;



//按钮搜索




Main problem is how to retrive the textbox Id and values which might be changed or unchanged.

proposed solution by user BillWoodruff , was implemented as
// outsode the page's functions inclution page_load and button click and userdefined functins
//to make it accesable throughout page
public static Dictionary<string,TextBox> SeriesToTextBox = null ;

//in button search

<pre lang="c#">

protected void btnSearch_Click(object sender,EventArgs e)

{

SeriesToTextBox = new Dictionary< string,TextBox> ();



// for for循环

SeriesToTextBox.Add(TxtBoxU.Text,TxtBoxU); -----> wrroe显示已添加具有相同键的项目。类型ArgumentException



请建议一些解决方案。

谢谢。

protected void btnSearch_Click(object sender, EventArgs e)
{
SeriesToTextBox = new Dictionary<string,TextBox>() ;

//in for loop
SeriesToTextBox.Add(TxtBoxU.Text, TxtBoxU); -----> wrroe is shown ""An item with the same key has already been added. type ArgumentException "

Please suggest some solutions.
thanks.


这篇关于在c#中创建和访问动态控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:26