现在的位置: 首页 > 综合 > 正文

C# GridView 控件绑定下拉列表框及给下拉列表框设定默认值

2013年08月17日 ⁄ 综合 ⁄ 共 4044字 ⁄ 字号 评论关闭

aspx文件内容

 <form id="form1" runat="server" onsubmit="">
        <div style="margin: 100px auto; width: 80%; text-align: left">
           
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowUpdating"
                OnRowUpdated="GridView1_RowUpdated" AutoGenerateColumns="False" DataKeyNames="pkID"
                DataSourceID="SqlDataSource1" PageSize="200" Width="100%" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
                <Columns>
                    <asp:BoundField DataField="pkID" HeaderText="pkID" InsertVisible="False" ReadOnly="True"
                        SortExpression="pkID" />
                        <asp:BoundField DataField="infoType" HeaderText="infoType" InsertVisible="False" ReadOnly="True"
                        SortExpression="infoType" />
                   <asp:BoundField DataField="title" HeaderText="标题" SortExpression="title" /><asp:BoundField DataField="webType" HeaderText="网站类型" SortExpression="webType" /><asp:BoundField DataField="remarkType" HeaderText="正负面评价" SortExpression="remarkType" /><asp:BoundField DataField="score" HeaderText="正负面评分" SortExpression="score" /><asp:BoundField DataField="pubTime" HeaderText="日期开始" SortExpression="pubTime" /><asp:BoundField DataField="endTime" HeaderText="日期结束" SortExpression="endTime" /><asp:BoundField DataField="replycount" HeaderText="相关文章数" SortExpression="replycount" />
                    
                    <asp:BoundField DataField="link" HeaderText="链接" SortExpression="link" />
                     <asp:BoundField DataField="editor" HeaderText="编辑者" SortExpression="editor" />
                    <asp:TemplateField HeaderText="分类"  >
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlInfoType" runat="server" DataSourceID="SqlDataSource1" DataTextField="t_name"
                                DataValueField="pkid"   >
                            </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                SelectCommand="SELECT [t_name], [pkid] FROM [t_info_type]"></asp:SqlDataSource>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                </Columns>
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                DeleteCommand="" InsertCommand="" SelectCommand="" UpdateCommand="">
                <DeleteParameters>
                    <asp:Parameter Name="pkID" Type="Int32" />
                </DeleteParameters>               
            </asp:SqlDataSource>
        </div>
    </form>

 

.cs文件代码

//红色字体部分用来设定下拉列表框默认值

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
            int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["infoType"]);
            int Pkid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["pkID"]);
            string strLink = Convert.ToString(((DataRowView)e.Row.DataItem)["link"]);
            ((DataRowView)e.Row.DataItem).Row["title"] = "dddddd";
            e.Row.Cells[2].Text =" <a href='http://blog.my400800.cn' target=/"_blank/">"+ e.Row.Cells[2].Text+"</a>";

            DropDownList dd = (DropDownList)e.Row.FindControl("ddlInfoType");

            // DataTable table = dd.DataSource as DataTable;
            dd.SelectedValue = rid.ToString();

            dd.Attributes.Add("onchange", "ajaxUpdateInfoType('" + Pkid + "',this);");
        }
    }

抱歉!评论已关闭.