0

Please help me with small task. There are two tables in Database: Students and Operators.

Students has the following columns:

Name
Login
City
Operator_id

Operators has the following columns:

Operator_id
OparatorName
OperatorCode

The RadiobuttonList displays OparatorName

I need to choose OparatorName from RadiobuttonList and when pressing the Button Operator selection to see in Datalist a list of students who have such OparatorName.

Could some one help me how to implement this. Thank you

I have made some code in constructor VisualStudio: and it's look like `

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

`

The task sounds like: For a Button programmatically set the event that checks user selection in RadioButtonList and constructing appropriate Table DataList.

pavlo
  • 460
  • 6
  • 15
  • There are plenty of questions in SO related to master-detail problems. Try master-detail in your searches: http://stackoverflow.com/questions/400051/master-details-view-with-xmldatasources, http://stackoverflow.com/questions/5620142/detailsview-does-not-display, http://stackoverflow.com/questions/4593277/asp-net-mvc-master-detail – jruizaranguren Nov 07 '14 at 10:27

1 Answers1

0

you can use following code:

aspx page


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


public partial class _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();
}

}

Gaurav Jain
  • 444
  • 3
  • 13
  • Thank Gaurav Jain, but in `Default.aspx.cs` I have
    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click1(object sender, EventArgs e) { }
    – pavlo Nov 08 '14 at 10:11
  • replace your page load with above page_load method and button1_click with button_click and add following namepace to top using system.Data;using system.Data.sqlClient; – Gaurav Jain Nov 08 '14 at 10:29
  • Thank you very much for help. It works great! But I need to perform this task another way. Like I wrote in the top : **The task sounds like**: For a `Button` programmatically set the event that checks user selection in `RadioButtonList` and constructs appropriate Table `DataList`. – pavlo Nov 08 '14 at 18:30