1

I have a problem with the event listener in c#. What i would to do is to store in

 Session["ClickModule"]

a variable when i click a button.

This Variable allows me to keep "Selected" my button (A graphic selection with a javascript function).

I need this solution because when the user click the button the page is reloaded, and i need to load the correct settings, like the correct button layout.

So when the user click the button, before the postback i have to store the value in Session["ClickModule"] to retrieve it in the page_load function.

Page_Load code:

  if (!IsPostBack)//only for the first load
                    {
                    HtmlGenericControl li = new HtmlGenericControl("li");
                    li.Attributes["class"] = "dropdown";
                    li.ID = "Module" + row["ModuleID"];

                    HtmlAnchor a = new HtmlAnchor();

                        a.HRef = "javascript:showhide('" + row["ModuleID"] + "')";

                        a.InnerText = row["modtitle"].ToString();

                        Panel div = new Panel();
                        div.ID = "subModulesBar" + row["ModuleID"];
                        div.Attributes["class"] = "subModulesBar";


                        li.Controls.Add(a);
                        ModuleContainer.Controls.Add(li);
                        divcontainer.Controls.Add(div);
                        module = li;
                    }
     else// for the postback loads
     {
                        HtmlGenericControl li = new HtmlGenericControl("li");
                        li.ID = "Module" + row["ModuleID"];

                        Debug.WriteLine("SESSION MODULE = " + Session["PModuleID"].ToString() + " CURRENT MODULE = " + row["ModuleID"].ToString());
                        if (Session["PModuleID"].ToString() == row["ModuleID"].ToString())
                        {
                            Debug.WriteLine("UGUALE SELECTED");
                            li.Attributes["class"] = "dropdown moduleselected";
                        }
                        else
                        {
                            Debug.WriteLine("DIVERSO");
                            li.Attributes["class"] = "dropdown";
                        }





                        HtmlAnchor a = new HtmlAnchor();
                        a.HRef = "javascript:showhide('" + row["ModuleID"] + "')";

                        a.InnerText = row["modtitle"].ToString();

                        Panel div = new Panel();
                        div.ID = "subModulesBar" + row["ModuleID"];
                        div.Attributes["class"] = "subModulesBar";

                        li.Controls.Add(a);
                        ModuleContainer.Controls.Add(li);
                        divcontainer.Controls.Add(div);
                        module = li;

                    }

This is my JS function:

 function showhide(id) {

        var fulldrops = document.getElementsByClassName('subModulesBar');
        var lis = document.getElementsByClassName('dropdown');


        for (var i = 0; i < fulldrops.length; i++) {
            if (fulldrops[i].id == "ContentPlaceHolder1_subModulesBar" + id) {
                if (fulldrops[i].style.visibility == "visible") {
                    fulldrops[i].style.visibility = "hidden";
                } else {
                    fulldrops[i].style.visibility = "visible";
                }
            } else {
                fulldrops[i].style.visibility = "hidden";
            }
        }
        for (var i = 0; i < lis.length; i++) {
            if (lis[i].id == "ContentPlaceHolder1_Module" + id) {
                if (lis[i].className == "dropdown moduleselected") {
                    lis[i].className = "dropdown";
                    lis[i].style.backgroundImage = "url('images/arrowdown.png')";
                } else {
                    lis[i].className = "dropdown moduleselected";
                    lis[i].style.backgroundImage = "url('images/arrowup.png')";
                }
            } else {
                lis[i].className = "dropdown";
                lis[i].style.backgroundImage = "url('images/arrowdown.png')";
            }
        }
    }

it simple modify the layout (white background and arrow down when the button is clicked ,gray and arrowup when isn't).

I read a lot of post about this argument. But all of it suggest to call the function inside the pageload. But i can't follow this way because i need to store my value only if the user click the button.

Can someone help me?

Luke
  • 517
  • 10
  • 29
  • 2
    The question is not clear. Why do you need to store it to the session before `Page_Load`? Why can't you store it in the button-click event handler? Also, the wording _"before the postback to set the correct layout at my button"_ is quite confusing. Does _before postback_ mean that you want to store it at clientside? – Tim Schmelter Sep 25 '15 at 12:55
  • i modified my question. Is it clear now? – Luke Sep 25 '15 at 13:02
  • 1
    No, you've still the words _"before the postback i have to store the value"_. Why do you need to retrieve it in `Page_Load`, why don't you store it already on the initial load? Why can't you set it in the button-click event handler, so after `Page_Load` but only if the user clicked the button? – Tim Schmelter Sep 25 '15 at 13:04
  • You mean when user clicks on the button before a pageload occur you want to save your button layout in a session so that after pageload you can set that layout again. As this layout isn't static it's generated dynamically in some part of your application thus you want to preserve that ? Is this what you meant ? – Suprabhat Biswal Sep 25 '15 at 13:06
  • yes.. I have difficult to explain it in english, but it's excatcly what i would to do.. – Luke Sep 25 '15 at 13:08
  • Sounds like you need a custom control that is view state enabled. Did you do searches on "view state" and "state bag"? – Hogan Sep 25 '15 at 13:10
  • If you are using javascript you could just use cookies and ignore the .net platform. – Hogan Sep 25 '15 at 13:11
  • you need to check out the [page lifecycle](http://stackoverflow.com/a/21776865/542251) this tells you what events fire and in what order – Liam Sep 25 '15 at 13:16
  • I haven't many experience with JS. I added my part of code in my question. How i can manage it with the cookies? – Luke Sep 25 '15 at 13:17

0 Answers0