在 PageLoad 中访问 CommandEventArgs

发布于 2024-08-06 21:52:50 字数 1416 浏览 10 评论 0原文

我正在使用 c#.net

我在视图 (MultiView) 中有一个转发器,每行都包含一个“编辑”按钮(存储在 CommandArgument 上的是类型 ID)。

<asp:Button ID="buttonClick" OnCommand="ButtonClick" CommandArgument='<%#Eval("ID")%>' runat="server" Text="Edit"/>

当用户单击“编辑”按钮时,他们将进入另一个视图(编辑视图)。

根据选择的 Eval("ID") 类型,不同的 UserControl 必须可见。

我注意到,如果回发发生在同一表单(编辑视图)上,它就会丢失动态控件的状态。因此我认为我需要通过 Page_Load 访问/填充 DIV (appViewArea)。

在我的 Page_Load 中,如果用户访问了编辑视图,我只希望它用正确的 UserControl 填充 DIV。这是我当前的 Page_Load 中的内容。

if(_multiView1.ActiveViewIndex == 1) // This is the Edit View
{
  int typeId = Convert.ToInt32(viewAppointmentID.Value); // viewAppointmentID is a hidden field which is created within the ButtonClick method.

switch (typeId)
{
  case 1:
        {
          var control = Page.LoadControl("~/editBirthAppointment.ascx");
          appViewArea.Controls.Add(control); 
        }
        break;
  case 2:
        {
          var control = Page.LoadControl("~/editDeathAppointment.ascx");
          appViewArea.Controls.Add(control); 
        } 
        break;
}
}

protected void ButtonClick(object sender, CommandEventArgs e)
{
   var viewAppointmentID = Convert.ToInt32(e.CommandArgument);
   _multiView1.ActiveViewIndex = 1;
}

我的问题是它永远不会进入 if 语句 (_mutliview) 并且它也不存储 viewAppointmentID。我浏览过网络,但找不到任何真正对我有帮助的东西。我访问它的方式错误吗?

预先感谢您的任何帮助。

谢谢

克莱尔

I am using c#.net

I have a repeater within a View (MultiView) each row contains a ‘Edit’ button (stored on the CommandArgument is the type ID).

<asp:Button ID="buttonClick" OnCommand="ButtonClick" CommandArgument='<%#Eval("ID")%>' runat="server" Text="Edit"/>

When the user clicks on the ‘Edit’ button they are taken through to another view (Edit View).

Dependant on which type Eval("ID") is selected a different UserControl must be visible.

I have noticed that if postback occurs on the same form (Edit View) it loses its state for the dynamic control. Therefore I think I need to access/populate the DIV (appViewArea) via the Page_Load.

Within my Page_Load I only want it to populate the DIV with the correct UserControl if the user has accessed the Edit View. Here is what I currently have within my Page_Load.

if(_multiView1.ActiveViewIndex == 1) // This is the Edit View
{
  int typeId = Convert.ToInt32(viewAppointmentID.Value); // viewAppointmentID is a hidden field which is created within the ButtonClick method.

switch (typeId)
{
  case 1:
        {
          var control = Page.LoadControl("~/editBirthAppointment.ascx");
          appViewArea.Controls.Add(control); 
        }
        break;
  case 2:
        {
          var control = Page.LoadControl("~/editDeathAppointment.ascx");
          appViewArea.Controls.Add(control); 
        } 
        break;
}
}

protected void ButtonClick(object sender, CommandEventArgs e)
{
   var viewAppointmentID = Convert.ToInt32(e.CommandArgument);
   _multiView1.ActiveViewIndex = 1;
}

My problem is that it never goes into the if statament (_mutliview) and it also doesn’t store the viewAppointmentID. I have looked around the web but cannot find anything that really helps me out. Am I accessing it the wrong way?

Thanks in advance for any help.

Thanks

Clare

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-08-13 21:52:50

您不需要 page_load,而是来自网格的 oncommand 事件,在这种情况下,您检查命令名称和/或命令参数,

似乎您应该使用

  if(!Page.IsPostback())
       // fill grid here

,并且在 onRowCommand 事件中,您可以将用户控件添加到页面

you don't need the page_load but the oncommand event from the grid and in that event you check the commandname and/or commandarguments

it seems that you should use

  if(!Page.IsPostback())
       // fill grid here

and in the onRowCommand event you can add the usercontrols to the page

赤濁 2024-08-13 21:52:50

我将此代码添加到我的按钮事件中。它现在似乎可以工作,因此必须将其存储在 ViewState 中。

I added this code instead into my button event. It seems to work now so must be storing it within the ViewState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文