Question : asp.net I am trying to convert an html form into an aspx form or ? what ever. Need Help on how to to it.

asp.net I am trying to convert an html form into an aspx form or ? what ever. Need Help on how to it.

I have just started trying to convert a html dropdown to a aspx dropdownlist using a db as a source. An i got this error. so I am on the wrong foot..I have provided the complete page.

----------------

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Source Error:


Line 92:           </td>
Line 93:         <td class="small">
Line 94:         <asp:DropDownList ID="DropDownList3" runat="server" Width="160"
Line 95:                DataSourceID="SqlDataSource3" DataTextField="Make" DataValueField="Make"
Line 96:                SelectedValue='<%# Bind("Make") %>'>
 

Source File: C:\WebSite_m4\Annonymous\SearchEquipment.aspx    Line: 94
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
<%@ Page Language="VB" AutoEventWireup="false" MasterPageFile="~/Nested Master Pages/annonymous.master" %>

<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
 
    End Sub
 
    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
     
        ' Get a reference to the currently logged on user
        Dim currentUser As MembershipUser = Membership.GetUser()
        ' Determine the currently logged on user's UserId value

        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
        
        ' Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters("@UserId").Value = currentUserId
        
    End Sub
    
    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'If Cancel button is clicked, send user to Default
        Response.Redirect("~/Annonymous/index.aspx")
    End Sub

    Dim currentUser As MembershipUser

    Public Sub Page_Load()
        currentUser = Membership.GetUser()
    End Sub
   

    'Welcome: <b><%=currentUser.UserName%></b>.<br />
'UserId: <b><%=currentUser.ProviderUserKey%></b>.  
</script>

<%-- Add content controls here --%>

<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" 
                
                SelectCommand="SELECT Year, Make, Model, Type, MilesHours, VinSerial, Description, Location, Price FROM Equipment Where IsLockedOut = 'False'" 
                
                InsertCommand="INSERT INTO Equipment(UserId, Year, Make, Model, Type, MilesHours, VinSerial, Description, Location, Price, IsLockedOut, LastLockOutDate) VALUES (@UserId,@Year,@Make,@Model,@Type,@MilesHours,@VinSerial,@Description,@Location,@Price,@IsLockedOut,@LastLockOutDate)">
               
                <InsertParameters>
                    <asp:Parameter Name="UserID" />
                    <asp:Parameter Name="Year" />
                    <asp:Parameter Name="Make" />
                    <asp:Parameter Name="Model" />
                    <asp:Parameter Name="Type" />
                    <asp:Parameter Name="MilesHours" />
                    <asp:Parameter Name="VinSerial" />
                    <asp:Parameter Name="Description" />
                    <asp:Parameter Name="Location" />
                    <asp:Parameter Name="Price" />
                    <asp:Parameter Name="IsLockedOut"  DefaultValue="False" />
                    <asp:Parameter Name="LastLockOutDate"  DefaultValue="" />
                </InsertParameters>
            </asp:SqlDataSource>

            <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" 
                DeleteCommand="DELETE FROM [Type] WHERE [TypeID] = @TypeID" 
                InsertCommand="INSERT INTO [Type] ([TypeID], [Type]) VALUES (@TypeID, @Type)" 
                SelectCommand="SELECT [MakeID], [Make] FROM [Make] ORDER BY [Make]" 
                UpdateCommand="UPDATE [Type] SET [Type] = @Type WHERE [TypeID] = @TypeID">
                <DeleteParameters>
                    <asp:Parameter Name="TypeID" Type="Object" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Type" Type="String" />
                    <asp:Parameter Name="TypeID" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="TypeID" Type="Object" />
                    <asp:Parameter Name="Type" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>

       <table> ????
        <tr>
         <td class="small">
           Truck Type:
          </td>
        <td class="small">
        <asp:DropDownList ID="DropDownList3" runat="server" Width="160"
               DataSourceID="SqlDataSource3" DataTextField="Make" DataValueField="Make" 
               SelectedValue='<%# Bind("Make") %>'>
        </asp:DropDownList>
            
        </td>
        </tr>
  
        <tr>
        <td class="small">
         Make:
         </td>
        <td class="small">
        <select name="ctl00$contentHolder$ctlSearch$ddlMake" id="ctl00_contentHolder_ctlSearch_ddlMake" class="small">
		<option value="">All</option>
		<option value="Autocar">Autocar</option>
		<option value="Bering">Bering</option>
		<option value="Capacity">Capacity</option>
		<option value="Caterpillar">Caterpillar</option>
		<option value="Century">Century</option>
		<option value="Chevrolet">Chevrolet</option>
		<option value="Dodge">Dodge</option>
		<option value="Ford">Ford
     </option>

		<option value="Ford">Ford</option>
		<option value="Freightliner">Freightliner</option>
		<option value="FWD">FWD</option>
		<option value="GMC">GMC</option>
		<option value="Hino">Hino</option>
		<option value="Holmes">Holmes</option>
		<option value="International">International</option>
		<option value="Isuzu">Isuzu</option>
		<option value="Jeep">Jeep</option>
		<option value="Kenworth">Kenworth</option>
		<option value="Mack">Mack</option>
		<option value="Magnum">Magnum</option>
		<option value="Marmon">Marmon</option>
		<option value="Mercedes Benz">Mercedes Benz</option>
		<option value="Mitsubishi-Fuso">Mitsubishi-Fuso</option>
		<option value="Oshkosh">Oshkosh</option>
		<option value="Ottawa">Ottawa</option>
		<option value="Palfinger">Palfinger</option>
		<option value="Peterbilt">Peterbilt</option>
		<option value="Sterling">Sterling</option>
		<option value="UD">UD</option>
		<option value="Volvo">Volvo</option>
		<option value="Western Star">Western Star</option>
		<option value="White">White</option>
		<option value="White/Volvo">White/Volvo</option>
		<option value="WhiteGMC">WhiteGMC</option>
		<option value="Workhorse">Workhorse</option>
	  </select>
             </td>
                </tr>
                <tr>
                    <td class="small">
                        Model:
                    </td>
                    <td class="small">
                        <input name="ctl00$contentHolder$ctlSearch$txtModel" type="text" id="ctl00_contentHolder_ctlSearch_txtModel" class="small" />
                    </td>
                </tr>
                <tr>
                    <td class="small">
                        Year:
                    </td>
                    <td class="small" nowrap="1">

         <select name="ctl00$contentHolder$ctlSearch$ddlYearMin" id="ctl00_contentHolder_ctlSearch_ddlYearMin" class="small">
		<option value="0">-----</option>
		<option value="2011">2011</option>
		<option value="2010">2010</option>
		<option value="2009">2009</option>
		<option value="2008">2008</option>
		<option value="2007">2007</option>
		<option value="2006">2006</option>
		<option value="2005">2005</option>
		<option value="2004">2004</option>
		<option value="2003">2003</option>
		<option value="2002">2002</option>
		<option value="2001">2001</option>
		<option value="2000">2000</option>
		<option value="1999">1999</option>
		<option value="1998">1998</option>
		<option value="1997">1997</option>
		<option value="1996">1996</option>
		<option value="1995">1995</option>
		<option value="1994">1994</option>
		<option value="1993">1993</option>
		<option value="1992">1992</option>
		<option value="1991">1991</option>
		<option value="1990">1990</option>
		<option value="1989">1989</option>
		<option value="1988">1988</option>
		<option value="1987">1987</option>
		<option value="1986">1986</option>
		<option value="1985">1985</option>
		<option value="1984">1984</option>
		<option value="1983">1983</option>
		<option value="1982">1982</option>
		<option value="1981">1981</option>
		<option value="1980">1980</option>
		<option value="1940">pre-1980</option>
 
	</select>
         to
         <select name="ctl00$contentHolder$ctlSearch$ddlYearMax" id="ctl00_contentHolder_ctlSearch_ddlYearMax" class="small">
		<option value="9999">-----</option>
		<option value="2011">2011</option>
		<option value="2010">2010</option>
		<option value="2009">2009</option>
		<option value="2008">2008</option>
		<option value="2007">2007</option>
		<option value="2006">2006</option>
		<option value="2005">2005</option>
		<option value="2004">2004</option>
		<option value="2003">2003</option>
		<option value="2002">2002</option>
		<option value="2001">2001</option>
		<option value="2000">2000</option>
		<option value="1999">1999</option>
		<option value="1998">1998</option>
		<option value="1997">1997</option>
		<option value="1996">1996</option>
		<option value="1995">1995</option>
		<option value="1994">1994</option>
		<option value="1993">1993</option>
		<option value="1992">1992</option>
		<option value="1991">1991</option>
		<option value="1990">1990</option>
		<option value="1989">1989</option>
		<option value="1988">1988</option>
		<option value="1987">1987</option>
		<option value="1986">1986</option>
		<option value="1985">1985</option>
		<option value="1984">1984</option>
		<option value="1983">1983</option>
		<option value="1982">1982</option>
		<option value="1981">1981</option>
		<option value="1980">1980</option>
 
	</select>
       </td>
        </tr>
       <tr>
          <td class="small">
             Engine HP:
       </td>
       <td class="small">
        <select name="ctl00$contentHolder$ctlSearch$ddlHPMin" id="ctl00_contentHolder_ctlSearch_ddlHPMin" class="small">
		<option value="0">No Min</option>
		<option value="600">600</option>
		<option value="550">550</option>
		<option value="500">500</option>
		<option value="450">450</option>
		<option value="400">400</option>
		<option value="350">350</option>
		<option value="300">300</option>
		<option value="250">250</option>
		<option value="200">200</option>
		<option value="150">150</option>
 
	   </select>
                to
             <select name="ctl00$contentHolder$ctlSearch$ddlHPMax" id="ctl00_contentHolder_ctlSearch_ddlHPMax" class="small">
		<option value="9999">No Max</option>
		<option value="600">600</option>
		<option value="550">550</option>
		<option value="500">500</option>
		<option value="450">450</option>
		<option value="400">400</option>
		<option value="350">350</option>
		<option value="300">300</option>
		<option value="250">250</option>
		<option value="200">200</option>
		<option value="150">150</option>
 
	</select>
                    </td>
                </tr>
                <tr>
                    <td class="small">
                        Price:
                    </td>
                    <td class="small" nowrap="1">
                        <input name="ctl00$contentHolder$ctlSearch$txtPriceMin" type="text" id="ctl00_contentHolder_ctlSearch_txtPriceMin" class="small" style="width:75px;" />
                        to
                        <input name="ctl00$contentHolder$ctlSearch$txtPriceMax" type="text" id="ctl00_contentHolder_ctlSearch_txtPriceMax" class="small" style="width:75px;" />
                    </td>
                </tr>
                <tr>
                    <td class="small" nowrap="1">
                        State:
                    </td>
                    <td class="small">
                        <select name="ctl00$contentHolder$ctlSearch$ddlState" id="ctl00_contentHolder_ctlSearch_ddlState" class="small">
		<option value="">All</option>
		<option value="AL ">Alabama</option>
		<option value="AZ ">Arizona</option>
		<option value="AR ">Arkansas</option>
		<option value="CA ">California</option>
		<option value="CO ">Colorado</option>
		<option value="CT ">Connecticut</option>
		<option value="DE ">Delaware</option>
		<option value="FL ">Florida</option>
		<option value="GA ">Georgia</option>
		<option value="ID ">Idaho</option>
		<option value="IL ">Illinois</option>
		<option value="IN ">Indiana</option>
		<option value="IA ">Iowa</option>
		<option value="KS ">Kansas</option>
		<option value="KY ">Kentucky</option>
		<option value="LA ">Louisiana</option>
		<option value="ME ">Maine</option>
		<option value="MD ">Maryland</option>
		<option value="MA ">Massachusetts</option>
		<option value="MI ">Michigan</option>
		<option value="MN ">Minnesota</option>
		<option value="MS ">Mississippi</option>
		<option value="MO ">Missouri</option>
		<option value="MT ">Montana</option>
		<option value="NE ">Nebraska</option>
		<option value="NH ">New Hampshire</option>
		<option value="NJ ">New Jersey</option>
		<option value="NM ">New Mexico</option>
		<option value="NY ">New York</option>
		<option value="NC ">North Carolina</option>
		<option value="ND ">North Dakota</option>
		<option value="OH ">Ohio</option>
		<option value="OK ">Oklahoma</option>
		<option value="ON ">Ontario</option>
		<option value="OR ">Oregon</option>
		<option value="PA ">Pennsylvania</option>
		<option value="SC ">South Carolina</option>
		<option value="SD ">South Dakota</option>
		<option value="TN ">Tennessee</option>
		<option value="TX ">Texas</option>
		<option value="UT ">Utah</option>
		<option value="VT ">Vermont</option>
		<option value="VA ">Virginia</option>
		<option value="WA ">Washington</option>
		<option value="WV ">West Virginia</option>
		<option value="WI ">Wisconsin</option>
		<option value="WY ">Wyoming</option>
 
	</select>
                    </td>
             </tr>
         <tr>
                <td class="small">
                        Zip Code:
               </td>
         <td class="small">
        <input name="ctl00$contentHolder$ctlSearch$txtZip" type="text" id="ctl00_contentHolder_ctlSearch_txtZip" class="small" style="width:41px;" />
           within &nbsp;
        <select name="ctl00$contentHolder$ctlSearch$ddlMiles" id="ctl00_contentHolder_ctlSearch_ddlMiles" class="small">
		<option value="10">10</option>
		<option value="20">20</option>
		<option value="30">30</option>
		<option value="40">40</option>
		<option value="50">50</option>
		<option value="75">75</option>
		<option value="100">100</option>
		<option value="150">150</option>
		<option value="250">250</option>
		<option value="500">500</option>
		<option value="750">750</option>
		<option value="1000">1000</option>
		<option value="1500">1500</option>
 
	     </select>
                        miles
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="image" name="ctl00$contentHolder$ctlSearch$btnSearch" id="ctl00_contentHolder_ctlSearch_btnSearch" src="Images/LeadButtons/ATsearchtrucks_up.png" alt="Search Trucks" style="border-width:0px;" />
                    </td>
                </tr>
        </table>
   </form

</asp:Content>

Answer : asp.net I am trying to convert an html form into an aspx form or ? what ever. Need Help on how to to it.

yes. you have to get this error.

As Bind method is used only for binding data to data controls like gridview, repeater, datalist, etc.

If you have to bind dropdownlist  (or any other control) outside of these data controls then you have to explicitly set its value from code behind.

Like for e.g. when you are binding to dropdownlist.
at last you can set:

DropDownList3.Items.FindByValue("yourvaluehere").Selected  = true;
OR
DropDownList3.Items.FindByText("yourtexthere").Selected  = true;

Hope this clears you.
Random Solutions  
 
programming4us programming4us