ListView和多个动态创建的控件:关于ASP问题 [英] Question concerning asp:listview and multiple dynamically created controls
问题描述
我有一个显示获得点击一个按钮创建文本框列表列表视图。我也想一个标签被旁边会增加什么是每个txtbox创建从步x说:步骤X + 1:
I have a listview that displays a list of textboxes that get created on a button click. I would also like a label to be created next to each txtbox that would increment what is says from step x: to step x+1:
我需要为此创造另一个ListView控件,或者是有一个更简单的方法(我希望)?
Do I need to create another listview control for this, or is there a much easier way (which I hope)?
下面是当前Web code为我的ListView:
Here is the current web code for my listview:
<tr align="center" valign="middle">
<td>
<asp:ListView ID="lvDynamicTextboxes" runat="server" ItemPlaceholderID="itemPlaceholder" onitemdatabound="lvDynamicTextboxes_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblStep" runat="server" Text="Step 1:" />
<asp:TextBox ID="txtStep" runat="server" TextMode="MultiLine" Rows="3" Width="300" style="margin-top:10px;" />
</ItemTemplate>
</asp:ListView>
<br /><asp:Button ID="btnAddNewStep" runat="server" Text="Add another step" onclick="btnAddNewStep_Click" style="margin-top:5px;" />
</td>
</tr>
这里是code-背后
And here is the code-behind
protected void btnAddNewStep_Click( object sender, EventArgs e )
{
this.UpdateDataSource();
this.IncrementTextboxCount();
this.BindListView();
}
private void BindListView()
{
//create an enumerable range based on the current count
List< string > dataSource = this.GetDataSource();
//bind the listview
this.lvDynamicTextboxes.DataSource = dataSource;
this.lvDynamicTextboxes.DataBind();
}
private void IncrementTextboxCount()
{
List< string > dataSource = this.GetDataSource();
dataSource.Add( string.Empty );
this.SetDataSource( dataSource );
}
private List< string > GetDataSource()
{
List< string > dataSource = null;
if ( ViewState[ "DataSource" ] != null )
dataSource = ( List< string > )ViewState[ "DataSource" ];
else
{
dataSource = new List< string >();
dataSource.Add( string.Empty );
ViewState[ "DataSource" ] = dataSource;
}
return dataSource;
}
private void UpdateDataSource()
{
List< string > dataSource = new List< string >();
foreach ( ListViewItem item in this.lvDynamicTextboxes.Items )
if ( item is ListViewDataItem )
{
TextBox txt = (TextBox)item.FindControl( "txtStep" );
dataSource.Add( txt.Text );
}
this.SetDataSource( dataSource );
}
protected void lvDynamicTextboxes_ItemDataBound( object sender, ListViewItemEventArgs e )
{
if ( e.Item is ListViewDataItem )
{
TextBox txt = (TextBox)e.Item.FindControl( "txtStep" );
txt.Text = ( (ListViewDataItem)e.Item ).DataItem.ToString();
}
}
private void SetDataSource( List< string > dataSource )
{
ViewState[ "DataSource" ] = dataSource;
}
编辑:
由于似乎有点混乱,我会尽力澄清:
Since there seems to be a bit of confusion, I'll try to clarify:
截至目前,我有一个列表视图的文本框下方有一个按钮。
As of now, I have a textbox in a listview with a button underneath.
________
| txtbox |
|________|
_____
|_btn_|
当你点击一个按钮,它会产生另一个文本框,所以点击两次造成的:
When you click a button, it generates another text box, so clicking it twice results in this:
________
| txtbox |
|________|
________
| txtbox |
|________|
________
| txtbox |
|________|
_____
|_btn_|
这些文本框是创建一个过程的步骤,因此,所有我想做的是下一个增加一个生成的标签给每个生成的文本框说哪个步骤是。所以,我希望它看起来是这样的:
These textboxes are to create steps in a process, so all I would like to do is add a generated label next to each generated textbox to say which step it is. So I want it to look like this:
________
["Step 1"] | txtbox |
|________|
________
["Step 2"] | txtbox |
|________|
________
["Step 3"] | txtbox |
|________|
_____
|_btn_|
如果他们再次单击该按钮,然后把它与文字产生另一个标签第4步
And if they click the button again, then another label is generated with the text "Step 4"
推荐答案
我最终做重复的功能包含标签另一列表视图,然后把它们放在一个表。这不是pretty,但它的作品。我还添加下一个最新的一个文本框删除链接。只是抬起头来的人通过code期待。
I ended up making duplicate functions for another listview that contained the labels and then putting them in a table. It's not pretty, but it works. I've also added a remove link next the newest textbox. Just a heads up for anyone looking through the code.
<table>
<tr>
<td valign="top">
<asp:ListView ID="lvDynamicLabels" runat="server" ItemPlaceholderID="itemPlaceholder2" onitemdatabound="lvDynamicLabels_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder2" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblStep" runat="server" Width="100px" style="margin-top:30px; margin-bottom:16px;" />
</ItemTemplate>
</asp:ListView>
</td>
<td>
<asp:ListView ID="lvDynamicTextboxes" runat="server" ItemPlaceholderID="itemPlaceholder" onitemdatabound="lvDynamicTextboxes_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:TextBox ID="txtStep" runat="server" TextMode="MultiLine" Rows="3" Width="275px" style="margin-top:10px;" />
</ItemTemplate>
</asp:ListView>
<asp:LinkButton ID="lnkRemove" runat="server" Text="Remove" Visible="false" OnClick="lnkRemove_Click" style="font-size:small; position:absolute; margin-top:30px;" />
<br /><asp:Button ID="btnAddNewStep" runat="server" Text="Add another step" onclick="btnAddNewStep_Click" style="margin-top:5px;" />
</td>
</tr>
</table>
和背后的code:
protected void lnkRemove_Click( object sender, EventArgs e )
{
UpdateDataSource( true );
UpdateLabelDataSource( true );
BindListView();
BindLabelListView();
if ( lvDynamicTextboxes.Items.Count == 1 )
lnkRemove.Visible = false;
}
private void BindListView()
{
List< string > dataSource = this.GetDataSource();
this.lvDynamicTextboxes.DataSource = dataSource;
this.lvDynamicTextboxes.DataBind();
}
private void IncrementTextboxCount()
{
List< string > dataSource = this.GetDataSource();
dataSource.Add( string.Empty );
this.SetDataSource( dataSource );
}
private List< string > GetDataSource()
{
List< string > dataSource = null;
if ( ViewState[ "DataSource" ] != null )
dataSource = ( List< string > )ViewState[ "DataSource" ];
else
{
dataSource = new List< string >();
dataSource.Add( string.Empty );
ViewState[ "DataSource" ] = dataSource;
}
return dataSource;
}
private void UpdateDataSource( bool delete )
{
List< string > dataSource = new List< string >();
foreach ( ListViewItem item in this.lvDynamicTextboxes.Items )
if ( item is ListViewDataItem )
{
TextBox txt = (TextBox)item.FindControl( "txtStep" );
dataSource.Add( txt.Text );
}
if ( delete )
dataSource.RemoveRange( dataSource.Count-1, 1 );
this.SetDataSource( dataSource );
}
protected void lvDynamicTextboxes_ItemDataBound( object sender, ListViewItemEventArgs e )
{
if ( e.Item is ListViewDataItem )
{
TextBox txt = (TextBox)e.Item.FindControl( "txtStep" );
txt.Text = ( (ListViewDataItem)e.Item ).DataItem.ToString();
}
}
private void SetDataSource( List< string > dataSource )
{
ViewState[ "DataSource" ] = dataSource;
}
private void BindLabelListView()
{
List< string > lblDataSource = this.GetLabelDataSource();
//bind the listview
this.lvDynamicLabels.DataSource = lblDataSource;
this.lvDynamicLabels.DataBind();
}
private void IncrementLabelCount()
{
List< string > lblDataSource = this.GetLabelDataSource();
lblDataSource.Add( "Step " + ( lblDataSource.Count + 1 ) );
this.SetLabelDataSource( lblDataSource );
}
private List< string > GetLabelDataSource()
{
List< string > lblDataSource = null;
if ( ViewState[ "lblDataSource" ] != null )
lblDataSource = ( List< string > )ViewState[ "lblDataSource" ];
else
{
lblDataSource = new List< string >();
lblDataSource.Add( "Step 1" );
ViewState[ "lblDataSource" ] = lblDataSource;
}
return lblDataSource;
}
private void UpdateLabelDataSource( bool delete )
{
List< string > lblDataSource = new List< string >();
int count = 1;
foreach ( ListViewItem item in this.lvDynamicLabels.Items )
if ( item is ListViewDataItem )
{
Label lbl = (Label)item.FindControl( "lblStep" );
lbl.Text = "Step " + count;
lblDataSource.Add( lbl.Text );
count++;
}
if ( delete )
lblDataSource.RemoveRange( lblDataSource.Count-1, 1 );
this.SetLabelDataSource( lblDataSource );
}
protected void lvDynamicLabels_ItemDataBound( object sender, ListViewItemEventArgs e )
{
if ( e.Item is ListViewDataItem )
{
Label lbl = (Label)e.Item.FindControl( "lblStep" );
lbl.Text = ( (ListViewDataItem)e.Item ).DataItem.ToString();
}
}
private void SetLabelDataSource( List< string > lblDataSource )
{
ViewState[ "lblDataSource" ] = lblDataSource;
}
这篇关于ListView和多个动态创建的控件:关于ASP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!