第一次GridVIew不在asp.net中的默认DropDown列表上显示数据

时间:2014-02-28 06:28:48

标签: c# asp.net gridview

我不知道为什么它表现得像这样实际我有下拉列表由defualt显示一个名称,在数据库中也是它的数据我也应用于gridview数据绑定但没有富有成果的结果第一次它没有显示数据相同在下拉列表中选择的值,但如果我喜欢更改并选择列表中的其他项目并再次选择在下拉列表中显示的那个值,那么它的工作原因是什么。我在这里发送代码。

aspx.cs

<td>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                    DataSourceID="SqlDataSource1" DataTextField="Class" DataValueField="Class">
                </asp:DropDownList>
            </td>



 <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                    DataSourceID="SqlDataSource10" OnRowCommand="GridView1_RowCommand" EmptyDataText="There are no data records to display.">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" Visible="false" SortExpression="ID" />
                        <asp:BoundField  DataField="ClassID" HeaderText="ClassID" SortExpression="ClassID" />
                        <asp:BoundField  DataField="FilePath" HeaderText="File Name" SortExpression="FilePath" />
                        <asp:ButtonField CommandName="Select" HeaderText="Download TimeTable" 
                            ShowHeader="True" Text="Download" />

                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                    DeleteCommand="DELETE FROM [UploadTabel] WHERE [ID] = @ID"
                    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
                    SelectCommand="SELECT [ID], [ClassID], [FilePath] FROM [UploadTabel] Where ClassID=@Class" >
                    <DeleteParameters>
                        <asp:Parameter Name="ID" Type="Int32" />
                    </DeleteParameters>
                   <SelectParameters>
                        <asp:ControlParameter ControlID="DropDownList1" Name="Class" 
                            PropertyName="SelectedValue" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>

CS

protected void Page_Load(object sender, EventArgs e)
    {

        GridView3.DataBind();
      //  lblSelectedDate.Text = AJAXDatePickerControl1.selectedDate.ToShortDateString();
    }

    protected void Page_PreLoad(object sender, EventArgs e)
    {
        try
        {
          //  GridView1.DataBind();
           GridView3.DataBind();
        }
        catch { }

    }

1 个答案:

答案 0 :(得分:0)

试用此代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            GridView3.DataBind();
            }
          //  lblSelectedDate.Text = AJAXDatePickerControl1.selectedDate.ToShortDateString();
        }