大家好, 我的网格视图如下:
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中绑定
发布评论