Dynamically create imagebutton

G

Guest

Hello,

When a person subits my web page, I do a simple check (validation) and if
there are any errors, I create a variable (strValidateText) and set that
value to a label on the form. What I am trying to do create an image button
along with the html. I am getting the error: Operator '&' is not defined
for types 'String' and 'System.Web.UI.WebControls.ImageButton'.




Sub Submit_Click(ByVal Src As Object, ByVal Args As CommandEventArgs)
If Trim(Me.ddlRequestType.SelectedItem.Text) = "None Selected" Then
Dim ibRequestType As ImageButton = New ImageButton
ibRequestType.OnClientClick = "txtServicesTerminationCount.focus()"
ibRequestType.ImageUrl = "../../images/art_dbflash_go.jpg"

strValidateText = _
"<tr>" & _
"<td width=200 class=sckArial_BlkB08><div align=left>Request Type
DropDown &nbsp;</div></td>" & _
"<td width=400 class=sckArial_BlkM08><div align=left>Please select a
Request Type." & _
ibRequestType & </div></td></tr>"
strValidateForm = "Validation Error"
End If
End Sub

If strValidateForm = "Validation Error" Then Me.lblValidateForm.Text =
strValidateText
End Sub
 
G

Guest

A better way to do this is to use a ASP.Net placeholder webcontrol, and
dyanmically add to the placeholder's Control collection.

Dim ibRequestType As ImageButton = New ImageButton
ibRequestType.OnClientClick = "txtServicesTerminationCount.focus()"
ibRequestType.ImageUrl = "../../images/art_dbflash_go.jpg"

phMyPlaceholder.Control.Add(ibRequestType)

Hope this helps!

Joel Cade, MCSD
Fig Tree Solutions, LLC
http://www.figtreesolutions.com
 
S

Steven Cheng[MSFT]

Hi Sck10,

From the error message you provided, it seems that the problem is caused by
the string concatenation in the assignment statement of the
"strValidateText" variable, are you missing a quote (") and we should add
dynamic webcontrols onto page via some other means rather than simple
append them in html string:

From the code you provided, it seems that you has miss a " at the
following line
ibRequestType & </div></td></tr>"

the "ibRequestType" should be a type which can be convert to a string
value, however, it is an ImageButton control which is not a proper value.


As Joel has suggested, we can put an PlaceHolder control on the webform
page and add dynamic control into the PlaceHolder control( or other
container controls such as Panel, Table(TableCell)...). In addition, if
the dynamic controls have their own post back events and eventhandlers, we
should be care that we must add them in page's Init or Load event everytime
rather than only add them in another server control's postback event.
Otherwise, the dynamic control's postback event(eventhandler) won't be
fired.

Here is some tech articles and former threads on creating dynamic controls
on web form page:

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/?id=317794


http://groups.google.com/groups?hl=en&lr=&threadm=4gHm$tzMEHA.3316@cpmsf
tngxa10.phx.gbl&rnum=2&prev=/groups%3Fhl%3Den%26lr%3D%26q%3Dasp.net%2Bdynami
c%2Bcontrol%2Bpost%2Bback%2Bsteven%2Bcheng

http://groups.google.com/groups?hl=en&lr=&threadm=dD861VkOEHA.1016@cpmsftn
gxa10.phx.gbl&rnum=4&prev=/groups%3Fhl%3Den%26lr%3D%26q%3Dasp.net%2Bdynamic%
2Bcontrol%2Bpost%2Bback%2Bsteven%2Bcheng

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Sck10,

Have you had a chance to check the suggestions in my last reply or have you
got any further ideas on this problem? IF there're anything else we can
help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top