c# - 从类中获取文本值的相同元素的不同结果

标签 c# asp.net webforms

在我的站点中,我定义了一个 Dictionary,它根据 session 返回 PersianEnglish 字符串。这是我的字典的代码:

public static string Find_Term(string term)
{
    var dictionary = new Dictionary<string, string>();
    dictionary.Add("Accommodation Barges", "بارج اقامتی");
    dictionary.Add("Bulk Carriers", "فله بر");
    dictionary.Add("Barge", "بارج");
    dictionary.Add("Cable Layers", "کابل گذار");
    dictionary.Add("Cargo Ships", "باربری");
    dictionary.Add("Container Ships", "کانتینر بر");
    dictionary.Add("Crew Boats", "پرسنل بر");
    dictionary.Add("Cruise Ships", "کروز");
    dictionary.Add("Dive Boats", "قایق قواصی");
    dictionary.Add("Drilling Rigs", "سکوی حفاری");
    dictionary.Add("Fishing Boat", "ماهیگیری");
    dictionary.Add("Ferries", "فری");
    dictionary.Add("Floating Cranes", "کرن شناور");
    dictionary.Add("Floating Hotels", "هتل شناور");
    dictionary.Add("Glass Bottom Boats", "کف شیشه ای");
    dictionary.Add("Hovercraft", "هاورکرافت");
    dictionary.Add("Hydrofoils", "Hydrofoils");
    dictionary.Add("Ice Breakers", "یخ شکن");
    dictionary.Add("Life Boats", "لایف بوت");
    dictionary.Add("Landing Craft", "لندینگ کرافت");
    dictionary.Add("Life Rafts", "لایف رافت");
    dictionary.Add("Passenger Boats", "مسافر بر");
    dictionary.Add("Patrol Boats", "گشت");
    dictionary.Add("Pilot Boats", "پایلوت بوت");
    dictionary.Add("Platforms", "پلتفرم");
    dictionary.Add("Research Vessels", "تحقیقاتی");
    dictionary.Add("Salvage Ships", "از رده خارج");
    dictionary.Add("Supply Boats", "ساپلای بوت");
    dictionary.Add("Support Vessels", "ساپورت بوت");
    dictionary.Add("Tankers", "تانکر");
    dictionary.Add("Tour Boats", "تور");
    dictionary.Add("Towboats", "هدایت کننده");
    dictionary.Add("Tugs", "یدک کش");
    dictionary.Add("Utility Boats", "یوتیلیتی بوت");
    dictionary.Add("All Types", "همه نوع");
    dictionary.Add("LogIn", "ورود");
    dictionary.Add("Register", "ثبت نام");
    dictionary.Add("LogOut", "خروج");
    dictionary.Add("Home", "صفحه اصلی");
    dictionary.Add("News", "تازه ها");
    //dictionary.Add("Builder", "سازنده");
    dictionary.Add("Builder", "سازنده");
    dictionary.Add("Request this vessel", "درخواست این شناور");
    dictionary.Add("Ship Particular", "مشخصات شناور");
    dictionary.Add("Build Year", "سال ساخت");

    if (HttpContext.Current.Session["Lang"].ToString() == "fa")
    {
        return dictionary[term];
    }
    else
    {
        return term;

    }
    //
    // TODO: Add constructor logic here
    //
}

返回的字符串用于网站关键元素的文本属性。例如,它在中继器中的按钮上工作正常:

<asp:Repeater runat="server" ID="vslCat" DataSourceID="SqlDataSource2">
                <ItemTemplate>
                    <asp:LinkButton runat="server" Text='<%# Dict.Find_Term(Eval("vCat").ToString()) %>' PostBackUrl='<%# Request.RawUrl + "?vslCat=" + Eval("vCat") %>' CssClass="ui-btn ui-mini ui-shadow ui-btn-icon-right ui-icon-carat-r"></asp:LinkButton>
                </ItemTemplate>
            </asp:Repeater>

但是在中继器之外的同一个元素中,它没有错误地返回任何内容!!!

<asp:LinkButton runat="server" Text='<%# Dict.Find_Term("All Types")%>' CssClass="ui-btn ui-mini ui-shadow ui-btn-icon-right ui-icon-carat-r" PostBackUrl="~/Default2.aspx" ID="btnCatAllen"></asp:LinkButton>
            <asp:Repeater runat="server" ID="vslCat" DataSourceID="SqlDataSource2">
                <ItemTemplate>
                    <asp:LinkButton runat="server" Text='<%# Dict.Find_Term(Eval("vCat").ToString()) %>' PostBackUrl='<%# Request.RawUrl + "?vslCat=" + Eval("vCat") %>' CssClass="ui-btn ui-mini ui-shadow ui-btn-icon-right ui-icon-carat-r"></asp:LinkButton>
                </ItemTemplate>
            </asp:Repeater> 

不知道是什么问题!!!如果您发现任何线索,请告诉我。

最佳答案

当我们使用像<% %> 这样的代码块时在 Elements 属性中,如 <asp:Button runat="server" text='<%# ...Codes... %>'></asp:Button>在使用数据源的任何元素之外,如 ListView 或转发器,我们应该使用元素 id 并在代码后面给它一个 go orrrrr 我们应该这样做:

<asp:HyperLink runat="server" NavigationUrl="#"><%= ...Codes... %></asp:HyperLink> 

请注意,代码块的开头已从 # 更改为至 = !!!

关于c# - 从类中获取文本值的相同元素的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815995/

相关文章:

c# - 切换到 IIS8 后断开连接时 ASP.NET 处理请求

c# - 如何使用 HTMLElement 以编程方式单击链接元素?

c# - 如何使用自定义 XmlResolver 解析 DTD *内部* 的公共(public)标识符?

c# - 如何避免 "Nested transactions are not supported."错误?

c# - 如何在 ASP.NET 中找出当前登录用户的名称?

javascript - 在 OnChange JavaScript 中访问 Repeater 项控件

javascript - 带有 JQuery 验证的表单生成器

c# - 如何转换项目并在 sitecore 中获取选定的项目?

c# - 依赖属性的代码片段在哪里

asp.net - 执行SQL代码优化数据库时Hangfire导致Timeout过期异常 - ASP.NET Core