zoukankan      html  css  js  c++  java
  • C# GridView 控件绑定下拉列表框及给下拉列表框设定默认值

    aspx文件内容

     <form id="form1" runat="server" onsubmit="">
            <div style="margin: 100px auto; 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);");
            }
        }

  • 相关阅读:
    基础实验7-2.2 插入排序还是堆排序 (25分)
    进阶实验6-3.1 红色警报 (25分)--并查集
    基础实验3-2.2 单链表分段逆转 (25分)--单链表
    基础实验6-2.2 汉密尔顿回路 (25分)--邻接矩阵
    案例6-1.3 哥尼斯堡的“七桥问题” (25分)--dfs图连通+度判断
    基础实验8-1.1 单身狗 (25分)
    基础实验7-2.3 德才论 (25分)--排序
    基础实验7-2.4 PAT排名汇总 (25分)--结构体排序(快排)
    进阶实验4-3.4 笛卡尔树 (25分)--二叉排序树+堆排序
    基础实验4-2.7 修理牧场 (25分)-堆+哈夫曼树
  • 原文地址:https://www.cnblogs.com/jishu/p/1940095.html
Copyright © 2011-2022 走看看