1

我们在我们的站点中有一些函数调用是有条件的,所以我们用委托函数替换它们,问题是现在在函数调用中,控件值总是第一个(就像它的缓存一样)。可能是什么原因?

最好在初始化时设置委托或使用 if 每次调用正确的函数?

简化示例:

aspx:

<form runat="server" id="form1">
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="False" EnableScriptGlobalization="True" EnableScriptLocalization="True" EnablePageMethods="True"></ajaxToolkit:ToolkitScriptManager>

    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
        <ContentTemplate>
            <asp:CheckBox ID="cbViewPrinted" Text="View printed docs" runat="server" />
            <asp:Button ID="btnFilter" runat="server" OnClick="btnFilter_Click"  />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

CS:

private delegate void LoadDataFuncx();
private LoadDataFuncx LoadData
{
    get
    {
        (LoadDataFuncx)Session["LoadData"];
    }
    set
    {
       Session["LoadData"]=value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Initialize();
    }
}

private void Initialize()
{
    if(conditionx==true)
    {
        LoadData=LoadDataCaseA;
    }
    else
    {
        LoadData=LoadDataCaseB;
    } 
}

private void LoadDataCaseA()
{
   //if called from delegate this always is false (even when the user checked the control or i set the value to checked by code later...)
   bool viewprinted=cbViewPrinted.Checked;
}

private void LoadDataCaseB()
{
   //if called from delegate this always is false (even when the user checked the control or i set the value to checked by code later...)
   bool viewprinted=cbViewPrinted.Checked;
}

protected void btnFilter_Click(object sender, EventArgs e)
{
    LoadData(); //calling from delegate...always gets the first control values (cached?) :S

    if(conditionx==true) //direct calling by checking condition, gets the current control value (correct)
    {
        LoadDataCaseA();
    }
    else
    {
        LoadDataCaseB();
    } 
}
4

0 回答 0