ASP.NET PostBack on selecting checkbox of treeview

强颜欢笑 提交于 2019-12-03 21:45:35

问题


I have an asp.net project and working in 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? 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: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#

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)

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);
                        }
                    }       
                }

回答1:


There is no AutoPostBack property on TreeView. 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

You need to do something else, like mentioned on this link

1) Add on click attribute to TreeView1 on page load

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

2) add java script function and do the post back

    <script type="text/javascript">

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

3). Implement TreeNodeCheckChanged event

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



回答2:


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:

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




回答3:


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("", "");
}}


来源:https://stackoverflow.com/questions/15945027/asp-net-postback-on-selecting-checkbox-of-treeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!