• GridView里面模板获取唯一值(如ID)


    我的习惯一般都是用BUTTONFIELD或者COMMANDFIELD,用rowcommand事件,或者selectindexchange事件

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
           if (e.commandName = "....")
               {
                int i = Convert.toint32(e.CommandArgument.ToString()); //获取行号
                LinkButton lbt1 = ((LinkButton)GridView1.Rows.Cells[0].FindControl("LinkButton1"));
                string id = ((Label)GridView1.Rows.Cells[0].FindControl("ID")).Text;//ID绑定在一个Label上
               }
    }
    或者用COMMANDFIELD里面的选择
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                LinkButton lbt1 = ((LinkButton)GridView1.SelectedRow.Cells[0].FindControl("LinkButton1")); //SelectedRow获取行号,选择才能用
                string id = ((Label)GridView1.SelectedRow.Cells[0].FindControl("ID")).Text;//ID绑定在一个Label上
            }
    GV在布局里面用的话只能用<asp:TemplateField>里面的<Template>然后就在里面绑定数据
    .aspx:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None"
                                BorderWidth="0px"
                                onrowcommand="GridView1_RowCommand" AllowPaging="True"
                onpageindexchanged="GridView1_PageIndexChanged" PageSize="6">
            <Columns>
                <asp:TemplateField>
                <ItemTemplate>
                <div class="mrt2">
       <ul>
       <li><p class="w542px2"><span>·</span>
                <asp:Label ID="ID" runat="server" Text='<%# Bind("Id") %>' Visible="false"></asp:Label>
                <asp:Label ID="Title_lbe" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                <asp:Label ID="Class_lbe" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </p>
                <p class="w135px"><span class="caozuo marglt24px caozuobg1"><span><a href="Article_mod_up.aspx?id=<%# Eval("Id") %>">编辑</a></span></span>
                <span class="caozuo caozuobg2"><span><a href="#">
                    <asp:LinkButton ID="LinkButton1"
                    runat="server" CommandName="del" CommandArgument='<%# Eval("Id") %>' >删除</asp:LinkButton></a></span></span></p></li>
                </ul>
          </div>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>

    .CS:
    protected void GridView1_RowCommand(object sender, CommandEventArgs e)
            {
                if (e.CommandName == "del")
                {
                    string id = e.CommandArgument.ToString();
                }
            }
    简短有力,和传统的获取行号法很像,但是快多了,区别在于一个是获取GV内置控件的属性,一个是自己给CommandArgument赋值
  • 相关阅读:
    PostgreSQL杂记页
    程序员给女朋友周年纪念的浪漫
    实现乐鑫esp8266的无线OTA升级,实现远程在线升级固件
    XAOP的使用示范例子
    XPage的使用示范例子
    Python手势识别与控制
    Python人体肤色检测
    基于Opencv自带BP网络的车标简易识别
    iOS 自定义转场动画
    Python实时语音识别控制
  • 原文地址:https://www.cnblogs.com/405464904/p/1533934.html
Copyright © 2020-2023  润新知