Filling a Dropdownlist kinda old fashin way

liunx

Guest
Hi all,
I am not liking the new databind stuff in .net. It feels like i get limited. In asp i would do something like below, custom funtions which i could do all sorts of different stuff,
Is there away to do this with out getting locked up in .nets binding codes thanks
Function PrintAgencyList(Db as object,SelectedAgency as string)
Dim sSQL
Dim Rs as object
Dim sSelected

sSQL = "exec sp_ComboAgency " & Session("OPID")

Rs = Db.Execute(sSQL)

Rs = Db.Execute(sSQL)
While not rs.eof
if SelectedAgency = rs.Fields("Agency").value & "" then
sSelected = " selected "
else
sSelected = " "
end if
Response.Write("<option " & sSelected & " value=""" & rs.Fields("Agency").value & """>" & rs.Fields("Agency").value & " " & rs.Fields("Agency_Name").value & " </option>" )


Rs.Movenext
End While
rs.close
rs = nothing


end Functionthe asp label web control accepts html code and will rend just like normal html.

if you call the label control: msgPrint.

use your code above, and change this:

Response.Write("<option " & sSelected & " value=""" & rs.Fields("Agency").value & """>" & rs.Fields("Agency").value & " " & rs.Fields("Agency_Name").value & " </option>" )


to this:

msgPrint.Text = ("<option " & sSelected & " value=""" & rs.Fields("Agency").value & """>" & rs.Fields("Agency").value & " " & rs.Fields("Agency_Name").value & " </option>" )


p.s:
If I may suggest, I highly recommand one start to adept OOP programming, because scripting after all, is hard to keep up with today's complex computing.Let me rephrase my question then how can i do custom tags or coding usin the bind stuff thankshow can i do custom tags or coding usin the bind stuff thanks

the post I wrote above is "custom tags" approach,
if you want to use data binding (I assume you want drop down list)

1) define sql string
2) open connecting
3) point your drop down list object's DataSource to the sql executeReader()
4) point your drop down list object's DataTextField to the text in your sql table
5) point your drop down list object's DataValueField to a ID off your sql table
6) issue a .DataBind() method for your drop down list object
7) its good to make sure nothing is selected, so call .SelectedINdex = -1

for example... (its gonna be in C#)

SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("sp_ComboAgency", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@OPID", Session["OPID"]));
lstDropBox.DataSource = cmd.ExecuteReader(); //the name of ur drop box
lstDropBox.DataTextField = "Agency_Name";
lstDropBox.DataValueField = "Agency";
lstDropBox.DataBind();
con.Close();
lstDropBox.SelectedIndex = -1; //nothing is cucrently selectedsee when you do datasource textfield and value you are getting really limitted to do custom actions on rows but still thanks for the help. I actually did this which i actally tried and made it work so i can do custom stuff for each record if i need to.
<!-- m --><a class="postlink" href="http://www.webdeveloper.com/forum/showthread.php?t=92173">http://www.webdeveloper.com/forum/showt ... hp?t=92173</a><!-- m --> is the post that show the code sorry for taking your time. Thanks tho.see when you do datasource textfield and value you are getting really limitted to do custom actions on rows

When it come with simple control such as drop down list, you are right... datasource won't be as flexable, and you probably need to do more programming to order for simple control to work with datasource. By then, might as well stick with the legacy asp approach.



I am not liking the new databind stuff in .net. It feels like i get limited.


When it comes to a more advance control such as dataGrid, dataList, etc. You can customize your tags at your aspx page, and therefore it has a higher degree of flexability. You can also put additional asp web controls withing dataGrid/dataList which makes the complex application itself easier to manage. In this case, the legacy asp approach will be hard to keep up. I'm not saying it can't be done, programmers been using the older approach for the last 8-10 some yrs, and I'm just suggest there's a good reason for using the OOP approach when it comes to more complex application.

But yea, I do see your point of datasource being limited with it come with simple control.

regard,Your right on that i do use datagrids alot and it helps the process big time and i do have to use databind but the good part which lets me customize my code is DataGrid1_ItemDataBound function. It totally gives me all the flexibility. And eases my job big time. I was doing this post because a dropdown. May it has itemdatabound too that i dont know of (i should find that out actually.)
OOP is what i do most of the time some limitations kill's me but you can get around it to make it work Thanks for the help and input.
 
Back
Top