简体   繁体   English

ASP.net网页更新问题(回发?)

[英]ASP.net web page update issue ( Postback?)

Greeting of the day everyone. 大家今天的问候。

Hoping you are all well. 希望你一切都好。 I need your help to solve a simple issue ( I an new to C# + ASP.net). 我需要您的帮助来解决一个简单的问题(我是C#+ ASP.net的新手)。

I have created a ASp.net portal. 我已经创建了一个ASp.net门户。 When a user open that portal (web page). 用户打开该门户网站(网页)时。 the Webpage give him a list of Groups whose he is the member of and the list of group whose he can add to himself.. 该网页为他提供了他所属的网上论坛列表以及可以添加到自己的网上论坛列表。

It also contain two Drop Downs. 它还包含两个下拉菜单。 One contains the list of groups, whose use can add to themselves. 一个包含组列表,其使用可以添加到自己的列表中。 The second one contains the list of groups , whose the user is member of, and he can remove it from those groups. 第二个列表包含用户所属的组列表,他可以将其从这些组中删除。

Adding and removing code is working fine. 添加和删​​除代码可以正常工作。 But I have to refresh the page manually to show the updated information. 但是我必须手动刷新页面以显示更新的信息。 After clicking the Add or Remove Button page is not refreshing with the updated information. 单击“添加或删除按钮”页面后,不会使用更新的信息刷新。

I tried to use redirected to main 我试图使用重定向到主 在此处输入图片说明 page using Response.Redirect("Success.aspx") 使用Response.Redirect("Success.aspx")

But nothing working, 但是没有任何作用

protected void Page_Load(object sender, EventArgs e)
{
    string uN = "domain\\michael.jackson";
    //SystemResources sr = new SystemResources();
    //ActiveUser usr = sr.GetUserDetails(uN).FirstOrDefault();

    LabelUserName.Text = "michael.jackson";

    // Getting the list of groups using of praticular OU the classOrgUnitGroup

    string sdomain = ClassGroupPortalSettings.getDomainName();
    string sInterstOU = "OU=testing,DC=analysys,DC=com";
    classOrgUnitGroup a = new classOrgUnitGroup();
    List<string> allGroups = a.GetGroupFromOu(sdomain, sInterstOU);
    string grouplist = "<ul>";
    foreach (string group in allGroups)
    {
        grouplist = grouplist + "<li><a href='showgroupmembers.aspx?group=" + group + "'>" + group + "</a></li>";

    }

    grouplist = grouplist + "</ul>";
    lblOpenGroups.Text = grouplist;

    //// 4. Finding users group membership.
    string sDomain = ClassGroupPortalSettings.getDomainName();
    classUserGroupMembership myMembership = new classUserGroupMembership();
    List<string> myGroupsMem = myMembership.getMyGroupMembership(sDomain, "domain\\michael.jackson");

    string glList = "<ul>";
    string openlList = "<ul>";
    string otherGroup = "<ul>";
    foreach (string grp in myGroupsMem)
    {
        //BulletedListMyGroups.Items.Add(grp);

        glList = glList + "<li>" + grp + "</li>";
        if (allGroups.Contains(grp))
        {
            DropDownListMyGroups.Items.Add(grp);
            openlList = openlList + "<li>" + grp + "</li>";
        }
        else 
        {
            otherGroup = otherGroup + "<li>" + grp + "</li>";
        }
    }

    glList = glList + "</ul>";
    openlList = openlList + "</ul>";
    otherGroup = otherGroup + "</ul>";
    LabelOtherGrp.Text = otherGroup;
    LabelOpenGrp.Text = openlList;

    // LabelMyGroup.Text = glList;

    foreach (string emailGroup in allGroups)
    {
        if (!myGroupsMem.Contains(emailGroup))
        {
            DropDownListOpenGroups.Items.Add(emailGroup);
        }
    }

}

This is code which run when a page reload. 这是重新加载页面时运行的代码。

The issue is : When I click on Remove Button or Add Button, it run the code to add or remove user from selected group. 问题是:当我单击“删除按钮”或“添加按钮”时,它将运行代码以从所选组中添加或删除用户。 when page reload after clicking on button. 单击按钮后重新加载页面时。 The Group membership labels. 组成员资格标签。 and Dropdown box is not updating with code :( 和下拉框不更新代码:(

I'm assuming your scenario like this, you're having two Drop down. 我假设您的情况是这样,您有两个下拉菜单。 One is full of records and the items can be select. 一个充满记录,可以选择项目。 Once items are selected, user will submit the button then records will save into database/xml. 一旦选择了项目,用户将提交按钮,然后记录将保存到database / xml中。 Later you're retrieving the items from db/xml and showing that in second Drop down. 稍后,您将从db / xml中检索项目,并在第二个下拉列表中进行显示。

Now you're issue is that the records are not showing in second Drop down after clicking the submit button, 现在的问题是,在单击“提交”按钮后,第二个下拉列表中没有显示记录,

If it so, you have two options. 如果是这样,您有两个选择。

1) You can pull your records as soon as insert things made into db. 1)只要在db中插入内容,就可以提取记录。 2) You can pull your records in Page load itself as I mentioned below, 2)您可以按照下面提到的方式在Page load本身中提取记录,

If(Postback) // Or If(PostBack == true)
{
  // Pull your records and display in Drop down.
}

It is just the order of events which causing confusion IMO. 只是导致IMO混乱的事件顺序。 Page_Load is called first and then control events(click, change etc). 首先调用Page_Load,然后控制事件(单击,更改等)。 So on add/delete you need to rebind the items. 因此,在添加/删除时,您需要重新绑定项目。

https://msdn.microsoft.com/en-gb/library/ms178472%28v=vs.100%29.aspx https://msdn.microsoft.com/zh-CN/library/ms178472%28v=vs.100%29.aspx

Move out the data bind logic to a function and call function at the end of click event as well. 在单击事件结束时,也将数据绑定逻辑移至函数并调用函数。 Then you can reuse the function in page load and put logic to bind in page load only of its not postback because you might be rebinding unnecessarily in page load. 然后,您可以在页面加载中重用该函数,并仅在不进行回发的情况下将逻辑绑定到页面加载中,因为您可能在页面加载中不必要地重新绑定。

protected void Page_Load(object sender, EventArgs args)
    {
      if(!IsPostBack)
    {
       DataBindLogic();
    }
    }

    private void DataBindLogic()
    {
      /*Put databind code here */
    }

    protected void RemoveBtn_Click(Object sender,Eventargs args)
    {
      /*Do db update */
      DataBindLogic();
    }

Thanks a lot everyone and and each of you for your time and help and energy, thanks a ton. 非常感谢每个人以及你们每个人的时间,帮助和精力,非常感谢。

The issue is resolved, I have added another lines of code in the Postback, which checks the action of the User. 问题已解决,我在回发中添加了另一行代码,用于检查用户的操作。

For example, if used Remove from the Group, 例如,如果使用“从组中删除”,

  • Then get the name of the Group and use action REMOVE 然后获取组的名称并使用“删除”操作
  • Then Update the DropDown box and Current membership box. 然后更新下拉列表框和当前成员资格框。

I did the same for the add group too, and it resolved the issue for me. 我对添加组也做了同样的事情,它为我解决了这个问题。

Thanks again , and whishing you a great day ahead. 再次感谢您,并祝您明天愉快。

Regards and Best wishes Aman 问候和祝福阿曼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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