In this thread I will ask noob questions about asp.net

midrange

Senior Member
Joined
Apr 8, 2009
Messages
727
Reaction score
0
Firstly:

I've copied some folders and files from the CSS Friendly Control Adapters into my solution and now have the menu in my master page rendering as an unordered list rather than in tables. Cool potatoes. Now, how do I get a class added to the li or a tag on the current page so that I can highlight/style it?

Is there another solution?
 
Is there a .ClassName property on the control in question? That would be a good start if there is. (Haven't used the CCSFCA in ages)

EDIT: Stoopid. I missed the part about the li element, and not the whole control...

Turns out I wasn't far off. Found this:
I'm changing the class dynamically at the li level, but the same logic can be used for links. You need to specify an id and runat attribute, for example...
Code:
<li runat="server" id="liExample"><asp:HyperLink ID="lnkExample" runat="server" NavigateUrl="example.aspx">example</asp:HyperLink></li>
And then just check the Path of the document for either "foo" or "bar"
Code:
1    string strPath = Page.AppRelativeVirtualPath;
2    if (strPath.Contains("foo"))
3    {
4        //Set the list item class
5        liExample.Attributes.Add("class", "current");
6    
7        //Set the Hyperlink Class
8        lnkExample.CssClass = "current";
9    }

The meat of it is in line 5. I think this is what you were looking for.
 
Last edited:
Are there any CSS class names being added to the elements at all? Isn't there some higher-level property on the control itself that you can use to set the CSS class to use? Usually it is CssClass or something in that vein, if the controls follow Microsoft's standards...

There are classes being added, but generic ones, nothing for the current page.

Thanks raithlin, I'll look at that, just stuck for time right now, but I need this working by close of play on Sunday. I'm finding asp.net web forms not very "RAD" at all.
 
Have you considered a <asp:Menu> control. That gives you a whole bunch of style options.

We've used a variations with repeater controls and jquery lately, we found it a bit more elegant in our situation with rendering nav controls.

maybe some code so we can see what you are doing?
 
@Raithlin, that's the solution I've used, thanks. Hard-coded the menu in the master page then run some if statements setting the class.

@Nefertiti, the <asp: Menu> control is a piece of crap. It was why I was trying to use the CSFAC. (Caveat: my .NET knowledge is nowhere.)
 
@Raithlin, that's the solution I've used, thanks. Hard-coded the menu in the master page then run some if statements setting the class.

@Nefertiti, the <asp: Menu> control is a piece of crap. It was why I was trying to use the CSFAC. (Caveat: my .NET knowledge is nowhere.)

Haha! That would have been 15 points for me on StackOverflow. ;)

Glad you came right in the end, dude. Keep on learning - Web Forms can be RAD if you only need what is provided. Getting into the code, not so much - but it is still very powerful. Wait until you hit MVC (big projects, mind you. Not worth the effort for smaller ones)...
 
Haha! That would have been 15 points for me on StackOverflow. ;)

Glad you came right in the end, dude. Keep on learning - Web Forms can be RAD if you only need what is provided. Getting into the code, not so much - but it is still very powerful. Wait until you hit MVC (big projects, mind you. Not worth the effort for smaller ones)...

I present you with 150 myBB points :)

Next noob question. I need to write a latest news section. I have a simple table (id, title, newcontent, etc) and have created a linq to sql ... um, thing.

In the admin section I want to read out a list of the titles and have them link to another page that displays their details where they can be edited/deleted etc.

I can do this:

stmDBDataContext db = new stmDBDataContext();

var newstitles = from t in db.News select t;


foreach (var x in newstitles) {
Response.Write(x.title);
}

But I can't do this, for example:

GridView1.DataSource = newstitles;
GridView1.DataBind();

I get this in return:

Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.

****.
 
Top
Sign up to the MyBroadband newsletter
X