【问题标题】:Use checkBox and textBox in DataList在 DataList 中使用 checkBox 和 textBox
【发布时间】:2013-01-17 00:04:57
【问题描述】:

我在 asp.net 中创建了一个 DataList -

<asp:DataList runat="server" ID="pTextBox">

<ItemTemplate>

<asp:CheckBox ID="CheckBoxPN" runat="server"  Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>&nbsp;


</ItemTemplate>
</asp:DataList>

这会根据从 webService 传递的字符串值创建复选框和文本框。

当用户单击CheckBoxPN 并使用该字符串值在页面上的DataList 之外填充另一个文本框时,如何获取profileTextBox 文本字符串值??

【问题讨论】:

  • “我怎样才能得到”...在哪里?客户端还是服务器端?
  • 将字符串值传递给页面上的另一个文本框,而不是我应该提到的 DataList。立即编辑。

标签: c# asp.net string checkbox


【解决方案1】:

您可以使用CheckBoxCheckedChanged 事件并将它的NamingContainer 转换为DataListItem,您只需使用FindControl 即可找到不同的服务器控件:

protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
    CheckBox chk = (CheckBox) sender;
    DataListItem item = (DataListItem) chk.NamingContainer;
    TextBox txt = (TextBox) item.FindControl("profileTextBox");
    this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}

顺便说一句,这种方法适用于任何网络数据绑定控件(RepeaterGridView 等)

【讨论】:

  • 我试过你的解决方案并将这个添加到 aspx - 但是,如果我在调试时在“CheckBoxPN_CheckedChanged”上设置断点,当我在其上设置断点并且未检索到值时,它不会到达事件??
  • @Jambo:我假设您在回发时错误地对 DataList 进行了数据绑定。只需在第一次加载时执行此操作。所以这可能会在Page_Load 中解决它:if(!IsPostBack){DataBindDataList();}
  • 看来你是个英雄。繁荣!完毕!非常感谢。
猜你喜欢
  • 2018-03-13
  • 2013-07-16
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 2012-11-02
  • 2011-02-04
相关资源
最近更新 更多