当链接到另一个页面asp.net时,Page_Load触发

时间:2015-06-22 14:46:24

标签: c# html asp.net

我对ASP.NET很新,我一直在阅读与此相关的一些问题,但我仍然无法弄清楚我的代码有什么问题,我有一个default.aspx页面,顶部有一个菜单,使用列表(ul和li项目)创建并放置<a href="">标签来创建指向其他页面的链接但是在链接到另一个页面后,Page_Load事件触发在离开页面之前,我理解这将是Response.Redirect的预期行为,但我不知道如何使用标签来避免这种情况(如果可能的话),这是我用于默认的标记.aspx页面:

<ul id="lista">
    <li><a href="Default.aspx"><strong>Inicio</strong></a></li>
    <li><a href="Items.aspx"><strong>Item</strong></a></li>
    <li><a href="IKs.aspx"><strong>IK</strong></a></li>
    <li><a href="#"><strong>Acerca de</strong></a></li>
</ul>

这是我为Page_Load背后的代码:

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        ExcelUtility excel = new ExcelUtility();
        dtDefault = excel.LeerExcel();
        gridResults.DataSource = dtDefault;
        gridResults.DataBind();
        gridResults.VirtualItemCount = dtDefault.Rows.Count;
    }
}

基本上,我想要做的是在离开之前按照指向其他页面的链接而不加载默认页面,希望我自己清楚!

编辑:根本原因是在正文部分的开头有默认的<form runat="server">标记,这导致一旦链接在同一页面中再次触发Page_Load事件被点击,将超链接放在表单标签之外就可以了。

1 个答案:

答案 0 :(得分:0)

您的HTML代码应位于包含属性PostBack的某个HTML标记或自定义ASP控件中。这应该向服务器发出try{ int pur_user = prefs.getInt("C_user", Integer.MIN_VALUE); URL url = new URL("http://www.*.com/includes/purchase.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestMethod("POST"); JSONObject jsonObject = new JSONObject(); jsonObject.put("PUR_sku", SKU); jsonObject.put("PUR_user", pur_user); //convert JSONObject to JSON to String json = jsonObject.toString(); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(json); writer.close(); responseCode = connection.getResponseCode(); if(responseCode == 200) { InputStream content = connection.getInputStream(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } result = sb.toString(); //TODO get your stuff from result content.close(); } catch (Exception ex) { Log.e(TAG, "Failed to parse JSON due to: " + ex); } finally { connection.disconnect(); } } else { Log.e(TAG, "Server responded with status code: " + responseCode); } } catch(Exception ex) { Log.e(TAG, "Failed to send HTTP POST request due to: " + ex); } 请求。