DPC:Creating a DataBound List of Radio Buttons--PA
2024-07-21 02:27:38
供稿:网友
in part 1 we looked at how to bind a datareader to a radio button list, but how do we respond to the user's choice? in this part we'll look at how to display a list of books for the particular publisher the user selected from the databound radio button list!
responding to a user's radio button choice
what we'd like to accomplish is to allow the user to choose a radio button option, click a button, and have the chosen publisher's books appear. so, to accomplish this, we'll need to first add a button control after the radio button list control in our html section:
<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" />
<br>
<asp:button id="btnviewbooks" runat="server"
text="view published books" onclick="btnviewbooks_click" />
</form>
</body>
</html>
note that the btnviewbooks button control's onclick event handler was wired up to the btnviewbooks_click event handler. this is an event handler that we'll create in our server-side script block. this event handler will need to ascertain what radio button option was selected and construct a sql query based upon that information. (this sql query will snag all of the books out of the tblbooks table whose publisherid matches the id of the selected publisher from the radio button list.) furthermore, a datareader (or dataset) should be populated with the results of this custom sql query, and that datareader should be bound to a datagrid web control in order to display the books for the publisher.
so, we'll need one more web control, a datagrid, which will be used to display the books published by the selected publisher. the datagrid web control i used can be seen below, it should come after the button control (which has been removed for brevity). feel free to make any stylistic changes to the datagrid:
<html>
<body>
<form runat="server">
... content removed for brevity ...
<p align="center">
<asp:label id="lbltitle" runat="server" font-name="verdana"
font-size="large" font-bold="true" />
<asp:datagrid id="dgbooks" runat="server"
font-name="verdana" font-size="smaller"
headerstyle-backcolor="purple" headerstyle-forecolor="white"
headerstyle-font-size="small" headerstyle-font-bold="true"
autogeneratecolumns="false">
<columns>
<asp:boundcolumn headertext="book title"
headerstyle-horizontalalign="center"
datafield="title" />
<asp:boundcolumn headertext="synopsis"
headerstyle-horizontalalign="center"
datafield="description" />
</columns>
</asp:datagrid>
</p>
</form>
</body>
</html>
notice that a label web control, lbltitle, was also added. this will be used to display a nice-looking heading above the datagrid web control.
all that's left to tackle is writing the btnviewbooks_click event handler, which will fire whenever the user clicks the btnviewbooks button control. remember that this event handler needs to populate a datareader with a list of books that were published by the selected publisher.
... page_load event handler removed for brevity ...
sub btnviewbooks_click(sender as object, e as eventargs)
'if the user has not selected an item from the radiobuttonlist,
'do nothing
if radlstpubs.selecteditem is nothing then exit sub
'create a connection
dim myconnection as new _
sqlconnection(configurationsettings.appsettings("connectionstring"))
'create the command object, passing in the sql string
dim strsql as string = "select title, description from tblbooks " & _
" where publisherid = " & radlstpubs.selecteditem.value & _
" order by title"
dim mycommand as new sqlcommand(strsql, myconnection)
myconnection.open()
dgbooks.datasource = mycommand.executereader(commandbehavior.closeconnection)
dgbooks.databind()
lbltitle.text = "books published by " & radlstpubs.selecteditem.text
end sub
</script>
[view a live demo!]
in the btnviewbooks_click event handler we begin by making sure that a radio button option was selected. if it wasn't, we simple exit the event handler. next, we connect to the database and populate a sqldatareader with the books that were published by the selected publisher. note that we are creating a custom sql string, returning records where the publisherid column in the tblbooks table is equal to the selected radio button's value property (which, if you recall, was the publisherid of the tblpublishers table (the primary key)).
once we have our populated sqldatareader, we can simply bind it to the dgbooks datagrid web control. this, as you can see, takes only two lines of code! finally, we set the lbltitle label control to a descriptive title. and... we're done! that's it!
conclusion
all in all, we created a very useful and visually appealing data-driven web application in a matter of minutes! amazing. of course, radio button lists are not the only form element that can be data bound. listboxes (see creating databound dropdown lists in asp.net) and checkbox lists can also be data bound in a similar fashion. well, i hope you learned something new and are as excited about asp.net as i am!
happy programming!
,欢迎访问网页设计爱好者web开发。