如何在asp.net中的页面加载时将数据源设置为gridview中的dropdownlist [英] how to set datasource to dropdownlist in gridview at page load in asp.net

查看:84
本文介绍了如何在asp.net中的页面加载时将数据源设置为gridview中的dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2列Sno和Deptname的网格
并在deptname列中有dropdownlist.
如何在Page_Load
中将数据源提供给该下拉列表
我尝试过这样,但它给出了错误

源代码:

i am have a grid with 2 column Sno and Deptname
and in the deptname column i am having dropdownlist.
how to give datasource to that dropdownlist in Page_Load

i tried like this but it gives error

source code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
           <Columns>
               <asp:TemplateField HeaderText="Sno">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text='<%# Bind("Sno") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="DeptName" >
                   <HeaderTemplate >
                   <asp:DropDownList ID="dd"  runat="server" AutoPostBack="true"

                           DataTextField="DeptName" AppendDataBoundItems="true"

                           onselectedindexchanged="dd_SelectedIndexChanged" >
                   <asp:ListItem>Select</asp:ListItem>
                   </asp:DropDownList>

                   </HeaderTemplate>
                   <ItemTemplate >
                       <asp:Label ID="Label1" runat="server" Text='<%# Bind("DeptName") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>




cs代码:




cs code:

protected void Page_Load(object sender, EventArgs e)
   {
            con = new SqlConnection(@"Data Source=PC026328\SQLEXPRESS;Initial Catalog=gan;Integrated Security=True");
          SqlDataAdapter da = new SqlDataAdapter("select deptname from department", con);
          DataSet ds = new DataSet();
          da.Fill(ds);
//error line
<dropdownlist ddl="(DropDownList)gridView1.HeaderRow.FindControl("dd");">
ddl.DataSource = ds.Tables[0];
 }</dropdownlist>


帮帮我.. !!


help me..!

推荐答案

我通常对GridView/ListView中的此类嵌套ddls进行内联绑定.
I usually do inline binding for such nested ddls inside GridView/ListView.
<asp:GridView ID="gv1" runat="server" GridLines="None">
<Columns>
.....
<asp:TemplateField>
<li>
    <asp:DropDownList ID="ddl1" runat="server" SelectedValue='<%#Bind("mg1") %>' DataTextField="p_option" DataValueField="p_right_id" DataSourceID="SqlDataSource1">
    </asp:DropDownList>
</li>
</asp:TemplateField>
....
....
</GridView>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


ConnectionStrings:ConnectionString2 %> " span> SelectCommand =" My_SP" SelectCommandType StoredProcedure" < /asp:SqlDataSource >
ConnectionStrings:ConnectionString2 %>" SelectCommand="My_SP" SelectCommandType="StoredProcedure"> </asp:SqlDataSource>


如何将数据提供给尚未创建的DropDown列表,
GridView内部的DropDownList将在事件RowCreated触发后创建,因为RowCreated事件在Page_Load 事件触发后触发,您不能在Page_Load()
中执行此操作 您必须在
中执行此操作
How can you give the data to DropDown list as its not yet created,
The DropDownList inside the GridView will be created after the Event RowCreated fires,as RowCreated Event fires after the Page_Load event,you cant do this in Page_Load(),
You have to do this in
public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{}


这篇关于如何在asp.net中的页面加载时将数据源设置为gridview中的dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆