asp.net/ajax

asp.net/ajax  1.
http://www.asp.net/AJAX/Documentation/Live/tutorials/UsingUpdatePanelControls.aspx

管理掌握触发器/以编程方式刷新一个UpdatePanel

触发一个UpdatePanel控件使UpdatePanel控件开始刷新的控件,要通过ScriptManager在Page页上"注册".这,通常会自动发生,当子控件在UpdatePanel控件的ContentTemplate内的时候。你也可以明确地指定触发器---通过使用UpdatePanel控件的Triggers集合UpdatePanel1.Triggers是UpdatePanel1不是UpdatePanel!另外,你也可以在你的页面的服务器端代码(cs代码内)编程(动词)指定触发器以引起UpdatePanel控件的刷新动作.万一触发器控件在"设计时"不可用,你也可以注册(动词)控件成为(动词)一个触发器---通过使用ScriptManager控件的RegisterAsyncPostBackControl(Control)方法,当然使用时是ScriptManager1而不是ScriptManager!
被以编程方式指定为触发器的控件必须被注册----在任何一次当PostBack发生的时候。我们要求你把调用UpdatePanel控件的
RegisterAsyncPostBackControl(Control)方法的代码放在你的页面的Page_Load事件里,象下面这样:

protected void Page_Load()
{
    ScriptManager1.RegisterAsyncPostBackControl(SurveyDataList);
}

要以编程方式"刷新"UpdatePanel控件,你可以主动调用UpdatePanel控件的Update()方法.这是很有用的,尤其是在必须在服务器端执行一些处理-----在刷新UpdatePanel控件之前.代码:

protected void ChoicesRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
    SortedList answers 
= this.AnsweredQuestions;
    RadioButtonList r 
= (RadioButtonList)sender;
    answers[r.ToolTip] 
= r.SelectedValue;
    
this.AnsweredQuestions = answers;

    ResultsList.DataSource 
= this.AnsweredQuestions;
    ResultsList.DataBind();

    
if (this.AnsweredQuestions.Count == SurveyDataList.Items.Count)
        SubmitButton.Visible 
= true;

    UpdatePanel1.Update();
}

下面的例子展示了一个页面,该页面注册了一个控件作为触发器----通过使用RegisterAsyncPostBackControl(Control)方法,并且以编程方式刷新一个UpdatePanel控件-----通过使用Update()方法。



<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<script runat="server">

    protected SortedList AnsweredQuestions
    
{
        get 
return (SortedList)(ViewState["AnsweredQuestions"?? new SortedList()); }
        set 
{ ViewState["AnsweredQuestions"= value; }
    }


    protected 
void Page_Load()
    
{
        ScriptManager1.RegisterAsyncPostBackControl(SurveyDataList);
    }


    protected 
void ChoicesRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    
{
        SortedList answers 
= this.AnsweredQuestions;
        RadioButtonList r 
= (RadioButtonList)sender;
        answers[r.ToolTip] 
= r.SelectedValue;
        
this.AnsweredQuestions = answers;

        ResultsList.DataSource 
= this.AnsweredQuestions;
        ResultsList.DataBind();

        
if (this.AnsweredQuestions.Count == SurveyDataList.Items.Count)
            SubmitButton.Visible 
= true;

        UpdatePanel1.Update();
    }


    protected 
void SubmitButton_Click(object sender, EventArgs e)
    
{
        
// Submit responses.
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    
<title>Registering Controls as Async Postback Controls</title>
    
<style type="text/css">
    .AnswerFloatPanelStyle 
{
    background-color
: bisque;
    position
: absolute;
    right
: 10px;
    height
: 130px;
    width
: 150px;
    border-right
: silver thin solid; border-top: silver thin solid; 
    border-left
: silver thin solid; border-bottom: silver thin solid;    
    
}

    
</style>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:ScriptManager ID="ScriptManager1" runat="server" />
            
<div id="AnswerFloatPanel" class="AnswerFloatPanelStyle" runat="server">
                
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
                    
<ContentTemplate>
                        Completed Questions:
                        
<asp:DataList ID="ResultsList" runat="server">
                            
<ItemTemplate>
                                
<asp:Label ID="ResultQuestion" runat="server" Text='<%# Eval("Key") %>' />
                                ::
                                
<asp:Label ID="ResultAnswer" runat="server" Text='<%# Eval("Value") %>' />
                            
</ItemTemplate>
                        
</asp:DataList>
                        
<style="text-align: right">
                            
<asp:Button ID="SubmitButton" Text="Submit" runat="server" Visible="false"
                                OnClick
="SubmitButton_Click" />
                        
</p>
                        
<asp:Label ID="Message" runat="Server" />
                    
</ContentTemplate>
                
</asp:UpdatePanel>
            
</div>
            
            
<asp:XmlDataSource ID="SurveyDataSource" 
                               runat
="server" 
                               XPath
="/Questions/Question"
                               DataFile
="~/App_Data/SurveyQuestions.xml"/>
            
<asp:DataList
                
ID="SurveyDataList"
                DataSourceID
="SurveyDataSource"
                runat
="server">

                
<ItemTemplate>
                  
<table cellpadding="2" cellspacing="2">
                    
<tr>
                      
<td valign="top">
                        
<asp:Label id="QuestionLabel" Text='<%# XPath("@Title")%>' runat="server" />
                      
</td>
                    
</tr>
                    
<tr><td>
                      
<asp:RadioButtonList ID="ChoicesRadioButtonList" runat="server" 
                        DataSource
='<%#XPathSelect("Choices/Choice") %>'
                        DataTextField="InnerText" DataValueField="InnerText" 
                        AutoPostBack="True"
                        ToolTip='
<%"Question" + XPath("@ID"%>'
                        OnSelectedIndexChanged="ChoicesRadioButtonList_SelectedIndexChanged"/>
                    
</td></tr>
                  
</table>
                  
<hr />
                
</ItemTemplate>
            
</asp:DataList>
        
</div>
    
</form>
</body>
</html>
2007年9月13日
原文地址:https://www.cnblogs.com/simhare/p/892058.html