[英]Refresh data in GridView on DropDownList selected index change
[英]I have to change GridView Data on Dropdown Selected Index Change
我必须在DropDown Selected Value Change上更改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>
您需要添加
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.