【问题标题】:how use button with radiobuttonlist and Datalist in asp.net?如何在asp.net 中使用带有radiobuttonlist 和Datalist 的按钮?
【发布时间】:2014-11-08 18:06:56
【问题描述】:

请帮我完成一些小任务。 数据库中有两个表:Student 和 Operators。

学生有以下列:

Name
Login
City
Operator_id

运算符有以下列:

Operator_id
OparatorName
OperatorCode

RadiobuttonList 显示 OparatorName

我需要从RadiobuttonList 中选择OparatorName,然后按下按钮Operator selection 以在Datalist 中查看拥有OparatorName 的学生列表。

有人可以帮助我如何实现这一点。 谢谢

我在构造函数 VisualStudio 中编写了一些代码:它看起来像 `

    <br />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="OperatorName" DataValueField="OperatorName">
    </asp:RadioButtonList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT [OperatorName] FROM [Operator]"></asp:SqlDataSource>
    <br />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
    <br />
    <br />
    <br />
    <asp:DataList ID="DataList1" runat="server" CellPadding="4" DataSourceID="SqlDataSource2" ForeColor="#333333" RepeatColumns="3">
        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <ItemTemplate>
            OperatorID:
            <asp:Label ID="OperatorIDLabel" runat="server" Text='<%# Eval("OperatorID") %>' />
            <br />
            Adress:
            <asp:Label ID="AdressLabel" runat="server" Text='<%# Eval("Adress") %>' />
            <br />
            City:
            <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
            <br />
            LastName:
            <asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
            <br />
            FirstName:
            <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
            <br />
            <br />
        </ItemTemplate>
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT [OperatorID], [Adress], [City], [LastName], [FirstName] FROM [main] WHERE ([OperatorID] = @OperatorID) ORDER BY [FirstName] DESC, [LastName] DESC">
        <SelectParameters>
            <asp:ControlParameter ControlID="RadioButtonList1" Name="OperatorID" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <br />
    <br />

</div>
</form>

`

任务听起来像:对于Button,以编程方式设置检查事件 RadioButtonList 中的用户选择并构建适当的 表DataList

【问题讨论】:

标签: asp.net button datalist radiobuttonlist


【解决方案1】:

您可以使用以下代码:

aspx 页面


<form id="form1" runat="server">
<div>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" >
    </asp:RadioButtonList>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Operator selection" 
        onclick="Button1_Click" />
    <br />
    <asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
    <%# Eval("Name") %>
     <%# Eval("Login") %>
     <%# Eval("City") %>

    </ItemTemplate>
    </asp:DataList>
</div>
</form>

aspx.cs 页面


公共部分类_默认:System.Web.UI.Page

{

static SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        con = new SqlConnection(@"Data Source=yourservername;Initial   
                   Catalog=yourdatabase;Integrated Security=True");

        if(con.State.ToString()=="closed")
            con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter ad1 = new SqlDataAdapter();
        ad1.SelectCommand = new SqlCommand("select * from operator", con);
        ad1.Fill(ds);
        con.Close();
        RadioButtonList1.DataSource = ds.Tables[0];
        RadioButtonList1.DataTextField = "OparatorName";
        RadioButtonList1.DataValueField = "Operator_id";
        RadioButtonList1.DataBind();
        con.Close();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string operator_id = "";
    for (int i = 0; i < RadioButtonList1.Items.Count; i++)
    {

        if (RadioButtonList1.Items[i].Selected)
            operator_id += RadioButtonList1.Items[i].Value+",";
    }

    if (con.State.ToString() == "closed")
        con.Open();
    DataSet ds1 = new DataSet();
    SqlDataAdapter ad1 = new SqlDataAdapter();
    ad1.SelectCommand = new SqlCommand("select * from student where Operator_id IN ("+operator_id.TrimEnd(',')+")", con);
    ad1.Fill(ds1);
    con.Close();
    DataList1.DataSource = ds1.Tables[0];

    DataList1.DataBind();
    con.Close();
}

}

【讨论】:

  • 感谢 Gaurav Jain,但在 Default.aspx.cs 我有
    使用 System;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;公共部分类 _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click1(object sender, EventArgs e) { }
  • 用上面的 page_load 方法替换你的页面加载,用 button_click 替换 button1_click 并使用 system.Data 将以下命名空间添加到顶部;使用 system.Data.sqlClient;
  • 非常感谢您的帮助。效果很好!但我需要以另一种方式执行此任务。就像我在顶部写的那样:任务听起来像:对于Button,以编程方式设置检查RadioButtonList 中的用户选择并构造适当的表DataList 的事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多