首页 > 开发 > 综合 > 正文

DPC:Creating a DataBound List of Radio Buttons--PA

2024-07-21 02:16:49
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

creating a databound list of radio buttons



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

for more information on asp.net
this article examines how to create a databound listbox using asp.net. for more information on asp.net be sure to check out the articles in the asp.net article index. the code in this article is based on the beta 2 version of asp.net. to learn more about the free asp.net beta 2, be sure to check out this faq.  


introduction
a common task web developers face is creating a form element (such as a series of checkboxes, radio buttons, or a listbox) whose members are populated by the contents of a database table. this is quite possible (and quite easy) to accomplish with asp.net in fact, in a previous article (creating databound dropdown lists in asp.net), we examined how to create a databound listbox. in this article, we'll examine how to create a series of radio buttons whose values are automagically populated from a database table! we'll also look at how to respond to the user selecting a particular option and showing more detailed information based upon the radio button selected.

the impetus
on 4guys there is a sample on-line chapters page, which lists a number of books that have free, on-line sample chapters. all of this information is stored in a database that contains two tables: tblpublishers and tblbooks. the tblpublishers contains a row for each publisher that has a book that appears in the sample chapters listing, while the tblbooks table contains a row for each sample book. note that the tblbooks table contains a publisherid column which serves as a foreign key to the tblpublishers table, tying a particular publisher to a particular book.

wouldn't it be nice to generate a list of radio buttons based upon the publishers in the tblpublishers table? from this list, the user could select a particular publisher, at which point, the user would be shown a listing of books published by that particular publisher. in creating the radio button list, we could hard code the list of publishers, but that would be subject to change any time we added or removed publishers from the tblpublishers table. ideally, the radio button list would be dynamically generated based upon the values in the tblpublishers table.

if you'd like to see what the end result will look like, check out the checkboxlistdemo.aspx">live demo. pretty nice looking, eh? realize that the radio button list was created dynamically using databind (essentially three lines of code in total). and this entire page, from start to finish, took me about four minutes to write. (i type fast. :-))

creating the databound radio button list
creating a databound series of radio buttons is painfully simply with asp.net. we only really need to do three things:


create the asp:radiobuttonlist web control, specifying the database columns we want to bind to the radio buttons' displayed text values and the hidden values that are passed along when the form is submitted.

write the code to populate a datareader (or dataset) with the applicable database information.

databind the datareader (or dataset) to the asp:radiobuttonlist web control
that's it! so, let's examine each of these three steps. first, create the needed web control in the html section of your asp.net web page.

<html>
<body>
  <form runat="server">
    <b>choose a publisher's books to view</b><br>
    <asp:radiobuttonlist id="radlstpubs" runat="server"
             datavaluefield="publisherid" datatextfield="name" />
  </form>
</body>
</html>




the two most important things to notice is that we set the asp:radiobuttonlist web control's datavaluefield and datatextfield properties. both properties should map to a column name in the database table that we wish to bind the radio button list to. the datatextfield property are the names that will be visually displayed next to each radio button; the datavaluefield are the hidden values that are associated with each radio button. also note that the radio button list was placed within a webform (the form tag with a runat="server" attribute).

now that we've tackled our first step, let's look at performing our remaining two steps: reading the database table tblpublishers into a datareader and binding the populated datareader to the radio button list!

<% @import namespace="system.data" %>
<% @import namespace="system.data.sqlclient" %>
<script language="vb" runat="server">
  sub page_load(sender as object, e as eventargs)
    if not page.ispostback then
      '*** step 1: populate a datareader ***
      'create a connection
      dim myconnection as new _
          sqlconnection(configurationsettings.appsettings("connectionstring"))

      'create the command object, passing in the sql string
      const strsql as string = "select publisherid, name from tblpublishers"
      dim mycommand as new sqlcommand(strsql, myconnection)

      'open the connection and set the datasource to the
      myconnection.open()
      
      'populate the datareader
      dim mydatareader as sqldatareader
      mydatareader = mycommand.executereader(commandbehavior.closeconnection)

      '*** step 2: bind the datareader to radlstpubs ***
      radlstpubs.datasource = mydatareader
      radlstpubs.databind()  
    end if  
  end sub

  ...
</script>




in the above page_load event handler we accomplish our two steps: populating a datareader (in this case a sqldatareader) with the contents of the tblpublishers table; and binding the resulting datareader to our radio button list, radlstpubs. note that we only wish to run this binding code on the first visit to the page (more on why later), hence we only perform the databinding if page.ispostback is false, meaning that the page has not yet been posted back.

now that we've looked at how to create a databound radio button list, let's turn our attention to responding to the user's selection of a particular radio button. in part 2 we'll examine this aspect and the remainder of our application!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表