标签: postback

Asp.NET服务器控件回发

我有一个想要创建的控件.这是我要完成的一个简单示例.

我希望控件包含一个按钮.

Button b = new Button();
b.Text = "Test";
b.Click += new EventHandler(b_Click);

this.Controls.Add(b);
Run Code Online (Sandbox Code Playgroud)

现在,控件呈现正常,按钮显示在页面上.我遇到的问题的核心是b_Click事件处理程序永远不会被触发.

protected void b_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)

这里的任何帮助将不胜感激.我不想在这里使用用户控件纯粹是出于自私的原因,并希望将其完全封装在一个DLL中.

提前致谢.

编辑**

namespace ClassLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl
    {

        protected override void CreateChildControls()
        {
            Button b = new Button();
                    b.ID = "button";
            b.Text = "Click Me";
            b.Click += new EventHandler(b_Click);

            this.Controls.Add(b);

            base.CreateChildControls();
        }

        protected void b_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new LiteralControl("<p>Click!</p>"));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

所以从评论中我试过这个.最简单的例子,仍然没有去.有什么东西我从根本上失踪了吗?

c# asp.net events user-controls postback

2
推荐指数
1
解决办法
3126
查看次数

回复从PHP到javascript的响应

我是新形式和发布数据......所以我不知道如何解决这个问题!

我有一个简单的PHP页面(第1页):

<form method="post" action="/page2.php">
<input type="search" value="E-Mail Address" size="30" name="email" />
<input type="submit" value="Find E-Mail" />
</form>
Run Code Online (Sandbox Code Playgroud)

您如何注意到...此表单将'email'值发布到page2.在page2中有一个小脚本,它在数据库中查找以检查电子邮件地址是否存在.

$email = $_POST['email'];

$resut = mysql_query("SELECT * FROM table WHERE email = $email");
.   
.
.
/* do something */
.
.
.

if($result){
    //post back yes
}
else{
   //post back no
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何在php中回复帖子!如何从javascript方法读取回发数据,该方法显示报告搜索结果的警报?

这只是我正在尝试做的一个例子,因为我的page2在回发之前做了一些其他动作.

当我点击提交按钮时,我正在尝试为旋转指示器设置动画...这就是我需要回发到javascript方法的原因!因为javascript函数应该停止动画并使用搜索结果弹出警报!

非常感谢提前!

javascript php post postback

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

如何在ASP.NET MVC中使用带PostBack的服务器控件?

单击该按钮后,它会抛出"viewstate MAC failed的验证"异常.我知道MVC不支持PostBack但是有什么方法可以解决这个问题吗?

或者我们需要在MVC中使用HtmlHelper?

以下是我在View中的代码:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<form id="form1" runat="server">

<h2>Hello World</h2>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />

<asp:Button ID="Button1" runat="server" Text="Button" />

</form>

</asp:Content>
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc postback servercontrols

2
推荐指数
1
解决办法
1255
查看次数

ASP:Checkbox如何仅在选中时自动回复?

我有一个复选框,设置如下:

<asp:CheckBox ID="myCheckbox" runat="Server" OnClick="showLoadingScreen(this.checked);" AutoPostBack="true" Text="Check me for more data!" /> 
Run Code Online (Sandbox Code Playgroud)

showLoadingScreen函数如下:

function showLoadingScreen(isChecked) {         
if (isChecked)
    {
        document.getElementById('form1').style.display='none';
        document.getElementById('img_loading').style.display='block';
    }
else { return false; }
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了else子句,希望我只能在复选框被选中后回复,但它会在任何一种情况下都回发.

我在页面上有一个网格(在form1内),在页面加载时加载了一组数据,但是为了向它添加一些额外的数据,我添加了这个复选框(它是一个运行时间较长的进程,所以我只想按需加载,而不是预先加载.当它被检查时,我想显示加载gif,回发,抓取数据,然后返回.如果取消选中该框,我不想做任何事情,因为在页面上留下足够多的数据是完全正常的(也就是说,预先显示的数据是选中复选框时显示的数据的子集).

是否有任何方法可以使复选框自动回复检查,但不是未选中?

编辑:使用Dark Falcon的建议,我修改了复选框,如下所示:

<asp:CheckBox ID="myCheckbox" runat="Server" OnClick="return showLoadingScreen(this.checked);" AutoPostBack="true" Text="Include HQ Values" /> 
Run Code Online (Sandbox Code Playgroud)

和javascript:

function showLoadingScreen(checked) {         
alert(checked);
if (checked)
    {
        document.getElementById('form1').style.display='none';
        document.getElementById('img_loading').style.display='block';
        document.form1.submit();  //my own addition, to get it to post back
    }
else { return false; }
}
Run Code Online (Sandbox Code Playgroud)

现在,它会在已检查时重新发布,但该框无法再取消选中.正如您所看到的,我添加了一个警报来显示传入的值.当您取消选中该框时,它会传递正确的值(false),但之后会以某种方式再次检查.

这不是一个大问题,因为没有理由取消选中该框(因为正如我之前所说,检查时数据集是未经检查的数据集的超集),但我仍然想知道它为什么这样做.有任何想法吗?

javascript asp.net checkbox postback autopostback

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

FileUpload控制回发问题

我在UpdatePanel内的aspx页面上有一个FileUpload控件,点击一个按钮我要设置一个带有所选文件文件名的标签.

这是我的代码:

ASPX页面:

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:FileUpload runat="server" ID="fuSimple"></asp:FileUpload>
            <asp:Button runat="server" Text="Button" ID="btnPostback" 
                onclick="btnPostback_Click" />
            <br />
            <asp:Label ID="lblFileName" runat="server" Text="File Name: "></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
Run Code Online (Sandbox Code Playgroud)

代码背后:

protected void btnPostback_Click(object sender, EventArgs e)
    {
        lblFileName.Text = "File Name: " + fuSimple.FileName;  
    }
Run Code Online (Sandbox Code Playgroud)

每次我按下按钮,我都会得到一个空字符串.我错过了什么?

此外,当我没有UpdatePanel这样做时,它工作正常.

评论/帮助表示赞赏.

asp.net postback updatepanel file-upload

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

ASP.NET动态命令按钮仅每隔一次触发一次

类似于这个问题,ASP.Net动态命令按钮事件不会触发,但问题略有不同.

下面提供的是我的代码的(非常)精简版本.

    protected void Page_Load(object sender, EventArgs e)
    {
        RenderDataItems();
    }

    private void RenderDataItems()
    {
        pnlDataItems.Controls.Clear()
        DataTable dt = MyClass.GetAllData();

        foreach (DataRow dr in dt.Rows)
        {
            Button b = new Button();
            b.Command += new CommandEventHandler(SelectItem);
            b.CommandArgument = dr["ID"].ToString();
            b.ID = "btnData" + dr["ID"].ToString();

            if (hdnDataListID.Value == dr["ID"].ToString())
            {
                b.Text = "Selected Item";
            }
            else
            {
                b.Text = "Pick This Item";
            }
            pnlDataItems.Controls.Add(b);
        }
    }

    private void SelectItem(object sender, CommandEventArgs e)
    {
        hdnDataListID.Value = e.CommandArgument.ToString();
        RenderDataItems();
    }

    private void …
Run Code Online (Sandbox Code Playgroud)

c# asp.net postback .net-2.0

2
推荐指数
1
解决办法
2319
查看次数

在PostBack上保留动态UserControl

我正在将用户控件动态加载到div中,我需要在回发时保留该控件,以便Save在用户完成编辑后调用方法。div和所有用户控件都具有EnableViewState = True。

ASPX Div声明

<div id="dynamicDiv" runat="server" enableviewstate="true">
</div>
Run Code Online (Sandbox Code Playgroud)

背后的代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not IsPostBack) Then
        'Implement some logic to load the User Controls using LoadControl
        'Set all the userControl properties (including EnableViewState = True)
        'Call a method in the user control to load it's content

        Me.dynamicDiv.Controls.add(userControl)
    End If
End Sub

Protected Sub Save(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    For Each …
Run Code Online (Sandbox Code Playgroud)

vb.net asp.net viewstate user-controls postback

2
推荐指数
1
解决办法
4764
查看次数

ASP MVC3视图模型在回发时将数据从集合对象丢失到编辑方法

我的ASP MVC3视图模型在Edit方法加载此特定页面时工作正常,但是当用户点击保存并进行回发时,两个集合对象(List<T>)中包含的所有信息都将丢失.有任何想法吗?

这是视图中的代码.这会正确加载所有代理信息(ID和状态代码)

    @for (int i = 0; i < Model.Fixed.Count; i++)
    {           

        if(!String.IsNullOrWhiteSpace(Model.Fixed[i].AgentId))
        {
           fixedRow++;

           if (fixedRow > 2)
           {
               var rowId = "row" + fixedRow.ToString() + "F";
               <tr id=rowId class="noSee">
                    <td>
                        @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                         (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.Fixed[i].AgentId)
                        @Html.ValidationMessageFor(model => model.Fixed[i].AgentId)
                    </td>
                    @if(fixedRow > 1)
                    {
                        var send = "MoreFixed(" + (fixedRow + 1).ToString() + ");";
                        var dataId = "plus" + fixedRow.ToString() + "F;";
                        <td id=@dataId  class="more" onclick=@send>+</td>
                    }
               </tr> 
           }
           else
           { …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc postback viewmodel asp.net-mvc-3 asp.net-mvc-2

2
推荐指数
1
解决办法
3838
查看次数

保留asp.net中的枚举值

我有一个asp.net页面,其中我使用枚举(在app_code中的类文件中定义了属性)

现在我的问题是,无论何时页面获得回发,属性中的枚举值都会重置为第一个

我甚至尝试将属性设置为静态,但仍然没有帮助.

下边是

我的枚举和财产声明:

private static UrlType _type;
public static UrlType UrlPattern
{
    get
    {
        HttpContext.Current.Response.Write("GET: " +_type + "<br>");
        return _type;
    }
    set
    {
        _type = value;
        HttpContext.Current.Response.Write("SET : " +_type + "<br>");
    }
}
public int VanityId { get; set; }
public enum UrlType
{
    ArticleOnly,
    ArticleCategoryCombination,
    Normal,
    TechForum
}
Run Code Online (Sandbox Code Playgroud)

这就是我所说的:

public void BindRewrite()
{
    GrdRewrite.DataSource = objVanity.GetAllRewriteVanities(Vanity.UrlPattern);
    GrdRewrite.DataBind();
    if (Vanity.UrlPattern == Vanity.UrlType.ArticleCategoryCombination)
    {
        GrdRewrite.Columns[2].Visible = false;
        GrdRewrite.Columns[3].Visible = GrdRewrite.Columns[5].Visible = GrdRewrite.Columns[6].Visible = true;
    }
    else if …
Run Code Online (Sandbox Code Playgroud)

c# asp.net viewstate enums postback

2
推荐指数
1
解决办法
2101
查看次数

无效的回发或回调参数.使用启用事件验证

从2.0迁移到.net 4.5.1后我有这个错误

无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证.

[ArgumentException:无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证.]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId,String argument)+144
System.Web.UI .Control.ValidateEvent(String uniqueID,String eventArgument)+111
System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey,NameValueCollection postCollection)+55
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler .LoadPostData(String postDataKey,NameValueCollection postCollection)+16
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,Boolean fBeforeLoad)+303
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1593

我可以做什么?

我在localhost上有这个问题,但如果我尝试远程访问它,问题就解决了(在另一个工作站上).如果同事试图在他的电脑(对我工作的那个)上访问它,他们会遇到同样的问题,但如果他们使用我的工作站,该应用程序工作.

c# asp.net validation postback callback

2
推荐指数
1
解决办法
9246
查看次数