简体   繁体   English

asp.net:treeview - 如果选中则显示文本框?

[英]asp.net: treeview - show textbox if checked?

i need to display a list of strings to a user. 我需要向用户显示字符串列表。 the user can select multiple strings. 用户可以选择多个字符串。 if a specific set of strings is selected a textbox will appear next to each string. 如果选择了一组特定的字符串,则每个字符串旁边都会出现一个文本框。

what i want to do is show the user a TREEVIEW with each node being one of the strings. 我想要做的是向用户显示一个TREEVIEW,每个节点都是其中一个字符串。 they will select every string they want by checking the box. 他们会通过勾选方框选择他们想要的每个字符串。

the problem: if the user selects specific checkboxes i need a textbox to appear to GET input from the user. 问题:如果用户选择特定的复选框,我需要一个文本框来显示来自用户的GET输入。

question : how do i get string input from the user with a treeview? 问题 :如何通过树视图从用户获取字符串输入?

A TreeView does not support including TextBoxes natively. TreeView本身不支持包含TextBoxes。 You could use a JavaScript solution where you create client side text boxes and serialize the value mapping to an <asp:HiddenField /> . 您可以使用JavaScript解决方案创建客户端文本框,并将值映射序列化为<asp:HiddenField />

Set up your TreeView and HiddenField: 设置TreeView和HiddenField:

<asp:TreeView runat="server"
    ID="MyTreeView"
    ShowCheckBoxes="All"
    NodeStyle-CssClass="node">
    ...
</asp:TreeView>
<asp:Hidden runat="server" ID="TreeViewTextValues" />

Some jQuery: 一些jQuery:

$(".node :checkbox").click(function (e) {
    var node = $(this).closest(".node");
    if (this.checked) {
        $("<input/>").addClass("nodeTextBox").appendTo(node);
    }
    else {
        node.find("input.nodeTextBox").remove();
    }
});
$("form").submit(function (e) {
    var nodeText = $("input.nodeTextBox").map(function () {
        return encodeURIComponent(this.value);
    });
    $("input[type=hidden][id$=TreeViewTextValues]").val(nodeText.join("&"));
});

Some C#: 一些C#:

var text = TreeViewTextValues.Value.Split('&').Select(s => Server.UrlDecode(s));
// text.ElementAt(n) maps to MyTreeView.CheckedNodes[n]

Untested, but hopefully a helpful start. 未经测试,但希望是一个有用的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM