简体   繁体   中英

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).

I have created a ASp.net portal. 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")

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. Later you're retrieving the items from db/xml and showing that in second Drop down.

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. 2) You can pull your records in Page load itself as I mentioned below,

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. Page_Load is called first and then control events(click, change etc). So on add/delete you need to rebind the items.

https://msdn.microsoft.com/en-gb/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

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