网页标题更改

时间:2017-02-14 10:20:48

标签: c# jquery checkbox

我正在对我们的网站进行一些更改,并且刚刚添加了一个效果很好的复选框列表,但是当我选择多个复选框时,我需要更改页面的标题。 目前,如果没有选择任何内容,则会显示“摘要......”。然后,当您选中一个复选框时,它会显示“摘要......(复选框的名称)” 当选择了多个复选框时,我需要它说“摘要......(多)” 我在aspx.cs中找到了这段代码:

public partial class _default : PortalReportPage
{
    public int RegionId { get; set; }
    public List<int> SelectedRegions { get; set; }
    public GroupStatisticCollection Stats { get; set; }
    public CompanyRegionCollection Regions { get; set; }
    public int PropertyCount { get; set; }

    protected override void BindPath()
    {
        PagePath = PortalPath.GetBasicPath();
        PagePath.AddNode(Container.Name, "~/company/default.aspx?id=" + Container.NodeId);
        PagePath.AddNode("Reports", "~/company/reports.aspx?id=" + Container.NodeId);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get all Selected regions into the SelectedRegions variable...
        SelectedRegions = GetArrayFromQueryString<Int32>("region").ToList();

        // TODO: remove this when the sole RegionId is ready to be replaced by the SelectedRegions list
        if (SelectedRegions.Count > 0)
        {
            RegionId = SelectedRegions.First();
        }
        else
        {
            RegionId = 0;
        }

        GetStatistics();

        if (!Page.IsPostBack)
        {
            this.Regions = CompanyRegionCollection.GetRegions(Container.NodeId);

            if (this.Regions != null && this.Regions.Count > 0)
            {
                if (this.RegionId == 0)
                {
                    this.PropertyCount = Regions.Sum(r => r.PropertyCount);
                }
                else
                {
                    if (this.Regions.Any(r => r.NodeId == RegionId))
                    {
                        this.PropertyCount = Regions.First(r => r.NodeId == RegionId).PropertyCount;
                    }
                }
            }

            if (this.PropertyCount == 0)
            {
                if (this.Regions.Count == 0)
                {
                    this.PropertyCount = PropertyList.GetProperties(PropertyCriteria.NewCriteria(Container.NodeId, PropertyCriteriaSubject.Company)).TotalRecords;
                }
            }

            BindOptionsPanel();
        }

        this.Title = "Summary for " + Container.Name;

        if (RegionId != 0)
        {
            try
            {
                this.Title += string.Format(" ({0})", NodeBasic.GetNodeBasic(RegionId).Name);
            }
            catch
            {

            }
        }
    }

    private void GetStatistics()
    {
        Stats = GroupStatisticCollection.GetCategoryStatistics(Container.NodeId, SelectedRegions);
    }

    private void BindOptionsPanel()
    {
        RegionsChecklist.Items.Add(new ListItem(" All", "0"));

        foreach (var region in Regions.OrderBy(r => r.Name))
        {
            var item = new ListItem(region.Name, region.NodeId.ToString());

            if (SelectedRegions.Contains(region.NodeId))
            {
                item.Selected = true;
            }

            RegionsChecklist.Items.Add(item);
        }
    }

    protected void ExportButton_Click(object sender, EventArgs e)
    {
        ExportToCsv(this.Stats, this.Title);
    }

    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        var url = "~/reports/compliance/default.aspx?id=" + Container.NodeId;

        string selectedRegionString = "";

        foreach (ListItem item in RegionsChecklist.Items)
        {
            if (item.Selected)
            {
                selectedRegionString = selectedRegionString + item.Value + ",";
            }
        }

        url += "&region=" + selectedRegionString.TrimEnd(','); 
        Response.Redirect(ResolveUrl(url));
    }

但我不确定我需要改变什么,如果我在正确的地方寻找? 有人可以提供任何建议吗?

2 个答案:

答案 0 :(得分:1)

在客户端处理此问题是个好主意。

尝试以下

<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){
        if($(this).prop("checked") == true){
            $(document).attr("title", $(this).next().text());
        }
        else if($(this).prop("checked") == false){
        }
    });
});

这会将您网页的标题更改为所选复选框的“文字”属性。

确保您没有设置AutoPostBack =“True”。

更新 - 对于服务器端

要在服务器端处理它,请设置AutoPostBack =“true”

然后在代码后面试试

 string sTitle = "";

            if (CheckBox1.Checked)
            {
                sTitle += CheckBox1.Text + " ";
            }
            if (CheckBox2.Checked)
            {
                sTitle += CheckBox2.Text + " ";
            }

            this.Title = sTitle;

答案 1 :(得分:0)

我使用了上面答案中提供给我的内容,并设法将下面的代码片段放在一起,以便进行处理。

this.Title =&#34;摘要&#34; + Container.Name;

        if (SelectedRegions.Count > 1)
        {
            RegionId = SelectedRegions.First();
            this.Title = "Summary for " + Container.Name + " (Multiple Regions)";
        }
        else if (SelectedRegions.Count > 0)
        {
            RegionId = SelectedRegions.First();
            this.Title += string.Format(" ({0})", NodeBasic.GetNodeBasic(RegionId).Name);
        }
        else
        {
            RegionId = 0;
            this.Title = "Summary for " + Container.Name;
        }