简体   繁体   中英

cannot retain the control state after postback in asp.net

I am working on asp.net 3.5.I have a requirement like dynamically generated a list of html input controls of type='radio'.I write a onclick() event to all controls.So if i click on any radio control,it will go to javascript function and then it will submit the form.Based on the value of the clicked radio input,related data is loaded to another control.

The all functionalities are working fine. But now my problem is after page is submitted(after postback) the clicked radio input is not checked. So how to retain the state of clicked radio button.

Actually i store the clicked radio button to the hidden field in javascript function.Then i tried to retrieve value from that hidden field and converted to radio button to set the property checked=true.But its not working.

So please help me.

protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IQueryable<tblOrganization> orglist = from o in db.tblOrganizations
                                              where o.CreatedBy == "Licensee" && o.RefID == user.UserID
                                              select o;
        if (orglist != null && orglist.Count() > 0)
        {
            string list = "<table border='0' cellpadding='0' cellspacing='0' width='350px'>";
            foreach (tblOrganization o in orglist)
            {
                list += "<tr>";
                list += "<td style='width:25px;' align='right' valign='top'><input type='radio' runat='server' name='org' id='" + o.OrgID + "' ViewStateEnabled='true' onclick='javascript:fnBindGroup(" + o.OrgID + ",this)'  /></td>";
                list += "<td style='width:325px;hieght:25px' align='left' valign='top'><a id='" + o.OrgID + "' href='#' onclick='javascript:fnSelectOrg(" + o.OrgID + ")' >" + o.OrgName + "</a></td>";
                list += "</tr>";
            }
            list += "</table>";
            divOrg.InnerHtml = list;
        }
        }
    }

I assume ASP.NET classic, not MVC.

If you create your controls in the Page_Load event then all your state will be destroyed after a Postback, because Viewstate is evaluated after Page_Init and before Page_Load. But if you create your controls in Page_Load it will not be present when the Viewstate is evaluated.

Try moving control creation to Page_Init

See also http://msdn.microsoft.com/en-us/library/ms178472.aspx

EDIT

Oh, I see.

1) When creating ASP.NET controls dynamic, you have to re-create them every time, not only in initial Get Request. ASP.NET will not maintain Controls for you, just their state.

2) You are exposing plain HTML. ASP.NET does not know about them. But: In the Request object you can find the original posted data

var value = Request["someid"]

You have two options:

1) Create ASP.NET Controls and add them to the controls tree (in page_init)

2) Write out plain HTML input tags (can be done in OnPreRender) and read the result via Request[...]

As Arthur said, if you create controls in Page_Load() event, move control creation in Page_Init(). If it does not help, post code for more information

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