IPostBackEventHandler前端自定义控件触发服务端事件

原理:使用 RaisePostBackEvent 方法捕获回发,并在服务器上引发 Click 事件。

需求:通过asp:GridView渲染的列表,能够点击某一行数据,获取当行的变量参数,以此触发后端方法。

前端代码:

<asp:GridView ID="gridView" RowStyle-CssClass="login-list" runat="server" Width="100%" CellPadding="3" AutoGenerateColumns="False" ShowHeader="false" BorderWidth="0px" OnRowDataBound="gridView_RowDataBound">
    <Columns>
        <asp:TemplateField >
            <ItemTemplate>
                <asp:Label runat="server" ID="lbUserId" Text='<%# "用户ID:"+Eval("userid") %>'></asp:Label>
                <asp:Label runat="server" ID="lbUserName" Text='<%# Eval("username")+"("+Eval("mobile")+")" %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
            <ItemTemplate>
                <asp:Image ID="img" ImageUrl="/assets/weixin/images/arr-right.png" CssClass="right-arr" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
                                        
</asp:GridView>

后端代码

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class wap_WxLogin : System.Web.UI.Page, IPostBackEventHandler
{
    ///......

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onClick"] = this.Page.ClientScript.GetPostBackEventReference(this, "ClickFlag:" + e.Row.RowIndex.ToString());

            Label lbUserName = (Label)e.Row.Cells[0].FindControl("lbUserName");
            string username = "" + DataBinder.Eval(e.Row.DataItem, "username");
            string mobile = "" + DataBinder.Eval(e.Row.DataItem, "mobile");
            mobile = MyFunctions.MobileNubSafety(mobile);
            lbUserName.Text = username + "(" + mobile + ")";
        }
    }
    /// <summary>
    /// 客户端点击行回传行号,从而定位点击哪一行
    /// </summary>
    /// <param name="eventArgument"></param>
    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument.StartsWith("ClickFlag:"))
        {
            int rowIndex = 0;
            int.TryParse(eventArgument.Substring(10), out rowIndex);
            try
            {
                GridViewRow row = gridView.Rows[rowIndex];

                Label lbUserId = (Label)row.Cells[0].FindControl("lbUserId");
                string UserId = "" + lbUserId.Text;
                UserId = UserId.Replace("用户ID:", "");
                string openId = "" + HttpContext.Current.Session.Contents["openid"];
                LoginByOpenId(openId, UserId);
            }
            catch (Exception ex)
            {
                MyFunctions.WriteError(ex);
                MyFunctions.ShowErrorByLayer("登录失败!");
                return;
            }

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值