我如何通过使用循环来获取代码背后的textbox
和textbox
通过我们生成的textboxes'.Here is my
。我想使用这些<form id="form" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
<asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" />
<asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" />
</form>
</asp:Content>
gridview`图像在gridview中插入值
ASPX:
public void show(object sender, EventArgs e)
{
string s = Session["num"].ToString();
int num = Int32.Parse(s);
for (int i =0; i <num; i++)
{
TextBox _text = new TextBox();
_text.Visible = true;
_text.ID = i.ToString();
this.form.Controls.Add(_text);
}
}
public void save(object sender, EventArgs e)
{
string s = Session["num"].ToString();
int num = Int32.Parse(s);
DataTable t = (DataTable)Session["tab"];
DataRow row1 = t.NewRow();
for (int i = 0; i < num; i++)
{
foreach (Control f in form.Controls)
{
if (f is TextBox)
{
TextBox txt = (TextBox)f;
string str = txt.Text;
row1["Name"] = str;
row1["address"] = str;
row1["num"] = str;
t.Rows.Add(row1);
}
}
}
GridView1.DataSource = t;
GridView1.DataBind();
}
ASPX.CS
show
当我点击textboxes
按钮,然后3 Save
生成(显示),我希望当我在textboxex中插入值并按gridview
按钮时,值显示在Name
在address
,num
,private void LogToForm(object line, string eventTime, Log.Severity severity) {
if (dataGridView_LogInfo.InvokeRequired) {
dataGridView_LogInfo.Invoke (
new Action<object, string, Log.Severity>(LogtoFormCallback),
new object[] { line, eventTime, severity }
);
} else {
LogtoFormCallback(line, eventTime, severity);
}
}
答案 0 :(得分:0)
您需要解决许多问题
ASPX
请注意添加PlaceHolder控件。这用于简化动态控件的管理。
<form>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
<asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" />
<asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" />
</form>
代码隐藏:
以下是我对Page_Load事件中需要进行的事情的假设
您必须每次都重新创建动态字段(CreateTbx()
),但由于您只希望它们最初通过按钮控件显示,因此您需要一种机制来确定该按钮何时被点击,因此会话[“显示“]布尔标志并初始化一次。
protected void Page_Load ( object sender, EventArgs e )
{
if ( !Page.IsPostBack )
{
Session[ "num" ] = 3;
DataTable tab = new DataTable();
Session[ "tab" ] = tab;
tab.Columns.Add( "Name" );
tab.Columns.Add( "address" );
tab.Columns.Add( "num" );
Session[ "shown" ] = false;
}
GridView1.DataSource = ( DataTable ) Session[ "tab" ];
CreateTbx();
}
此处单击“显示”按钮,通过调用CreateTbx()
创建动态字段public void show ( object sender, EventArgs e )
{
Session[ "shown" ] = true;
CreateTbx();
}
在这里,我使ID更符合传统的命名标准。
private void CreateTbx ()
{
if ( ( bool ) Session[ "shown" ] )
{
string s = Session[ "num" ].ToString();
int num = Int32.Parse( s );
for ( int i =0; i < num; i++ )
{
TextBox _text = new TextBox();
_text.Visible = true;
_text.ID = string.Format( "TbxInput{0}", i );
PlaceHolder1.Controls.Add( _text );
}
}
}
此处导入更改与原始代码相比:
Session["num"]
。创建需要Session["num"]
,而不是处理。请注意,我们只需要重新绑定而不是重新分配GridView数据源
public void save ( object sender, EventArgs e )
{
DataRow row = (( DataTable ) Session[ "tab" ]).NewRow();
for ( int i = 0; i < PlaceHolder1.Controls.Count; i++ )
{
string tbxid = string.Format( "TbxInput{0}", i );
TextBox txt = ( TextBox ) PlaceHolder1.FindControl( tbxid );
row[ i ] = txt.Text;
}
( ( DataTable ) Session[ "tab" ] ).Rows.Add( row );
GridView1.DataBind();
}
希望这有帮助