Changing style dynamically, using C#/ASP.NET
sharkydart
KY Icrontian
This is driving me crazy, since it seems like it should be a subtle, simple thing to do:
I have a style(css) class "questionSetLeft" and a class "questionSetRight", which both do what they are supposed to if I simply hard-code them into the aspx. I have been trying for hours to figure out how to simply change style on the fly.
I have a repeater, where the div "dvTemplate" surrounds the entirety of what this particular repeater is repeating, and looks (at the top) like so:
In the code-behind(.cs file), on the event where an item is bound to the repeater "rptQuestions", I make my attempt to change the style:
Basically, every way I have tried to use a local variable to reference the div and every way I have tried to modify any property of it have failed, and I am now completely sick and tired of dealing with this problem and not finding anything helpful on the internet.
I have a style(css) class "questionSetLeft" and a class "questionSetRight", which both do what they are supposed to if I simply hard-code them into the aspx. I have been trying for hours to figure out how to simply change style on the fly.
I have a repeater, where the div "dvTemplate" surrounds the entirety of what this particular repeater is repeating, and looks (at the top) like so:
< asp:Repeater ID="rptQuestions" runat="server" > < ItemTemplate > < div id="dvLeftTemplate" class="questionSetLeft" runat="server" >
In the code-behind(.cs file), on the event where an item is bound to the repeater "rptQuestions", I make my attempt to change the style:
private void rptQuestions_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) { HtmlContainerControl leftTemplate = (HtmlContainerControl)e.Item.FindControl("dvLeftTemplate"); if(m_ItemNumber % 2 != 0) leftTemplate.Visible = true;However, when I try to access the div tag to change the class it uses ...or even to try:
leftTemplate.Attributes.Add("style", "float: left; clear:right");it gives me the "Object reference not set to an instance of an object." error. I can think of two less-than-desirable changes I could make to my design that would solve this issue, but I just want to know if there exists a simple way to access a dynamic control to change the style.
Basically, every way I have tried to use a local variable to reference the div and every way I have tried to modify any property of it have failed, and I am now completely sick and tired of dealing with this problem and not finding anything helpful on the internet.
0
Comments
...one of those stupid things...
make sure, if you use an alternating item template, and you try and reference a control on every databind...... that the id is the same in both places >_<
hours and hours wasted on that one.