웹 컨트롤을 채우기 위해 .id를 얻으려면 .ClientID에 전화해야합니다.

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

  •  05-07-2019
  •  | 
  •  

문제

프로젝트를위한 양식 프레임 워크를 만들고 있으며 프로그래밍 방식으로 설정하려고 할 때 관련 구성 ASP의 속성 : 관련 텍스트 상자에 레이블을 호출해야합니다. ClientId 얻기 위해 ID 채찍질 (null이 아님).

디버깅하는 동안 중간 창을 사용하면 문제가 나타납니다.

_inputTextBox.ID
null
_inputTextBox.ClientID
"ctl00_MainContent_ctl01"
_inputTextBox.ID
"ctl01"

(값은 두 번째 시간입니다 ID 불린다)

나는 설정하고있다 _inputLabel.AssissociatedControlid = _inputtextbox.id 우선적으로 CreateChildControls (), 그리고 시도했습니다 RenderContents () 내 WebControl.

로드를 시도했습니다 ClientId 사용하지 않은 온도 변수로 먼저 작동하며 예를 들어 작동합니다.

var x = _inputTextBox.ClientID;
_inputLabel.AssociatedControlID = _inputTextBox.ID;

주는 : (정확한)

<label for="ctl00_MainContent_ctl01">Name of customer</label>
<input type="text" id="ctl00_MainContent_ctl01" name="ctl00$MainContent$ctl01"/>

대신 : (잘못)

<span>Name of customer</span>
<input type="text" id="ctl00_MainContent_ctl01" name="ctl00$MainContent$ctl01"/>

내 질문은 - 왜이 해킹을 수행해야합니까? 더 좋은 방법이 있습니까? ID 확실하게 채워지기 위해?

감사.

도움이 되었습니까?

해결책

The best thing to do is set the id on the control yourself. If you don't one will automatically be generated when the controls protected EnsureID method is called.
This method is automatically called at various times including, but not limited to, when the getter for ClientID is accessed.
You should also be aware that if you are dynamically manipulating controls then it is possible that the dynamically generated ids could vary based on the order that you add the controls to the page.
Also, even if you aren't dynamically creating the controls the dynamically generated id can vary if you force the id generation (by accessing ClientID) in a different order.

다른 팁

The ID of dynamically created controls is null unless explicitly defined until the RenderControl method is called.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top