form inside User Control

Im having a problem getting my form to work when placed inside a user control. I have this piece of code inside a user control:<BR>__________________________________________________ _________<BR><script language="vb" runat="server"><BR>Sub formhandler(Sender As Object, E as EventArgs)<BR> if request.form("name")="" then<BR> nameerror.text="Field is blank"<BR> else<BR> nameerror.text=""<BR> end if<BR>end sub<BR></script><BR><BR><form runat="server" method="get"><BR> <asp:textbox id="name" size="20" runat="server"/><BR> <asp:label id="nameerror" text="" runat="server"/><BR> <asp:button class="form" id="submit" onClick="FormHandler" runat="server" text="submit"/><BR></form><BR>__________________________________________________ _____________<BR><BR>When I use the form I can't get access to the posted data via the request.form method thus I always get "Field is blank".<BR>Is there something I should be doing when using a form inside a user control?<BR>Don't use Request.Form("name"). Rather, simply use the programmatic name of the Web control: name.<BR><BR>Sub formhandler(Sender As Object, E as EventArgs)<BR> if name.Text = "" then<BR> nameerror.text="Field is blank"<BR> else<BR> nameerror.text=""<BR> end if<BR>end sub<BR><BR>In any event, a much BETTER way to do what you are doing is to use the RequiredFieldValidator control. See: http://aspfree.com/aspnet/validator.aspx<BR><BR>hthYou need to use method=POST instead of method=GET<BR><BR>You should also check the generated HTML to make sure the name of the textbox does not change (I am pretty sure that the server _does_ change it) if you want to use Request.Form.<BR><BR>Is there a reason you don't want to use "name.text"?Well I have a mixed web and html controls i.e<BR><input id="picture" size="15" type="file" class="form" runat="server"/><BR><BR>I need to check this htmlcontrol as well as other web controls so thats why I went with using request.form. I tried get and post but no luck.There is absolutely no way you can upload a file using method=GET. Parameters from a GET form are just collected and posted with the querystring. You can't post binary data in the querystring and there is a size limit anyway.<BR><BR>If you are uploading files, you need to specify enctype="multipart/form-data" in the form tag. Another suggestion I would have is to use better naming convensions for your elements. Names like "Name" and "Picture" may give you problems. Try prefixing them with "txt" or something.<BR><BR><form id="frmUpload" method="post" enctype="multipart/form-data" runat="server"><BR><BR>If you are mixing Server-side controls with regular HTML INPUT tags, use Request.Form for the INPUT values and TextBoxName.Text for the Server side controls. I still don't know why you _have_ to use non-server side controls. You can still specify the runat=server attribute for regular HTML INPUT tags and use MyHTMLInput.Value in the ASP.net code to get the values.<BR><BR>ASP.net often changes the name of the server side controls to something other than what you have in the ID attribute. This is especially true with User Controls. You will usually get something like the following:<BR>name="MyControl:txtName"<BR>name="MyControl:txtFile"<BR><BR>So you can't use Request.Form("txtName") anyway.<BR><BR><BR>I don't think I explained the last bit enough. This is what I have inside a user control which is then used in other pages.<BR>-------------------------------------------------------<BR>Sub validateData(sender As Object, e As EventArgs)<BR> if picture.value=http://aspmessageboard.com/archive/index.php/"" then<BR> pictureReport.text="Please fill in above field"<BR> else<BR> pictureReport.text=""<BR> end if<BR>End Sub<BR><BR><form method="post" enctype="Multipart/Form-Data" runat="server"><BR><input id="picture" name="picture" size="15" type="file" class="form" runat="server><BR><asp:label id="pictureReport" class="white_8" runat="server" text=""/><BR><asp:button id="submit" class="form" runat="server" text="Submit" onClick="validateData"/><BR></form><BR>--------------------------------------------------------<BR>All the server controls work fine in the form (not shown above) but I have to use the HTML file input as there isn't an ASP.net version and I need to check if its been filled in.<BR>When the page is run with the user control I can't get access to the picture text box values. I've tried picture.value and request.form("picture") but the values are not there.<BR><BR>When I look at the output code the id of the html input text box changes to USERCONTROLNAME_picture so I tried saying USERCONTROLNAME_picture.value but still nothing.You cannot reference a server control using its generated name <BR>attribute. Try this instead (WARNING - UNTESTED CODE):<BR><BR>Sub validateData(sender As Object, e As EventArgs)<BR>if txtPicture.PostedFile.FileName="" then<BR>pictureReport.text="Please fill in above field"<BR>else<BR>pictureReport.text=""<BR>end if<BR>End Sub<BR><BR><form method="post" enctype="Multipart/Form-Data" runat="server"><BR><input id="picture" name="txtPicture" size="15" type="file" class="form" runat="server><BR><asp:label id="pictureReport" class="white_8" runat="server" text=""/><BR><asp:button id="submit" class="form" runat="server" text="Submit" onClick="validateData"/><BR></form><BR>
 
Back
Top