从列表视图创建的控件中获取数据?

时间:2014-06-02 21:02:51

标签: c# asp.net database

您好,感谢您抽出宝贵时间阅读此问题。

我的数据库表: enter image description here

我的Asp代码:

<asp:Button runat="server" ID="btnReturn" Text="Return to Questions" PostBackUrl="~/Default.aspx" />
<asp:ListView runat="server" ID="lstQuestion">
    <ItemTemplate>
        <h1><%# Eval("Title") %></h1>
        <asp:RadioButtonList runat="server" id="rblAnswers">
            <asp:ListItem Text="yes"></asp:ListItem>
            <asp:ListItem Text="no"></asp:ListItem>
            <asp:ListItem Text="Maybe"></asp:ListItem>
        </asp:RadioButtonList>
        <asp:TextBox runat="server" ID="txttest"></asp:TextBox>
    </ItemTemplate>
</asp:ListView>
<br />
<asp:Button runat="server" ID="btntest" Text="Get Result" OnClick="btntest_Click" />

浏览器中的输出

enter image description here

我想要达到的目的是:如果用户按下 btntest ,页面就像上面那样。一个简单的response.write()(开头)应该写:no - Coment1 - yes - Coment 2。

基本它会写出已经选择的东西并且如果输入了某些内容就会被记录。

我希望你理解并抱歉我的英语。

1 个答案:

答案 0 :(得分:2)

您需要使用find控件来引用ListView中的控件。但是,如果要访问的控件是ListView ItemTemplate的一部分,则需要遍历每个项目并找到该项目的控件。

我在没有IDE的情况下编写了这个,它应该是这样的:

//Iterate through the rows of the List View
foreach (item ListViewItem in lstView.Items)
{
     //If the control is a data item
     if (item.ItemType = ListViewItemType.DataItem)
     {
          RadioButtonList  rbl = item.FindControl("rblAnswers") as RadioButtonList;
          if(rbl != null)
          {
          //do something
          }

          TextBox tb = item.FindControl("txttest") as TextBox;
          if(tb != null)
          {
          //do something    
          }
     }
}