【问题标题】:Postback without clicking button in VB.NET?回发而不点击VB.NET中的按钮?
【发布时间】:2014-07-09 19:19:10
【问题描述】:

我的代码目前要求我单击按钮将信息发布回服务器,有没有办法使用代码自动执行此操作,基本上是模拟单击按钮或更好,无需任何按钮即可将其发布回?

ASPX 代码:

           <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1" %>

                <%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

                <!DOCTYPE html>


                  <form id="form1" runat="server">
                      <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
                          <Scripts>
                              <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
                              </asp:ScriptReference>
                              <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
                              </asp:ScriptReference>
                              <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
                              </asp:ScriptReference>
                          </Scripts>
                      </telerik:RadScriptManager>


                  <asp:HiddenField runat="server" ID="svgHolder" />
    <asp:Button ID="Button1" Text="Export the RadHtmlChart" OnClientClick="getSvgContent(this);"
            runat="server" />
    <table class="clearTable2" style="page-break-after: avoid;">
        <tr>
            <td class="clearTable1"></td>
            <td class="clearTable2">
                <asp:Label ID="lblSectionTitle" runat="server" CssClass="label2"></asp:Label>
            </td>
            <td class="clearTable1"></td>
        </tr>
    </table>
    <table class="clearTable2">
        <tr>
            <td class="clearTable1"></td>
            <td class="clearTable1" style="width: 100%; visibility: visible">

                <telerik:RadHtmlChart runat="server" ID="RadHtmlChart1">
                    <Legend>
                        <Appearance Position="Bottom">
                            <TextStyle FontSize="14" Color="Blue" FontFamily="Courier New, sans-serif" />
                        </Appearance>
                    </Legend>
                    <PlotArea>
                        <XAxis>
                            <Items>
                                <telerik:AxisItem LabelText="KM41872"/>
                                <telerik:AxisItem LabelText="KM41873"/>
                                <telerik:AxisItem LabelText="KM41871"/>
                            </Items>
                            <MajorGridLines Visible="false" />
                            <MinorGridLines Visible="false" />
                        </XAxis>
                        <YAxis>
                            <MinorGridLines Visible="false" />
                        </YAxis>
                        <Series>
                            <telerik:LineSeries Name="Mean">
                                <SeriesItems>
                                    <telerik:CategorySeriesItem Y="0.59" />
                                    <telerik:CategorySeriesItem Y="0.63" />
                                    <telerik:CategorySeriesItem Y="0.6" />
                                    <telerik:CategorySeriesItem Y="0.65" />
                                    <telerik:CategorySeriesItem Y="0.64" />
                                    <telerik:CategorySeriesItem Y="0.63" />
                                    <telerik:CategorySeriesItem Y="0.65" />
                                    <telerik:CategorySeriesItem Y="0.67" />
                                    <telerik:CategorySeriesItem Y="0.63" />
                                </SeriesItems>
                            </telerik:LineSeries>
                            <telerik:LineSeries Name="Min">
                                <SeriesItems>
                                    <telerik:CategorySeriesItem Y="0.55" />
                                    <telerik:CategorySeriesItem Y="0.56" />
                                    <telerik:CategorySeriesItem Y="0.55" />
                                    <telerik:CategorySeriesItem Y="0.61" />
                                    <telerik:CategorySeriesItem Y="0.56" />
                                    <telerik:CategorySeriesItem Y="0.57" />
                                    <telerik:CategorySeriesItem Y="0.59" />
                                    <telerik:CategorySeriesItem Y="0.61" />
                                    <telerik:CategorySeriesItem Y="0.55" />
                                </SeriesItems>
                            </telerik:LineSeries>
                            <telerik:LineSeries Name="Max">
                                <SeriesItems>
                                    <telerik:CategorySeriesItem Y="0.66" />
                                    <telerik:CategorySeriesItem Y="0.74" />
                                    <telerik:CategorySeriesItem Y="0.66" />
                                    <telerik:CategorySeriesItem Y="0.71" />
                                    <telerik:CategorySeriesItem Y="0.72" />
                                    <telerik:CategorySeriesItem Y="0.73" />
                                    <telerik:CategorySeriesItem Y="0.71" />
                                    <telerik:CategorySeriesItem Y="0.74" />
                                    <telerik:CategorySeriesItem Y="0.71" />
                                </SeriesItems>
                            </telerik:LineSeries>
                        </Series>
                    </PlotArea>
                </telerik:RadHtmlChart>
            </td>
            <td class="clearTable1"></td>
        </tr>
    </table>
    <script type="text/javascript">
        function getSvgContent(sender) {
            //obtain an SVG version of the chart regardless of the browser
            var chartRendering = $find("<%=RadHtmlChart1.ClientID %>").getSVGString();
            //store the SVG string in a hidden field and escape it so that the value can be posted to the server
            document.getElementById('<%=svgHolder.ClientID%>').value = escape(chartRendering);
            //initiate the postback from the button so that its server-side handler is executed
            __doPostBack(sender.name, "");
            $('#Button1').click();
        }
    </script>
                      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>

代码隐藏: 公共类WebForm1 继承 System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Button1.Text = HttpUtility.UrlDecode(svgHolder.Value)

End Sub


'Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'    Dim svgText As String = HttpUtility.UrlDecode(svgHolder.Value)
'End Sub
End Class

【问题讨论】:

  • 将 JavaScript 连接到您希望页面回发的客户端事件。然后使用 JavaScript 在事件处理程序中“单击”按钮。
  • $('#Button1').click();在我的代码中模拟了“点击”,但似乎没有效果
  • 你的代码做了一些客户端的事情,然后会回发到服务器。这很可能不是您想要的,因为您将丢失客户端更改。您还在您的问题中声明您想消除该按钮,但您没有说您希望它何时运行(应该连接到运行事件)。
  • 我基本上只是想接收我的 htmlchart 的 svg 部分,但现在它只有在我点击浏览器中加载的 aspx 页面上的按钮时才有效。如果可能,我希望它自动发回。
  • 我明白这一点,但您需要了解事情不会自行发生,您必须将它们与某个事件联系起来。这就是所谓的事件驱动。如果您希望它在服务器端自动执行某些操作,您应该在服务器端使用 Page_Load 方法,这样就不需要 JavaScript。否则,如果您想在客户端执行此操作,请弄清楚您需要让它响应什么事件,然后通过模拟按钮单击或直接调用 __doPosBack 将其连接到该事件。

标签: jquery asp.net vb.net postback


【解决方案1】:
function DoPostBack(obj) {
            __doPostBack(obj.id, 'OtherParams');
}

Just call __doPostBack

【讨论】:

  • 这是回发一个特定的对象吗?
  • 是的...只需创建一个按钮,通过将其包裹在 div
    MYBUTTON
    中使其隐藏在页面上。然后使用该按钮进行回发。这样你甚至可以通过服务器上的指定事件方便地处理它。
  • 这是我的想法,未经测试...
    ​​ton>
    ... then.... $('#lbtnRibbonChanged').click() 这比调用 __doPostBack 更简单,但是如果你不想使用 __doPostBack 会很有用一个按钮,需要回发一个特定的控件。如果您只想触发回发并在服务器上处理它,这是最好的方法。确保 ClientIDMode 为 Static,以便它可以找到按钮。
【解决方案2】:

我认为你想要的是 __dopostback javascript 函数(google it)。它本质上是伪造来自 .Net 控件的回发事件,因此它的工作方式就像单击您的按钮一样。如果您不希望它显示但仍然挂接到它以进行回发,您还可以设置按钮的 diplay:hidden 属性。

【讨论】:

  • 现在,我所拥有的不起作用,由于某种原因,我必须物理单击按钮才能回发,但我想要完成的是加载带有回发的页面已经完成或以某种方式模拟它
  • 对于奖励积分,您可以通过使用 [webmethod] encosia.com/… 装饰您的服务器端页面方法 ajax 来实现无缝连接。
  • 去掉sender.name和按钮的名字。
猜你喜欢
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 2018-11-18
  • 2013-11-02
相关资源
最近更新 更多