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?