gridview数据绑定问题

时间:2014-03-12 19:03:33

标签: c# asp.net gridview data-binding

试图弄清楚为什么我在使用GridView1进行数据绑定时遇到问题

HTML:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        CellPadding="10" RowStyle-VerticalAlign="Top"
        BackColor="White" BorderColor="Black" BorderWidth="1px"  OnRowCreated="GridView1_RowDataBound"
        Width="100%" AllowPaging="true" BorderStyle="Solid" PagerSettings-Position="TopAndBottom">
        <Columns>

            <asp:TemplateField HeaderText="Last" >
                <ItemStyle VerticalAlign="Top" />
                <ItemTemplate>
                    <%#Eval("Last")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="First" >
                <ItemStyle VerticalAlign="Top" />
                <ItemTemplate>
                    <%#Eval("First")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Supervisor ID" >
                <ItemStyle VerticalAlign="Top" />
                <ItemTemplate>
                    <%#Eval("supervisorId")%>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

代码隐藏:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // EdT:  if there is no value in session["userId"], redirect user to login page
            if ((HttpContext.Current.Session["userId"]) == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                userName = SqlHelperClass.GetUserName((int)HttpContext.Current.Session["userId"]);
                timeCard = new TimeCard((int)HttpContext.Current.Session["userId"], GetCurrentPayPeriod());

                // EdT: Set value of literal
                Literal1.Text = userName;

                // EdT: Set default values for SelectParameters
                string userId = HttpContext.Current.Session["userId"].ToString();
                string pped = Convert.ToString(GetCurrentPayPeriod());
                SqlDataSource1.SelectParameters["user"].DefaultValue = userId;
                SqlDataSource1.SelectParameters["pped"].DefaultValue = pped;
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // set value of dropdown list in GridView Row to the value contained in the timecard DataTable
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string dropDownListValue = CurrentTimeCard.TimeCardDataTable.Columns["projectName"].ToString();
            DropDownList dropDownList = (DropDownList)e.Row.FindControl("DropDownList1");
            dropDownList.DataSource = SqlDataSource2;
            dropDownList.SelectedValue = dropDownListValue;
        }
    }

SqlDataSource1标记:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MinnsTimeDatabase %>" 
        SelectCommand="spGetTimeCard" SelectCommandType="StoredProcedure" >
        <SelectParameters>
            <asp:Parameter Name="user" Type="Int32" />
            <asp:Parameter DbType="Date" Name="pped" />
        </SelectParameters>
    </asp:SqlDataSource>

赞赏任何想法

1 个答案:

答案 0 :(得分:0)

您还没有提到您的实际问题,但我看到的一个问题是,当您使用DataSourceID属性(因此是数据源控件)时,您不需要在GridView上调用DataBind() )。

所以我肯定会删除对{_ 1}}的调用,这是您的Page_Load函数的GridView1.DataBind();块。由于您正在RowDataBound事件中进行处理,因此在页面生命周期中多次GridView数据绑定很可能会导致问题。