• 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);");
            }
        }

  • 相关阅读:
    IntelliJ IDEA 最新注册码
    tidyverse|数据分析常规操作-分组汇总(sumamrise+group_by)
    ComplexHeatmap|根据excel表绘制突变景观图(oncoplot)
    Tidyverse| XX_join :多个数据表(文件)之间的各种连接
    LDheatmap | SNP连锁不平衡图(LD)可视化,自己数据实现版!
    Tidyverse|数据列的分分合合,爱恨情仇
    R-ggpmisc|回归曲线添加回归方程,R2,方差表,香不香?
    R-rbind.fill|列数不一致的多个数据集“智能”合并,Get!
    R|tableone 快速绘制文章“表一”-基线特征三线表
    R|生存分析
  • 原文地址:https://www.cnblogs.com/jishu/p/1940095.html
Copyright © 2020-2023  润新知