1

I'm new to SO and fairly new to ASP.NET.

I have a LinkButton with an OnClientClick event, which I would like to execute some code before doing a crosspage postback.

This seems to work--I am able to retrieve the value from the next page--however, when I click "View" the HiddenField value is set to "Edit". I did some testing and it seems that all of the javascript functions are being called, regardless of which button is clicked. "Edit" is the last value set, so I am always retrieving the value "Edit" from my second page.

Javascript:

function viewfunc(control) {
    <% hidden.Value = "View"; %>
    <% hiddenpanel.Update(); %>
    return true;
}

function editfunc(control) {
    <% hidden.Value = "Edit"; %>
    <% hiddenpanel.Update(); %>
    return true;
}

Form:

<form id="form1" runat="server">
<asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional">
        <ContentTemplate>
    <asp:Panel ID="Panel1" runat="server">
        <asp:GridView Id="gridview1" runat="server" AllowPaging="True">
        <Columns>

            // Data fields here...

        <asp:TemplateField>
              <ItemTemplate>
                        <asp:LinkButton ID="viewlabel" runat="server" Text="View" OnClientClick="if(!viewfunc(this)) return false;" PostBackURL="~/NextPage.aspx"></asp:LinkButton>
              </ItemTemplate>
                </asp:TemplateField>

              <asp:TemplateField>
              <ItemTemplate>
                        <asp:LinkButton ID="editlabel" runat="server" Text="Edit" OnClientClick="if(!editfunc(this)) return false;" PostBackURL="~/NextPage.aspx"></asp:LinkButton>
              </ItemTemplate>
              </asp:TemplateField>
        </Columns>  
        </asp:GridView>
    </asp:Panel>    
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server" ID="hiddenpanel" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Panel runat="server">
            <asp:HiddenField runat="server" Id='hidden' Value='Empty' />
        </asp:Panel>
        </ContentTemplate>
</asp:UpdatePanel>
</form>  

Note: I would have preferred to call these functions as codebehind functions, but I couldn't find a way to execute the codebehind after the click event but before the postback.

Thanks for any and all suggestions!

4

1 回答 1

0

请尝试以下操作:

function viewfunc(control) {
    document.getElementById('<% hidden.ClientID %>').value = "View";
    return true;
}

function editfunc(control) {
    document.getElementById('<% hidden.ClientID %>').value = "Edit";
    return true;
}

那些应该更新您的隐藏字段,然后触发 updatepanel 更新事件。

当你有这样的东西<% hidden.Value = "View"; %>会简单地运行时,Asp.net 不会关心它是否在 javascript 函数中。我认为您不需要UpdatePanel在功能完成后告诉“更新”,我认为如果您不这样做,它应该可以正常工作。

于 2013-07-29T16:32:48.513 回答