简体   繁体   English

根据另一个控件的结果更新用户控件

[英]Updating User Controls based on result of another control

I'm new to ASP.NET C# and trying to accomplish the following: 我是ASP.NET C#的新手,正在尝试完成以下任务:

Have a Default.aspx page with two user controls (Control1.ascx and Control2.ascx) Both user controls are within UpdatePanels. 具有两个用户控件(Control1.ascx和Control2.ascx)的Default.aspx页。两个用户控件都在UpdatePanels中。

Need the TextBox on the second user control disabled until the TextBox on the first control has been validated. 需要禁用第二个用户控件上的TextBox,直到验证第一个控件上的TextBox。 Once validated, TextBox on user control2 needs to be enabled. 验证后,需要在用户control2上启用TextBox。

These user controls will be re-used on multiple pages. 这些用户控件将在多个页面上重复使用。 Help is appreciated. 感谢帮助。 If I need to post some code let me know. 如果我需要发布一些代码,请告诉我。

Here's code sample: 这是代码示例:

Default.aspx Default.aspx

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" 
      CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="Control1.ascx" tagname="CTL1"  tagprefix="uc1" %>
<%@ Register src="Control2.ascx" tagname="CTL2"  tagprefix="uc2" %>

<uc1:CTL1 Id="CTL1" runat="Server" UpdateMode="Conditional"/>
<uc2:CTL2 Id="CTL2" runat="Server" UpdateMode="Conditional"/>

Default.aspx.cs Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Control1.ascx Control1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control1.ascx.cs" Inherits="Control1" %>
 <asp:UpdatePanel ID="UpdatePanel_Fld1" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
  <asp:TextBox ID="TextBox_Fld1" runat="server" Enabled="True" ></asp:TextBox>
  <asp:ImageButton ID="ImageButton_ValidateFld1" runat="server" ImageUrl="~/images/validate.jpg"
      onclick="ImageButton_ValidateFld1_Click" />
  <asp:Label ID="LabelFld1Summary" runat="server" >&nbsp;</asp:Label>
  </ContentTemplate>
 </asp:UpdatePanel>  

Control1.ascx.cs Control1.ascx.cs

  public partial class Control1 : System.Web.UI.UserControl
   {
       protected void Page_Load(object sender, EventArgs e)
       {
       }
       public void ImageButton_ValidateFld1_Click(object sender, ImageClickEventArgs e)
       {
           LabelFld1Summary.Text = "Validated";
       }
   }

Control2.ascx Control2.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control2.ascx.cs" Inherits="Control2" %>
 <asp:UpdatePanel ID="UpdatePanel_Fld2" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
  <asp:TextBox ID="TextBox_Fld2" runat="server" Enabled="False"></asp:TextBox>
  <asp:ImageButton ID="ImageButton_ValidateFld2" runat="server" ImageUrl="~/images/validate.jpg"
      onclick="ImageButton_ValidateFld2_Click" />
  <asp:Label ID="LabelFld2Summary" runat="server" >&nbsp;</asp:Label>
  </ContentTemplate>
 </asp:UpdatePanel>  

Control2.ascx.cs Control2.ascx.cs

public partial class Control2 : System.Web.UI.UserControl
   {
       protected void Page_Load(object sender, EventArgs e)
       {
       }
       public void ImageButton_ValidateFld2_Click(object sender, ImageClickEventArgs e)
       {
           LabelFld2Summary.Text = "Validated";
       }
   }

我建议使用简单的JavaScript或让jQuery进行启用/禁用操作,将一些js代码添加到客户端验证脚本中

I suggest getting your scenario working with synchronous postbacks first, then adding the updatepanels into the mix. 我建议您先使您的方案与同步回发一起使用,然后再将updatepanels添加到组合中。 As a rough sketch, in a server-side OnClick handler for some button or other you can set the enabledness of the second control based on the result of validation of the first control. 作为粗略的草图,在服务器端的某个按钮或其他按钮的OnClick处理程序中,您可以根据第一个控件的验证结果来设置第二个控件的启用性。 Then, you just need to make sure you call Update() on Control2.ascx's UpdatePanel when the asynch postback targeting Control1.ascx's UpdatePanel happens. 然后,只需确保在针对Control1.ascx的UpdatePanel的异步回发发生时,在Control2.ascx的UpdatePanel上调用Update()

That said, this is going to be mildly annoying from a user's perspective. 就是说,从用户的角度来看这将是令人讨厌的。 Unless the first control modulates a great deal of UI below it (like, switching to a separate control set entirely), why not just let your users enter text into both fields and validate on the combination of them? 除非第一个控件在其下方调制了大量UI(例如,完全切换到单独的控件集),否则为什么不让您的用户在两个字段中输入文本并验证它们的组合?

I agree with Simon. 我同意西蒙的观点。 jQuery would probably be the easiest way to accomplish this. jQuery可能是实现此目的的最简单方法。 you can use jQuery's ajax methods to make calls to WebMethods if you need to interact with the code behind for any reason. 如果出于任何原因需要与背后的代码进行交互,则可以使用jQuery的ajax方法来调用WebMethod。

I've used update panels and the ICallbackEventHandler interface ( http://madskristensen.net/post/Asynchronous-GridView-in-5-simple-steps.aspx is a good example of its use) with some success. 我使用了更新面板和ICallbackEventHandler接口( http://madskristensen.net/post/Asynchronous-GridView-in-5-simple-steps.aspx是其使用的一个很好的例子),并取得了一些成功。

While I tend to stay away from UpdatePanels, I realized that they use the ICallbackEventHandler interface behind the scenes and I probably make more work for myself. 尽管我倾向于远离UpdatePanels,但我意识到它们在后台使用了ICallbackEventHandler接口,我可能会为自己做更多的工作。 The only benefit of doing it the way I did is that I don't have a bunch of garbage sitting in my ViewState (UpdatePanels love ViewState), as I've persisted it in the server-side session state. 按照我的方式进行操作的唯一好处是,在ViewState中没有一堆垃圾(UpdatePanels喜欢ViewState),因为我将其持久保存在服务器端会话状态中。

In my case, the reason for going with the ICallbackEventHandler method over using jQuery's ajax calls and WebMethods in the code behind was that I wanted to have access to non-static fields (including my web controls). 就我而言,使用ICallbackEventHandler方法而不是在后面的代码中使用jQuery的ajax调用和WebMethods的原因是我想访问非静态字段(包括我的Web控件)。 Turns out I needed to manually persist the data displayed on the page in session or otherwise and probably ended up making more work for myself in the end. 事实证明,我需要在会话中手动保存页面上显示的数据,否则最终可能需要为我自己做更多的工作。 It was a good chance to learn something new, but the benefits I gained from having access to the pages controls were minimal/non-existent. 这是学习新知识的好机会,但是我从访问页面控件中获得的好处很小/根本不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于在另一个用户控件中的选择来重新创建用户控件中的动态控件 - recreation of dynamic controls in user control based in selection in another user control 在 asp.net 更新面板中更新用户控件(基于另一个用户控件中的操作) - updating a user control in an asp.net update panel (based upon action in another user control) 添加 3 个标签控件的值以在另一个标签控件中输出结果时出错 - Error with adding values of 3 label controls to output result in another label control 从另一个用户控件(登录)更改WPF用户控件的可见性 - Change WPF user controls visibility from another user control (login) Xaml Windows Phone中另一个用户控件中带有用户控件的StackPanel - StackPanel with User Controls in another User Control in Xaml Windows Phone 根据其他控件事件动态加载用户控件 - Load User Controls dynamically based on other control events 如何在不进行数据绑定的情况下从另一个用户控件访问控件? - How to access controls from another user control without data binding? 从另一个用户控件中的控件更新用户控件中的更新面板 - Updating an Update Panel in a usercontrol from a control in another user control 用户控件控件为空 - User Control controls are null 用户控件的基本用法,关闭用户控件并打开另一个用户控件参数 - Basic use of user controls, closing a user control and opening another, user control parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM