1

I am new to ASP.NET and C#.

What I am trying to do is some type of function where I can feed some argumennts, which will generate a dropdown list box for me. I know ASP.NET is much better, but I couldn't figure out how to accomplish similar or even better.

When I've done before with classic ASP/VBScript was I have a Sub routine to generate a dropdown list. Example:

Sub CreateSelectBox(selectboxID, onChangeTrigger, selectedValue, SQLTable, and SQLCondition)
     ' ... Query the databse from SQLTable, write a SelectBox with option values, and selected the selectedValue for arguments....
End Sub

So, all I have to do in the any submit form is just one line of code. like this:

<tr><td><%CreateSelectBox "DropDownList1", "onChangeRunJavascript123();", "Selected123", "SQL_CustomerTable", "where CustomerType = 'Consumer' order by SortOrder ASC" %></td></tr>

Please advise a better way to do this in ASP.Net C#, please provide a code sample if possible since I am new.

Thanks in advance,

1

1 Answer 1

2

Asp.net has a DropdownList control that helps to do that, this is very old tutorial and Databound Controls

Markup: .aspx file

<asp:DropdownList runat="server" ID="MyList" DataTextField="PersonName" DataValueField="PersonID">

</asp:DropdownList>

CodeBehind: .aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
       MyList.DataSource = getDataFromDB();
       MyList.DataBind();
   }
}


//this function can return IEnumerable e.g. DataTable of Person, List<Person>, SqlDataReader etc
private DataTable getDataFromDB()
{
   //select data from database
   //return data;
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1. I'd also suggest that Milacay reads up on data bound controls in general - that syntax suggests a background in ASP or Php :)
Yes I agree. I couldn't find a tutorial that's simple
Sam's Teach Yourself ASP.Net in ...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.