我必须在DropDown选择值更改上更改Gridview详细信息。这是我的代码,但它不起作用。它没有在GridView中显示数据。当我更改DropDownList的索引时,GridView为空。
index.aspx.cs代码:
protected void DropDownListDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "SELECT * FROM Doctor WHERE Doc_Id=" + DropDownListDoctor.SelectedValue + "";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(dt);
GridViewDoctorDetail.DataSource = dt;
}
index.aspx代码:
<form id="form1" runat="server">
<div>
<label>Select Doctor Name</label>
<br />
<asp:DropDownList ID="DropDownListDoctor" runat="server" AutoPostBack="True"
DataSourceID="Doctor_DataSource" DataTextField="DocName"
DataValueField="Doc_Id"
onselectedindexchanged="DropDownListDoctor_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="Doctor_DataSource" runat="server"
ConnectionString="Data Source=IM-82B70624D72D;Initial Catalog=AppointmentScheduler;Persist Security Info=True;User ID=sa;Password=za3452432760za"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [Doc_Id], [DocName] FROM [Doctor]">
</asp:SqlDataSource>
<br />
<label>Doctor Detail</label>
<br />
<asp:GridView ID="GridViewDoctorDetail" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical" Width="339px"
AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<asp:SqlDataSource ID="DoctorDetail_SqlDataSource" runat="server"
ConnectionString="Data Source=IM-82B70624D72D;Initial Catalog=AppointmentScheduler;Persist Security Info=True;User ID=sa;Password=za3452432760za"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Doctor] WHERE ([Doc_Id] = @Doc_Id)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListDoctor" DefaultValue="1"
Name="Doc_Id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
答案 0 :(得分:1)
您需要添加
GridViewDoctorDetail.DataBind();
到您的后面的代码。当回发发生时,gridview会丢失其先前的内容,如果没有,它就没有任何绑定到它的数据。
protected void DropDownListDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "SELECT * FROM Doctor WHERE Doc_Id=" + DropDownListDoctor.SelectedValue + "";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(dt);
GridViewDoctorDetail.DataSource = dt;
GridViewDoctorDetail.DataBind();
}