在用户控件代码隐藏通过无法访问的GridView事件(Can't access GridVi

2019-08-05 20:27发布

IpInterfaceUC用户控件

<div id="dvChannel" runat="server">
        <asp:GridView ID="gvChannelUC" 
            OnRowCommand="gvChannelUC_RowCommand"
            OnSelectedIndexChanged="gvChannel_SelectedIndexChanged"
        />
</div>

IPServices页面代码隐藏

if (!IsPostBack){
}else
{    
 string str_btn = Request.Form.Keys[Request.Form.Keys.Count - 1].ToString();
 handleClick(str_btn);
}

它总是显示str_btn是null.If我点击按钮,就会显示按钮的id.But当我点击GridView的在选择,这表明str_btn是null.It应显示GridView的ID,当我们单击选择。

感谢您的任何解释。

Answer 1:

先给name属性你的GridView,即name="yourGridName"

<div id="dvChannel" runat="server" name="yourGridName">
        <asp:GridView ID="gvChannelUC" name="yourGridName"
            OnRowCommand="gvChannelUC_RowCommand"
            OnSelectedIndexChanged="gvChannel_SelectedIndexChanged"
        />
</div>


Answer 2:

按我的理解,你需要在页面/代码隐藏(的.aspx)电网事件移交(的.ascx)。

在用户控件声明事件处理程序

 public event EventHandler<EventArgs>  RaiseSelectedIndexChanged=delegate {};

处理用户控件的SelectedIndexChanged在userControl.cs事件

   protected void gvChannel_SelectedIndexChanged(object sender, EventArgs e)
    {
        var raiseSelectedIndexChanged = RaiseSelectedIndexChanged ;
        if(raiseSelectedIndexChanged!=null)
        {
            raiseSelectedIndexChanged(sender, e);
        }
    }

注册并在ASPX使用您的用户控件(我希望你已经这样做)这个代码将在aspx页面去

        <uc:userControl OnRaiseSelectedIndexChanged="OnRaiseSelectedIndexChanged"/>

处理在ASPX代码事件背后

    protected void OnRaiseSelectedIndexChanged(object sender, EventArgs e)
    {
       //handle your event and put logic 
    }

我希望我说清楚,让我知道,如果它混淆了你。



文章来源: Can't access GridView event at usercontrol by codebehind