简体   繁体   中英

Returning value from code behind to aspx page

So, i want to set my cssclass with a codebehind function that looks like this, but it doesnt work, I only get an ampty string in my final code, what am I doing wrong?

Aspx:

<td align="left" class="<% GetCssForUser("start") %>">

CodeBehind:

        protected string GetCssForUser(string field)
    {
        string css = "";

        switch(field)
        {
            case "start":
                css = "start";
                break;
            case "end":
                css = "end";
                break;
            case "course":
                css = "course";
                break;
            case "group":
                css = "group";
                break;
            case "teacher":
                css = "teacher";
                break;
            case "room":
                css = "room";
                break;
            case "plan":
                css = "plan";
                break;
        }

        if (User.ActiveRole == Teacher)
        {
            css += "Teacher";
        }
        else 
        {
            css += "Student";
        }

        return css;
    }

i think you messing syntax in this code

<td align="left" class="<% GetCssForUser("start") %>">

it's use like this

<td align="left" class="<%=GetCssForUser("start") %>">

i think this will help you

Try:

class='<%=GetCssForUser("start") %>'

Note the %= and the opening '< and closing >' (because of the later "start" )

尝试以下操作: <td align="left" class="<%= GetCssForUser("start") %>">

您可以尝试使用此代码

youControl.Attributes.Add("class", css );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM