我有一个显示是那些获得点击一个按钮创建文本框列表的列表视图。 我也想一个标签来创建旁边会增加什么是步x表示每个txtbox:步骤X + 1:
我需要为此创造另一个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>
这里是代码隐藏
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;
}
编辑::
由于似乎有点混乱,我会尽力澄清:
截至目前,我在下面带有按钮列表视图一个文本框。
________
| txtbox |
|________|
_____
|_btn_|
当你点击一个按钮,它会产生另一个文本框,所以点击两次在这导致:
________
| txtbox |
|________|
________
| txtbox |
|________|
________
| txtbox |
|________|
_____
|_btn_|
这些文本框是创建一个过程的步骤,因此,所有我想做的是下一个加生成的标签给每个生成文本框说哪个步骤是。 所以,我希望它看起来是这样的:
________
["Step 1"] | txtbox |
|________|
________
["Step 2"] | txtbox |
|________|
________
["Step 3"] | txtbox |
|________|
_____
|_btn_|
如果他们再次单击该按钮,然后另一个标签与文本“第4步”产生
看起来你已经得到了所有你需要的代码......只是缺少一件事......添加时设置为0,在Page_Load中的私有类变量。 然后增加它在你的ItemDataBound来得到当前的步数。 然后调用FindControl已为您的标签,就像文本框...并更改lblStep.Text“步骤X”。 没什么吧。
一个数据源绑定到ListView控件之前,我建议在BindListView()备查,自己创建对象的新名单,那么你可以很容易地结合,就像这样:
this.lvDynamicLabels.DataSource =
from items in lblDataSource
select new
{
Value = items,
Index = strings.IndexOf(items) + 1
};
并简单地结合在列表视图数据:
<asp:Label ... Text='<%# Bind(Index) %>' />
<asp:Button ... Text='<%# Bind("Value")%>' />
,再加上你现在可以拒绝方法IncrementTextboxCount,也可自订指标值(使之成为“第一步”,目前其“1”)
我最终使重复的功能包含的标签,然后把它们放在一个表中的另一列表视图。 这不是很漂亮,但它的工作原理。 我还添加下一个最新的一个文本框删除链接。 只是抬起头来的人通过阅读代码。
<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>
而后面的代码:
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;
}
我不明白为什么你最后从分隔条件的文本框的标签。 为什么不离开模板你有它最初,并在后面的代码中此方法:
protected void lvDynamicLabels_ItemDataBound( object sender, ListViewItemEventArgs e )
{
if ( e.Item is ListViewDataItem )
{
Label lbl = (Label)e.Item.FindControl( "lblStep" );
lbl.Text = "Step " + (datasource.Count + 1).ToString();
TextBox txt = (TextBox)e.Item.FindControl( "txtStep" );
txt.Text = ( (ListViewDataItem)e.Item ).DataItem.ToString();
}
}