【问题标题】:How to bind datalist inside gridview?如何在gridview中绑定datalist?
【发布时间】:2013-12-30 14:12:36
【问题描述】:

我想在gridview控件中绑定datalist

在下图中 Step-1,Step-2,... 来自数据库,我想将其绑定到 datalist 中

我试过下面的代码

.aspx

 <asp:GridView ID="gvRoadMap" runat="server" AutoGenerateColumns="false" Width="100%"
                    Style="border: 0px solid #cdcdcd" OnRowDataBound="gvRoadMap_RowDataBound" border="0"
                    CellSpacing="1" CellPadding="3" AllowSorting="true">
                    <Columns>
                          <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:DataList ID="dlRMStepHeader" runat="server" RepeatDirection="Horizontal">
                                    <ItemTemplate>
                                        <asp:Label ID="lblStepName" Text='<%#Eval("STEPNAME") %>' runat="server"></asp:Label>
                                    </ItemTemplate>
                                </asp:DataList>
                            </HeaderTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:DataList ID="dlRMStepItem" OnItemDataBound="dlRMStepItem_ItemDataBound" runat="server">

                            </asp:DataList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            objUserInformation = this.Session["USERSECURITYINFO"] as UserSecurityInformation;
            //Presentationlayer_Views_AddRoadMap.USERID = Convert.ToInt64(BusinessEngineFacade.GetBusinessEngineFacade().get_GetSessionUserInformation().UserId);
            //this.hdnRoadMapID.Value = "0";
            if (base.Request.QueryString["ID"] != null)
            {
                trainingMapID = Convert.ToString(base.Request.QueryString["ID"]);
            }
            this.GetRoadMapData();
        }
    }

    private void GetRoadMapData()
    {
        RoadMapManager roadmapMgr = new RoadMapManager();
        DataSet dataSet = roadmapMgr.GetAllRaodMapData(Convert.ToInt64(trainingMapID));
        this.ViewState["RoadMapData"] = dataSet;
        if (dataSet.Tables.Count > 0)
        {
            if (trainingMapID != null)
            {
                this.txtRoadmapName.Text = Convert.ToString(dataSet.Tables[3].Rows[0][0]);
                this.txtDescription.Text = Convert.ToString(dataSet.Tables[3].Rows[0][1]);
                this.chkActive.Checked = Convert.ToBoolean(dataSet.Tables[3].Rows[0][2]);
            }
            else
            {
                this.txtRoadmapName.Text = "";
                this.txtDescription.Text = "";
                this.chkActive.Checked = false;
            }
            this.BindRoadMap(dataSet.Tables[0]);
        }
    }
    private void BindRoadMap(DataTable dt)
    {
        this.gvRoadMap.DataSource = dt;
        this.gvRoadMap.DataBind();
    }

    protected void gvRoadMap_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dataSource = new DataTable();
        DataList dataList = new DataList();
        if (e.Row.RowType == DataControlRowType.Header)
        {
            dataList = (DataList)e.Row.FindControl("dlRMStepHeader");
            dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
            dataList.DataSource = dataSource;
            dataList.DataBind();
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dataList = (DataList)e.Row.FindControl("dlRMStepItem");
            dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
            dataList.DataSource = dataSource;
            dataList.DataBind();
        }
    }



 ![enter image description here][1]

如何在asp.net中设计上图?

有什么想法吗?提前致谢。

【问题讨论】:

  • 您面临的具体问题是什么?不是在编译吗?是否引发异常?它没有给出预期的结果吗?通过提供有关您的问题的更多信息来帮助我们为您提供帮助...
  • dataList = (DataList)e.Row.FindControl("dlRMStepItem");在这里我收到一个错误“对象引用未设置实例”
  • 我有 C# 代码,但我不确定如何根据给定的代码进行设计

标签: c# asp.net gridview datalist


【解决方案1】:

您收到“对象引用未设置实例”错误(您在 cmets 中提到),因为您尝试获取的 DataList 仅存在于您的 EditItemTemplate 中: p>

<EditItemTemplate>            
    <asp:DataList ID="dlRMStepItem" OnItemDataBound="dlRMStepItem_ItemDataBound" runat="server">
    </asp:DataList>
</EditItemTemplate>

在尝试访问该控件之前,您需要更新 if 条件以确保 GridView 处于编辑模式:

if (e.Row.RowType == DataControlRowType.DataRow && gvRoadMap.EditIndex >= 0)
{
    dataList = (DataList)e.Row.FindControl("dlRMStepItem");
    dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
    dataList.DataSource = dataSource;
    dataList.DataBind();
}

对于 OnItemDataBound 事件未触发的问题,请尝试在 if 块中连接事件处理程序(我知道它已经在您的标记中,但只是为了确定):

if (e.Row.RowType == DataControlRowType.DataRow && gvRoadMap.EditIndex >= 0)
{
    // Bind the event
    dataList.ItemDataBound += dlRMStepItem_ItemDataBound;

    dataList = (DataList)e.Row.FindControl("dlRMStepItem");
    dataSource = ((DataSet)this.ViewState["RoadMapData"]).Tables[1];
    dataList.DataSource = dataSource;
    dataList.DataBind();
}

【讨论】:

  • 感谢您的快速响应,但 OnItemDataBound 未触发
  • @user2148679 很高兴我能帮上忙!关于 OnItemDataBound 的事情......你是说,现在你已经过去了另一个错误,“dlRMStepItem”DataList 的 OnItemDataBound 事件没有触发?
  • @user2148679 我更新了我的答案。让我知道是否可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多