简体   繁体   English

ASP.NET PostBack选择treeview的复选框

[英]ASP.NET PostBack on selecting checkbox of treeview

I have an asp.net project and working in C#. 我有一个asp.net项目并在C#中工作。

In my project I have a databound listbox that has checkboxes. 在我的项目中,我有一个带有复选框的数据绑定列表框。

When the user clicks on a checkbox it should for an example update a label/textbox. 当用户点击复选框时,它应该为示例更新标签/文本框。

The thing is, it doesnt update the label/textbox until I click on a button that does a postback. 问题是,在我点击执行回发的按钮之前,它不会更新标签/文本框。 How will I Call a postback on the checkbox changed event, since the "OnTreeNodeCheckChanged" event looks like it only fires once the postback has been triggered? 如何在复选框更改事件上调用回发,因为“OnTreeNodeCheckChanged”事件看起来只有在触发回发后才触发? Is this even a good idea (to want to call a postback every time the a checkbox has been changed) 这是一个好主意(每次复选框都要更改时要调用回发)

--Updated code Snippet-- Asp - 更新的代码片段 - Asp

 <asp:TreeView ID="treevCourses" runat="server" AutoPostBack="true" ShowCheckBoxes="All" Width="100%"
                OnTreeNodeCheckChanged="check_changed" Height="16px" ImageSet="Contacts">

(Tried having the handler in the C# part.) C# (尝试在C#部分中使用处理程序。)C#

protected void check_changed(object sender, TreeNodeEventArgs e)
        {
        lblTest.Text = "TestText";
        }

(Also tried having it in the script part) (也尝试在脚本部分使用它)

void check_changed(object sender, EventArgs e)
    {
        lblTest.Text = "TestText";
    }

Binding data to the Treeview (this happens on a button postback) 将数据绑定到Treeview(这发生在按钮回发上)

foreach (DataRow row in ds.Tables[0].Rows)
                {
                    TreeNode node = new TreeNode(row["courseName"].ToString(), row["courseName"].ToString());
                    //  node.PopulateOnDemand = true;
                    treevCourses.Nodes.Add(node);
                }


                //select from topic where parentId = topicId.
                ds = myConClass.returnSqlDataset("select cd.courseName,ct.[date] from courseDetails cd join courseTimes ct on cd.courseId = ct.courseId");

                foreach (TreeNode treenode in treevCourses.Nodes)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        if (row["courseName"].ToString() == treenode.Value)
                        {
                            TreeNode node = new TreeNode(row["date"].ToString(), row["date"].ToString());
                            treenode.ChildNodes.Add(node);
                        }
                    }       
                }

There is no AutoPostBack property on TreeView. TreeView上没有AutoPostBack属性。 And as per the MSDN , The TreeNodeCheckChanged event is raised when a check box in the TreeView control changes state between posts to the server 并且根据MSDN ,当TreeView控件中的复选框在发布到服务器的帖子之间改变状态时,会引发TreeNodeCheckChanged事件

You need to do something else, like mentioned on this link 您需要做其他事情, 如此链接中提到的那样

1) Add on click attribute to TreeView1 on page load 1)在页面加载时将Click属性添加到TreeView1

protected void Page_Load(object sender, EventArgs e)
{
     TreeView1.Attributes.Add("onclick", "postBackByObject()");
}

2) add java script function and do the post back 2)添加java脚本函数并回发帖子

    <script type="text/javascript">

     function postBackByObject()
     {
         var o = window.event.srcElement;
         if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        } 
    }
   </script>

3). 3)。 Implement TreeNodeCheckChanged event 实现TreeNodeCheckChanged事件

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            // do stuff
        } 

When you're dynamically binding the TreeView the TreeNodeCheckChanged event will not be fired when you click the checkbox, you can overcome this quite easily though with a little bit of javascript: 当你动态绑定TreeView时,单击复选框时不会触发TreeNodeCheckChanged事件,你可以通过一点点javascript轻松克服这个问题:

ASPX: ASPX:

<head runat="server">
    <script type="text/javascript">
        function fireCheckChanged() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="treevCourses" runat="server" 
            ShowCheckBoxes="Parent,Leaf" Width="100%" Height="16px" ImageSet="Contacts" 
            ontreenodecheckchanged="check_changed" />
    </div>
    </form>
</body> 

Code behind: 代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var data = new XmlDataSource();
        data.DataFile = Server.MapPath("~/input.xml");
        treevCourses.DataSource = data;
        treevCourses.DataBind();

        treevCourses.Attributes.Add("onclick", "fireCheckChanged()");
    }
}

protected void check_changed(object sender, TreeNodeEventArgs e)
{
    string clickedNode = e.Node.Text;
    System.Diagnostics.Debugger.Break();
}

Is this a good idea - Obviously sending requests to the server everytime the checkbox state is changed can become resource intensive but if you cannot replicate the same functionality using javascript then this is your only option 这是一个好主意 - 显然,每次更改复选框状态时向服务器发送请求都会变得占用大量资源,但如果您无法使用javascript复制相同的功能,那么这是您唯一的选择

Replace this line 替换此行

treevCourses.Attributes.Add("onclick", "fireCheckChanged()");

with

treevCourses.Attributes.Add("onclick", "fireCheckChanged(event)");

and replace the script with 并用。替换脚本

  function fireCheckChanged(e) {
 var evnt = ((window.event) ? (event) : (e));
 var element = evnt.srcElement || evnt.target;

 if (element.tagName == "INPUT" && element.type == "checkbox") {
      __doPostBack("", "");
}}

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

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