通过触发下拉列表选择的索引更改事件来更改网格视图数据源

时间:2013-09-12 04:29:09

标签: asp.net sql gridview

我正在设计一个基于网络的物流系统。在我的视图订单页面中,我有一些下拉列表,这些列表绑定到数据源和旁边的网格视图。我想在操作员选择相关下拉列表时更改网格视图数据源。我写了代码,但它没有工作,网格视图不显示任何数据。请告诉我我做错了什么。

//Code for firing drop down list event 

        public void ddlVehicleFire(Object sender, EventArgs e)
        {
        GridView1.DataSource = VehicleDastaSource;
        GridView1.DataBind();
        }

// aspx page

<asp:SqlDataSource ID="VehicleDastaSource" runat="server" ConnectionString="<%$ ConnectionStrings:LGDB %>" SelectCommand="SELECT [vehicleID], [manufacturer], [VIN], [capacity], [weight_], [dateOfProduction] FROM [vehicle]"></asp:SqlDataSource>

 Drop down list
   asp:DropDownList ID="DDLVehicle" runat="server" DataSourceID="vehicle"     DataTextField="vehicleID" DataValueField="vehicleID" AutoPostBack="True" OnSelectedIndexChanged="ddlVehicleFire"></asp:DropDownList>

by choosing vehicle ID i want to display relevant data inside grid view

1 个答案:

答案 0 :(得分:0)

1.make autopostback true for dropdown

C#

if (IsPostBack)

  {
   //your code here; 
  }

vb.net

If IsPostBack Then
        'your code here;

End If

的.aspx

<Columns>
        <asp:BoundField HeaderText="Id" 
            DataField="Id" ReadOnly="true" />
        <asp:BoundField DataField="CreatedBy" 
            ReadOnly="true" HeaderText="Created By" />
        <asp:BoundField DataField="CreateTime" 
            ReadOnly="true" HeaderText="Date Created"
            DataFormatString="{0:d}" />
    </Columns>