Click here to Skip to main content
15,888,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I am trying this since last four days but not getting it to work. I have a tree view control in my page with which I am creating html button with each tree node in code behind.

Like this:

C#
foreach (DataRow row in ds.Tables[0].Rows)
            {

                {
                    TreeNode childTreeNode = new TreeNode();
                    string date_created = row["date"].ToString();

                    string date = DateTime.Parse(date, CultureInfo.InvariantCulture).ToString("MM/dd/yyyy");
                    StringBuilder text = new StringBuilder();

                    text.Append(row["col_name"].ToString() + "  " + row["col_name"].ToString() + "." + row["col_name"].ToString() + "." + row["col_name"].ToString() + " <span style='color: black;fontstyle:italic;'>" + " (Created Date-" + date + ") " + "</span>");

                    text.Append(@"<button onclick=""javascript:clickedMe""><img src=""/Images/getInfo.png"" width=""30"" height=""15"" ></button>");

                    childTreeNode.Text = text.ToString();



So this part is working just fine. Right now what I am doing is that in my aspx page I have a javascript method as follows:
JavaScript
<script type ="text/javascript" language="javascript">
function clickedMe() {

    <%
      if(IsPostBack)
       {
       getResult();
       }
   %>
}
      <;/script>


and in my code behind I have this getResult() method as follows:
C#
public void btnGetData()
{
    try
    {
        {

            Session["name"] = TreeView1.SelectedNode.Text;
            if (IsPostBack)
            {
                Session["name"] = TreeView1.SelectedNode.Text;
                TreeView1.SelectedNode.Selected = false;
                ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'GetResult.aspx', null, 'height=400,width=800,status=yes,toolbar=no,minimizable=yes,maximizable=no, resizable= yes,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);

            }

        }
    }
    catch (Exception ex)
    {
        Exception E = ex;
    }
}


I also have treeview node change event.

C#
public void Select_Change(Object sender, EventArgs e)


So Clicking on button and node should open different windows.

The problem: When I click to button and after clicking on button if I click on the node associated with the same button I clicked, it opens the same window(i.e. opened by button), it should open different window. But if I click on another node it works fine and after clicking on another node if I click on that node again it will work fine.

I am really confused with this. I have tried almost all ways to solve this problem from the internet.

Is there is a way that I can attach my button to Click event Handler. I have tried it but it always gives me error while running that method is undefined.

Please help me.
Posted
Comments
F-ES Sitecore 16-Jul-15 11:34am    
A lot of your confusion is coming from the fact that you don't understand the client\server model of how asp.net works. This code here

function clickedMe() {
<%
if(IsPostBack)
{
getResult();
}
%>

javascript runs on the client in the browser. View the page source and what do you see?

function clickedMe() {

}

So clicking your button calls a function that does nothing, this means all you're doing is submitting the form as normal and getting a normal postback and maybe that is ending up with your btnGetData function getting called somehow? You also have basic syntax\attention to detail issues...you say the server function is called getResult, but your method is called btnGetData.

You can't call server-code from javascript as server code runs first and the results are given to the browser. asp.net isn't "running inside" the browser. Also if you *do* want to run javascript on a button click you need to ensure the js function returns false if you don't want the form to be submitted, and make sure the onclick handler returns false too.
Member 11842305 16-Jul-15 12:08pm    
Thank you for replying Sir:) Firstly, I am sorry about that syntax thing, mistakenly I wrote btnGetData in the post.

So how can I assign the Click event to my html button in code behind? I tried but it always says me method is undefined?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900