标签: webforms

.Net更新Session对象的副作用

Session我的.Net Webforms项目中的对象具有一些奇怪的功能(至少对我而言).我正在传递对象Sessions并且我没有使用任何global变量.我有一个描述如下的方法:

 private int Foo()
 {
     RmRule ruleInEdit = (RmRule)Session["ruleInEdit"];
     //here ruleInEdit.subjectAreaID=0
     bool isValid = Validate();
     if (isValid)
     {
        //do some stuff
        //here ruleInEdit.subjectAreaID is 10 instead of 0
        //the Validate function modified the ruleInEdit object 
      }

    //other code
  }

   private bool Validate()
   {
       bool isValid;
       //check some stuff
       RmRule rule = (RmRule)Session["ruleInEdit"];
       rule.subjectAreaID = 10;
       Session["ruleInEdit"] = rule;
       return isValid;
    }
Run Code Online (Sandbox Code Playgroud)

我认为subjectAreaID的值仍应为0但显然我误解了一些东西.ruleInEdit当我将对象写回时,.Net如何(或为什么)自动更新对象Session.

  • 在原始方法中Foo,我不应该强制ruleInEdit再次读取会话以获取更新的对象和属性吗?
  • 由于我初始化它的方式,此对象是否与会话范围相关联?

c# session webforms

0
推荐指数
1
解决办法
760
查看次数

Webforms:如果url没有.aspx扩展名,则不会捕获404错误

我有一个webforms项目,我想记录Global.asax中的所有404错误,所以我把这个代码放在那里:

void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;
    if (httpException == null)
    {
        Log.Error(string.Format("Unkown error:", Request.Url.AbsoluteUri), exception);
    }
    else if (httpException.GetHttpCode() == 404)
    {
        Log.Warn(string.Format("RequestError 404: {0}", HttpContext.Current.Request.Url.AbsoluteUri));
    }
    else
    {
        Log.Error(string.Format("RequestError {0}: {2}\r\n{1}", httpException.GetHttpCode(), Server.UrlDecode(Request.Headers.ToString()), HttpContext.Current.Request.Url.AbsoluteUri), httpException);
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于扩展名为".aspx"的所有请求.如果没有它或使用其他扩展,Application_Error则会被绕过.

这是我的 web.config

<customErrors defaultRedirect="/500.aspx" mode="Off">
  <error statusCode="404" redirect="/404.aspx"/>
  <error statusCode="500" redirect="/500.aspx"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)

任何的想法?

c# error-handling webforms

0
推荐指数
1
解决办法
1375
查看次数

使用按钮在aspx.cs上调用JavaScript函数(在aspx中)

我有这个aspx:

 <body>
    <div>
    <script type="text/javascript">
        function NewPage() {
            document.location.href = "http://www.nextservice.pt/"
        }
        </script>
         <form id="form1" runat="server">
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    <asp:Button ID="Btn2" runat="server" Text="OK" onclick="Button2_Click" />

            CODE1: <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000" />
        </form>
        </div>
</body>
Run Code Online (Sandbox Code Playgroud)

我正在使用Web表单,我不会在aspx.cs上调用此按钮

   public partial class SITE_TESTER : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click (object sender, EventArgs e)
    {
        string code = TextBox1.Text.ToString();

        if (!verifyCode(code))  // comparing users from table 
        {
            Label1.Text = "Not Exists";  //for invalid code
        }
        else …
Run Code Online (Sandbox Code Playgroud)

.net javascript asp.net webforms

0
推荐指数
1
解决办法
4万
查看次数

HttpRequest结束时的事件?

我正在使用asp.net表单.有一个Page_Load事件,但有结束事件吗?

我有一个在pageload上创建的linq datacontext,我想在完成后处理它.

c# linq asp.net webforms

0
推荐指数
1
解决办法
80
查看次数

为什么我在下面的代码中得到"服务器标签没有格式错误"?

我知道我错过了什么,但我有限的经历阻碍了我.有什么建议?谢谢你的帮助!

页码:

<asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <asp:Image runat="server" ID="image1" ImageUrl="<%# DataBinder.Eval(Container.DataItem, "url") %>" />
            </ItemTemplate>
        </asp:Repeater>
Run Code Online (Sandbox Code Playgroud)

代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
        var dt = new DataTable("images");
        dt.Columns.Add("url");
        dt.Rows.Add("http://www.boeing.com/assets/images/defense-space/military/p8a/images/P8-A_index.jpg");
        dt.Rows.Add("http://www.tractorbynet.com/forums/attachments/power-trac/258427d1333048884-p-8-replacing-p3c-p-8a_mma_changed_wing_lg.jpg");

        var ds = new DataSet();
        ds.Tables.Add(dt);

        Repeater1.DataSource = ds.Tables[0];
        Repeater1.DataBind();
    }
Run Code Online (Sandbox Code Playgroud)

错误: 在此输入图像描述

c# asp.net webforms

0
推荐指数
1
解决办法
1221
查看次数

在Page_Init上设置Page.EnableEventValidation

我有一个用户控件,我在代码隐藏中成功设置Page.EnableEventValidation = falsePage_Init事件(为了将页面标记呈现为字符串):

MyControl.ascx.cs

protected void Page_Init(object sender, EventArgs e)        
{  
    if (Request.Form["__EVENTTARGET"] != null
        && Request.Form["__EVENTTARGET"] == btnPrint.ClientID.Replace("_", "$"))
    {
        Page.EnableEventValidation = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在单独的页面上复制此功能时(这次是在runat=server脚本标记中)...

MyPage.aspx

<script runat="server">
    protected void Page_Init(object sender, EventArgs e)        
    {  
        if (Request.Form["__EVENTTARGET"] != null
            && Request.Form["__EVENTTARGET"] ==
            btnDownloadPDF.ClientID.Replace("_", "$"))
        {
            Page.EnableEventValidation = false;
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

...我收到以下错误:

'EnableEventValidation'属性只能在页面指令或配置部分中设置.

现在,在我的第一个例子中,我在最初尝试执行此操作时收到此错误Page_Load; 但是,似乎您可以通过编程方式禁用事件验证,只要它在(或之前)完成Page_Init.不幸的是,在我的第二个例子中,这不起作用.

为什么这种情况在一种情况下有效而另一种情况无效?它与代码不在代码隐藏的事实有关吗?

c# asp.net webforms

0
推荐指数
1
解决办法
5258
查看次数

将项目从checkboxlist传递到SQL Server表

我正在尝试从复选框列表中获取项目并将它们添加到SQL Server表中.我假设它就像循环遍历每个项目然后将其插入表中一样简单,但我得到一个未处理的异常,使用以下代码:

using (SqlConnection connection = new SqlConnection(connectionString))
{
     for (int i = 0; i <= UPCList.Items.Count; i++)
     {
         string finalSubmit =
              "INSERT INTO Boxes (BoxNumber)"
              + "VALUES @selected";

          SqlCommand command = new SqlCommand(finalSubmit, connection);
          command.Parameters.AddWithValue("@selected", i);
          command.Connection.Open();
          command.ExecuteNonQuery();
          command.Connection.Close();
      }
 }
Run Code Online (Sandbox Code Playgroud)

编辑:下面的建议之一有效,但它放入项目的ID而不是列表项本身的值.如何将列表中每个项的值插入SQL表?

c# sql-server ado.net webforms

0
推荐指数
1
解决办法
1024
查看次数

使用AngularJS或JQuery的表单生成器

我是核心C++开发人员,最近我学习了Scala和Lift并学习了AngularJS.我需要开发一个类似于http://www.jotform.com/的应用程序.

有很多机会可以更改现有功能或为我的应用程序添加新功能.我想根据http://www.slideshare.net/ericshepherd/building-a-javascript-module-framework-at-gilt构建具有插件架构支持的应用程序.如何使用AngularJS开发插件架构.由于我对网络开发很陌生,我不确定我是否正朝着正确的方向前进.

请告诉我哪个是开发应用程序的最佳方式?哪个更适合(AngularJS/JQuery)用于开发像jotform这样的应用程序?

jquery plugins templates webforms angularjs

0
推荐指数
1
解决办法
8931
查看次数

ASP.NET Ajax Toolbox错误无法读取未定义的属性UI

我正在尝试学习如何在我的.net应用程序中使用Ajax Toolbox.我通过Manage NuGet Packages添加了工具箱,并将以下内容添加到我的web.config文件中:

添加tagPrefix ="ajaxToolkit"assembly ="AjaxControlToolKit"namespace ="AjaxControlToolKit"

每当我去一个页面并尝试从Ajax Toolkit添加一个元素时,我收到一条错误消息"无法读取未定义的属性'UI'

如果我尝试在页面上添加ToolkitScriptManager,我会收到一条错误"只能将一个ScriptManager实例添加到页面中"

任何帮助,将不胜感激 :)

asp.net ajax webforms toolkit

0
推荐指数
1
解决办法
4852
查看次数

如何使用DropDownList的SelectedIndexChanged事件

我的DropDownListwebform中有两个s,当我在第一个下拉列表中选择一个值时,我希望在第二个下拉列表中自动选择一个相关值.

这就是我目前拥有的:

    <table>   
        <tr>
            <td>
                <asp:Label ID="lbmanu" runat="server" Text="Furniture Manufacturer : 
                   "></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddmanu" runat="server" 
                    DataSourceID="Sql_fur_model_manu"    
                    DataTextField="manufacturer" DataValueField="manufacturer" 
                    onselectedindexchanged="ddmanu_SelectedIndexChanged">
                </asp:DropDownList>
                <asp:SqlDataSource ID="Sql_fur_model_manu" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:conStr %>" 
                    SelectCommand="SELECT DISTINCT [manufacturer] FROM 
                     [furniture_manufacturer]">
                </asp:SqlDataSource>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbtype" runat="server" Text="Furniture Type : 
                        "></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddtype" runat="server" AutoPostBack="True">
                   </asp:DropDownList>
            </td>
        </tr>
   </table>
Run Code Online (Sandbox Code Playgroud)

代码背后:

protected void ddmanu_SelectedIndexChanged(object sender, EventArgs e)
{
    string query = "select furniture from furniture_model where manufacturer='" + 
    ddmanu.SelectedValue.ToString() + "'";
    con.Open();
    cmd …
Run Code Online (Sandbox Code Playgroud)

c# asp.net webforms event-handling

0
推荐指数
2
解决办法
10万
查看次数