在 PageLoad 中访问 CommandEventArgs
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要 page_load,而是来自网格的 oncommand 事件,在这种情况下,您检查命令名称和/或命令参数,
似乎您应该使用
,并且在 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
and in the onRowCommand event you can add the usercontrols to the page
我将此代码添加到我的按钮事件中。它现在似乎可以工作,因此必须将其存储在 ViewState 中。
I added this code instead into my button event. It seems to work now so must be storing it within the ViewState.