Dpc:creating a databound List of Radio buttons--part2[rank: in]

Source: Internet
Author: User
Tags bind exit query
In Part 1 we looked on 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 the "How to" display a list of the particular publisher the user selected from the Databoun D radio Button list!

Responding to a User ' s Radio Button Choice
What we like to accomplish are to allow the user to choose a radio button option, click a button, and have the chosen pub Lisher ' s books appear. So, to accomplish this, we'll need to the "a Button control" the radio button list control in our HTML section:

<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>




Note This Btnviewbooks button control ' s OnClick event handler is wired up to the Btnviewbooks_click event handler. This is the event handler that we'll create in our Server-side script block. This event handler would need to ascertain what radio button option is selected and construct a SQL query based upon that Information. (This SQL query would snag all of the books out of the Tblbooks table whose PublisherID matches the ID of the selected publ Isher from the radio button list.) Furthermore, a DataReader (or DataSet) should is populated with the results of this custom SQL query, and that DataReader Should is bound to a DataGrid, Web control, to display the "publisher".

So, we'll need one more Web control, a DataGrid, which'll be used to display the books published by the selected publish Er. The DataGrid Web control I used can is seen below, it should come after the button control which has the been for removed vity). Feel stylistic changes to the DataGrid:

<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>




Notice that's a label Web control, Lbltitle, is also added. This is 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'll fire whenever the user clicks the BT Nviewbooks Button control. Remember that this event handler needs to populate a DataReader with a list of "that were published by the selected p Ublisher.

... Page_Load EVENT HANDLER removed for brevity ...

Sub Btnviewbooks_click (sender as Object, E as EventArgs)
' If the user has not selected a 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 with making sure that a radio button option is selected. If It wasn ' t, we simple exit the event handler. Next, we connect to the database and populate a SqlDataReader with the "were published by" selected publisher . Note this we are creating a custom SQL string, returning records where the PublisherID column in the Tblbooks table is equ Al to the selected radio button's Value property (which, if you recall, is the publisherid of the tblpublishers table (th E primary key)).

Once We have we populated SqlDataReader, we can simply bind it to the Dgbooks DataGrid Web control. This, as and can, 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 ' can be data bound. ListBoxes (creating DataBound DropDown Lists in asp.net) and CheckBox Lists can also is data bound in a similar N. I hope you learned something new and are as excited about ASP.net as I am!

Happy programming!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.