【问题标题】:gridview databinding problemsgridview 数据绑定问题
【发布时间】:2014-03-12 19:03:33
【问题描述】:

试图弄清楚为什么我在将 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>

任何想法表示赞赏

【问题讨论】:

  • SqlDataSource1 的标记在哪里?
  • 在你的 asp:gridview 中你引用了 sqldatasource1 但我看不到你在哪里用数据库中的数据填充它。我想这不是问题,但在较低的函数 dropdownlist.datasource = "a string ?" 中,您的意思可能是 DataSourceID,或者您可以删除引号。
  • 在原始帖子的编辑中发布了数据源标记

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


【解决方案1】:

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

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

【讨论】:

  • 已删除并感谢您,但不行。问题是 GridView 根本不是 DataBinding,所以我在 RowDataBound 中的任何代码都没有运行
  • @deadEddie 啊,感谢您的澄清。我想到了另外两件事: 1. 我注意到您正在处理 RowCreated(尽管您将方法称为“RowDataBound”)。不一定是问题,只是想我会指出 2。也许您的参数之一为空?尝试在 SqlDataSource 标记中将 CancelSelectOnNullParameter 设置为 false。
  • 谢谢。我发现并纠正了 RowCreated 问题。将 CancelSelectOnNullParameter 设置为 false。不确定这是否正确,但我在 Page_Load 事件中设置了默认值。
  • @deadEddie 我在您显示的代码中看不到任何地方,您将值分配给 SqlDataSource 的参数。
  • 我只是将它们分配为默认值。代码隐藏已更新。
猜你喜欢
  • 2013-12-27
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 2017-07-20
相关资源
最近更新 更多