组合框selectedindexchanged事件

时间:2017-12-24 22:37:55

标签: c# html asp.net combobox

我正在制作一个项目,其中我被困在ajax工具包组合框的选定索引更改事件中。我想要的是,当我在组合框(studentname)中选择学生名称时,我将使用任何按钮将数据库中的其他列的值输入到文本框中...我已经在c#windows窗体中完成了但是无法完成它在asp.net中。 我的代码是`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Final_Project
{

public partial class Teacher_Mangement_page : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    string teachername;
                protected void Page_Load(object sender, EventArgs e)
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["studentdatastring"].ConnectionString;
                    teachername = "teacher1";//Session["name"].ToString();

                    if (!IsPostBack)
                    {
                        cb();
                        MultiView1.ActiveViewIndex = 0;
                    }
                }

                private void cb()
                {
                            ComboBox1.Items.Clear();
                            ComboBox2.Items.Clear();
                            con.Open();
                            SqlCommand cmd = new SqlCommand();
                            cmd.Connection = con;
                            cmd.CommandText = "select studentname,subject from academictable where teachername='"+teachername+"'";
                            SqlDataReader dr = cmd.ExecuteReader();
                            while (dr.Read())
                            {
                                ComboBox1.Items.Add(dr["studentname"].ToString());
                                ComboBox2.Items.Add(dr["subject"].ToString());
                            }
                            con.Close();
                 }

            protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {

                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "select * from academictable where studentname='" + ComboBox1.Text + "' and subject='"+ComboBox2.Text+"' ";
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        qt1.Text = rd["quiz1"].ToString();
                        qt2.Text = rd["quiz2"].ToString();
                        qt3.Text = rd["quiz3"].ToString();
                        qt4.Text = rd["quiz4"].ToString();
                        ast1.Text = rd["assignment1"].ToString();
                        ast2.Text = rd["assignment2"].ToString();
                        prot.Text = rd["project"].ToString();
                        mt.Text = rd["mid"].ToString();
                        ft.Text = rd["final"].ToString();
                    }
                    rd.Close();
                    con.Close();


            }
            string subject;
            protected void Button3_Click(object sender, EventArgs e)
            {
                MultiView1.ActiveViewIndex = 1;
                subject = ComboBox2.Text;
            }

            protected void Button5_Click(object sender, EventArgs e)
            {
                MultiView1.ActiveViewIndex = 0;
            }
}
}`

我的html代码是`

       <!DOCTYPE html>

       <html>
       <head runat="server">
       <title>Bootstrap demo</title>
        <link href="bootstrap/bootstrap.css" rel="stylesheet" type="text/css" />
        <meta charset="utf-8" />
       <meta name="viewport" content="width=device-width, intial-scale=1" />

<style type="text/css">

    .style3
    {
        text-align: center;
    }

    .style5
    {
        width: 582px;
        text-align: right;
    }

    .style6
    {
        text-align: center;
    }
    .style7
    {
        width: 468px;
        text-align: right;
    }
    h1
    { color:Aqua;
      font-style:italic;
      font-family:Broadway;
        }
     h2
     { color:Highlight;

         }
</style>

 </head>
   <body>
     <form id="form1" runat="server">
      <center><h1>STUDENT PORTAL</h1></center>
     <asp:MultiView ID="MultiView1" runat="server">

    <asp:View ID="select_subject" runat="server">

        <table class="w-100">
            <tr>
                <td class="style6" colspan="2">
                    <h2>SUBJECT SELECTION</h2></td>
            </tr>
            <tr>
                <td class="style7">
                    <h4>Subject:</h4></td>
                <td>
                    <ajaxToolkit:ComboBox ID="ComboBox2" runat="server" 
                        AutoCompleteMode="SuggestAppend" Width="300px">
                    </ajaxToolkit:ComboBox>
                </td>
            </tr>
            <tr>
                <td class="style6" colspan="2">
                    <asp:Button ID="Button2" runat="server" Text="Show Attendance" Width="162px" />
                    <ajaxToolkit:RoundedCornersExtender ID="Button2_RoundedCornersExtender" 
                        runat="server" BehaviorID="Button2_RoundedCornersExtender" 
                        TargetControlID="Button2" />
                    <asp:Button ID="Button3" runat="server" Text="Show Academic Details" 
                        Width="178px" onclick="Button3_Click" /><ajaxToolkit:RoundedCornersExtender ID="RoundedCornersExtender1"
                            runat="server" TargetControlID="Button3"/>
                </td>
            </tr>
        </table>

    </asp:View>
    <asp:View ID="Academic_details" runat="server">

    <table class="w-100">
        <tr>
            <td class="text-center" colspan="2">
               <h2>ACADEMIC DETAILS</h2></td>
        </tr>
        <tr>
            <td class="style5">
                Student Name:</td>
            <td>

                <ajaxToolkit:ComboBox ID="ComboBox1" runat="server" 
                    AutoCompleteMode="SuggestAppend" 
                    Width="300px" AutoPostBack="True">
                </ajaxToolkit:ComboBox>

            </td>
        </tr>
        <tr>
            <td class="style5">
                Quiz 1 Marks:</td>
            <td>
                <asp:TextBox ID="qt1" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Quiz 2 Marks:</td>
            <td>
                <asp:TextBox ID="qt2" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Quiz 3 Marks:</td>
            <td>
                <asp:TextBox ID="qt3" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Quiz 4 Marks:</td>
            <td>
                <asp:TextBox ID="qt4" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Assignment 1 Marks:</td>
            <td>
                <asp:TextBox ID="ast1" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Assignment 2 Marks:</td>
            <td>
                <asp:TextBox ID="ast2" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Presentation/Project Marks:</td>
            <td>
                <asp:TextBox ID="prot" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Sessional Marks:</td>
            <td>
                <asp:TextBox ID="sest" runat="server" ReadOnly="True" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Mid-Term Marks:</td>
            <td>
                <asp:TextBox ID="mt" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                Final-Term Marks:</td>
            <td>
                <asp:TextBox ID="ft" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style5">
                <em style="font-weight: bold; font-style: normal; color: rgb(106, 106, 106); font-family: arial, sans-serif; font-size: small; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
                Grade Point Average</em><span 
                    style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"><span>&nbsp;</span>(</span><wbr 
                    style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><em 
                    style="font-weight: bold; font-style: normal; color: rgb(106, 106, 106); font-family: arial, sans-serif; font-size: small; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">GPA</em><span 
                    style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">)<span>&nbsp;:</span></span></wbr>
            </td>
            <td>
                <asp:TextBox ID="gpat" runat="server" ReadOnly="True" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3" colspan="2">
                <asp:Button ID="Button4" runat="server" Text="Button" Width="162px" />
                <ajaxToolkit:RoundedCornersExtender ID="Button4_RoundedCornersExtender" 
                    runat="server" BehaviorID="Button1_RoundedCornersExtender" 
                    TargetControlID="Button4" />
                <asp:Button ID="Button5" runat="server" Text="Back" Width="162px" 
                    onclick="Button5_Click" />
                <ajaxToolkit:RoundedCornersExtender ID="Button5_RoundedCornersExtender" 
                    runat="server" BehaviorID="Button5_RoundedCornersExtender" 
                    TargetControlID="Button5" />
            </td>
        </tr>
    </table>
    </asp:View>
</asp:MultiView>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>

`

0 个答案:

没有答案