【问题标题】:C# Getting cell value on button click for each row on gridviewC#获取gridview上每一行的按钮单击单元格值
【发布时间】:2015-07-06 10:08:28
【问题描述】:

我有带有按钮的gridview。我希望每行上的按钮能够获取每行网格视图中第一个单元格的值。例如下表:

|标题 1 |标题 2 |标题 3 |
|快乐 |伤心 |按钮 1 |
|担心 |生气 |按钮 2 |
|兴奋 |皱眉 |按钮 3 |

当我按下按钮 1 时,它会显示 Happy。如果我按下按钮 2,它将显示“担心”。等等等等。需要您帮助我如何实现这一目标。

.HTML

<asp:gridview id="grdCreateGroup" runat="server" DataKeyNames="id" AutoGenerateColumns="false" OnRowCommand="grdCreateGroup_RowCommand" OnRowDataBound="grdCreateGroup_RowDataBound">
            <Columns>
                <asp:BoundField DataField="admissionNo" HeaderText="Admission No." />
                <asp:BoundField DataField="fullName" HeaderText="Full Name" />
                <asp:templatefield headertext="">
                    <itemtemplate>
                        <asp:Button ID="btnSendInvite" runat="server" Text="Send Invite" CommandName="sendInvite" /> 
                    </itemtemplate>
                </asp:templatefield>
            </Columns>
        </asp:gridview>

.CS

protected void grdCreateGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "sendInvite")
            {
                account accounta = new account();
                account accountb = new account();
                accountManager accountManager = new accountManager();
                invite invite = new invite();
                inviteManager inviteManager = new inviteManager();
                string emailAddress, admissionNo;
                bool status = true;

                emailAddress = HttpContext.Current.User.Identity.Name;
                accounta = accountManager.getAccInfoByEmailAddress(emailAddress);

                invite.leaderName = accounta.fullName;
                invite.groupNo = accounta.groupNo;
                //invite.recipientEmailAddress = accountb.recipientEmailAddress;

                //status = inviteManager.sendInvite(invite);

                GridViewRow Row = grdCreateGroup.Rows[0];
                Button btnSendInvite = (Button)Row.FindControl("btnSendInvite");

                if (status == true)
                {
                    divMessage.InnerHtml = invite.groupNo.ToString();

                    btnSendInvite.Text = "Invite Sent";
                    btnSendInvite.Enabled = false;
                }
                else
                {
                    divMessage.InnerHtml = "Record not added in database";
                }
            }
        }

【问题讨论】:

    标签: c# asp.net button gridview textbox


    【解决方案1】:

    将 CommandArgument='' 添加到您的按钮,在您后面的代码中,您可以获得引发事件的行。

     <asp:Button ID="btnSendInvite" runat="server" Text="Send Invite" CommandName="sendInvite"  CommandArgument='<%# Container.DataItemIndex %>'/> 
    

    在后面的代码中获取行

    protected void grdCreateGroup_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "sendInvite")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = grdCreateGroup.Rows[index];
    
                      Button b = (Button)row.FindControl("btnSendInvite");
                       b.Text = row.Cells[0].Text;
    
                }
    }
    

    【讨论】:

      【解决方案2】:
      protected void btnSendInvite_OnClick(object sender,EventArgs e)
      {
        Button btn=(Button)sender;
        GridViewRow row=(GridViewRow)btn.NamingContainer;
        string name=row.Cells[1].Text; // here you will get Name 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 1970-01-01
        • 2023-03-17
        相关资源
        最近更新 更多