Click here to Skip to main content
15,886,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a button control in code behind of an aspx page. I want to set this button control as TargetControlID of a ModalPopupExtender.
Here is what I am coding:
C#
Button btnPostComment = new Button();
 btnPostComment.Text = "Post Your Comment Here";
 btnPostComment.ID = "btnPostComment";
 ModalPopupExtender1.TargetControlID = Convert.ToString(Page.FindControl(Convert.ToString(btnPostComment.ClientID)));


It gives an exception:
The TargetControlID of 'ModalPopupExtender1' is not valid. The value cannot be null or empty.

How can I set the targetControlID at codebehind?
Posted
Updated 2-Jun-10 19:29pm
v3
Comments
Sandeep Mewara 3-Jun-10 16:03pm    
Looks like you didnt got what i said.
STEP1: I said, assign a valid TargetcontrolID (lets say xyzButton) at design time in your Designer.
STEP2: Now, at runtime, just change the targetcontrolID from xyzButton to btnPostComment.
STEP3: Hide the xyzButton using style display attribute

You can check if everything is working and correct just after STEP1 to make sure things are in right direction.
Karunish 3-Jun-10 16:13pm    
I have followed the exact steps :(
In the design window I have first dropped a dummy button btnPostCommentHidden and set that as the TargetControlID of ModalPopupExtender.
Then in the Code behind, I am creating a button and then assigning it as the TargetControlID for the ModalPopUp.
I debugged and found that the TargetControlID is changed to what I set in code behind.
Still the same result. . .Is there a way I can perform the click of the dummy button which is on design view on the click even of the button which is created on the code behind ?
Karunish 3-Jun-10 16:14pm    
By the way I checked that, if I don't use the code behind button and I use the one which is by default on the design view it works fine. . .

Try:
ModalPopupExtender1.TargetControlID = "btnPostComment";
 
Share this answer
 
Comments
Karunish 3-Jun-10 13:56pm    
I have tried that before also, but it didn't worked still get the same error.
I have tried btnPostComment.UniqueID and btnPostComment.ID
Also I have tried using the ID which is there in view source from browser at runtime.
Ok, here is your issue. Looks like in designtime you have not assigned any control as TargetControlID. Thus you get an exception. What you need is provide anything by default from before such that execution of code works well and later at runtime you can change the targetControl as you want.

Try this:
Set a hidden button in the page as the targetcontrolID. (while design time itself)
By hidden button i mean, place a button and set it's style as display:none
Now, at runtime change the targetControlID to "btnPostComment"

That should work!
 
Share this answer
 
Comments
Karunish 3-Jun-10 15:59pm    
Thanks for your suggestions Sandeep !
Here is what I tried after your last suggestion:
.aspx file code:

<asp:button runat="server" id="btnPostCommentHidden" width="0" height="0">
<ajaxtoolkit:modalpopupextender runat="server"
="" id="ModalPopupExtender1" cancelcontrolid="btnCancel" okcontrolid="btnOk" targetcontrolid="btnPostCommentHidden" popupcontrolid="PanelComments" popupdraghandlecontrolid="PopupHeader" drag="true" backgroundcssclass="ModalPopupBG">


Here is the code behind :
Button btnPostComment = new Button();
btnPostComment.Text = "Post Your Comment Here";
btnPostComment.ID = "btnPostComment";
string targetID = Convert.ToString((btnPostComment.ID));
ModalPopupExtender1.TargetControlID = targetID;
commentsPane.ContentContainer.Controls.Add(btnPostComment);

I have tried setting the targetID string with btnPostComment.UniqueID , "btnPostComment" and btnPostComment.ClientID

Still the outcome is same : The TargetControlID of 'ModalPopupExtender1' is not valid. A control with ID 'btnPostComment' could not be found.

I am glad you understand my requirement exactly now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900