国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂尔多斯市| 玛纳斯县| 宜兰市| 葵青区| 洪泽县| 拉萨市| 黄浦区| 思茅市| 若羌县| 响水县| 鹤岗市| 枣强县| 广灵县| 庐江县| 浙江省| 闸北区| 旅游| 尉犁县| 灵宝市| 庆安县| 隆尧县| 文昌市| 牙克石市| 团风县| 龙川县| 安泽县| 合山市| 闸北区| 铁岭县| 光泽县| 吉林市| 淳化县| 探索| 军事| 北碚区| 洮南市| 濮阳市| 湄潭县| 垣曲县| 葵青区| 新闻|