下拉列表未在GridView中绑定

编程入门 行业动态 更新时间:2024-10-26 06:29:14
本文介绍了下拉列表未在GridView中绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我的网格视图如下:

Hello everyone, I have gridview as follows:

<asp:GridView ID="role_update" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="role_update_RowCancelingEdit" OnRowEditing="role_update_RowEditing" OnRowUpdating="role_update_RowUpdating" CssClass="GridView"> <Columns> <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblname" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Code" HeaderStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblcode" runat="server" Text='<%# Bind("code") %>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Role" HeaderStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="role_label" runat="server" Text='<%# Bind("role") %>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" /> <EditItemTemplate> <asp:DropDownList runat="server" ID="role_list" DataTextField="rolename" DataValueField="role" > </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Action" ShowEditButton="True" ButtonType="Button"> <ControlStyle CssClass="btn" /> <ItemStyle HorizontalAlign="Center" /> </asp:CommandField> </Columns> </asp:GridView>

我想这样,当我点击编辑时,角色列下拉列表应该是当时绑定了,所以我在行编辑活动中尝试如下:

I want such that,when i click on edit,the role column dropdown list should be binded at that time,so i tried that in row editing event as follows:

protected void role_update_RowEditing(object sender, GridViewEditEventArgs e) { role_update.EditIndex = e.NewEditIndex; GridViewRow row = role_update.Rows[e.NewEditIndex]; DropDownList dl = (DropDownList)row.FindControl("role_list"); SqlConnection conn = new SqlConnection(); conn.ConnectionString= System.Configuration.ConfigurationManager.ConnectionStrings["xyz"].ConnectionString; conn.Open(); SqlCommand cmd = new SqlCommand("select DISTINCT role from table", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); dl.DataSource = ds; dl.DataBind(); conn.Close(); rolebind(); }

问题是,我在上面突出显示的行中得到对象引用错误,这意味着我得到null下拉,因为item_mplate中没有role_list ,它在edititemtemplate。 我的问题是如何在编辑时绑定下拉菜单? 我googled很多,但每个例子都显示在rowdatabound事件中的绑定,这对我来说是无用的(按照我的想法)。如果没有,那么如何实现呢? 任何帮助我会非常感激。

The problem is,i am getting object reference error at the line highlighted above,it means i am getting null dropdown as role_list is not there in itemtemplate,its in edititemtemplate. My question is how to bind dropdown at time of editing? I googled alot but every example shows binding in rowdatabound event,thats useless for me(as per as i think).If not,then how to accomplish that? Any help would be really appreciated.

推荐答案

您正在使用错误的事件来绑定您的gridview。使用RowDataBound事件进行绑定。例如: You are using wrong event to bind your gridview. Use RowDataBound event to bind. For example: protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e) { Control dl = e.Row.FindControl("yourDropDownList"); if (dl != null) { SqlConnection conn = new SqlConnection(); conn.ConnectionString= System.Configuration.ConfigurationManager.ConnectionStrings["xyz"].ConnectionString; conn.Open(); SqlCommand cmd = new SqlCommand("select DISTINCT role from table", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); dl.DataSource = ds; dl.DataBind(); conn.Close(); rolebind(); } }

更多推荐

下拉列表未在GridView中绑定

本文发布于:2023-10-12 23:13:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   列表   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!