如何在asp.net中使用单选按钮列表和数据列表按钮



请帮我完成小任务。数据库中有两个表:学生表和操作员表。

学生有以下栏:

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,以编程方式设置检查CCD_ 9中的用户选择及其构建表DataList

您可以使用以下代码:

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


公共分部类_Default: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();
}

}

相关内容

  • 没有找到相关文章

最新更新