How to resolve " a control with id X could not be located or a different control is assigned to the same ID after post back" in asp.net?

StackOverflow https://stackoverflow.com/questions/15410687

  •  23-03-2022
  •  | 
  •  

質問

Please help me to rectify this error.

An error has occurred because a control with id 'ctl81' could not be located or a different control is assigned to the same ID after post back. If the ID is not assigned, explicitly set the ID property of controls that raise post back events to avoid this error

This error occurred on a post back by an asp button click. All the controls in the page were dynamically generated. I tried to find this control in the page source but in vain.

    HtmlGenericControl td1 = new HtmlGenericControl("td");
    TextBox txt1 = new TextBox();
    txt1.ID = "toqty"+i.Tostring();
    td.controls.add(txt1);
    placeholder.controls.add(td);
役に立ちましたか?

解決

Since the error has occured due to a button click, the controls that where generated before the button click might have been cleared from your corresponding form. So i recommend you to check out the methods or codes that were written after the button click by pinning a breakpoint on the button_click() method. Try this and please leave a reply.

他のヒント

The error itself mentioned the solution. First check whether the ID is still assigned to the control after postback. If not, explicitly set the ID property to avoid this error

In my case similar exception was caused by a control which had id attribute set up forcibly in Render method, without setting its ID property to the same value.

Apparently, AutoID mode has problems where ID from code behind does not match rendered id attribute.

I preferred not to change Render stage because the control is widely used in a huge project and many functionalities rely on it working as it is. Instead I have set ID explicitly to ensure that it is unique and different from anything that can be set automatically. It helped.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top