Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

asp.net - Invalid postback or callback argument. When dropdown list populated with jquery and there is a postback

I have two dropdown list boxes on change of first I want to fill the second one. I have written a jquery for this as follow

function BindModel() {
var makeCode = $('#ddMake').val();
var ddModel = $("#ddModel");
if (makeCode == "0") {
    return false;
}
else {
    $("#loading").show();
    $.ajax({
        type: "POST",
        url: "Services/GetModels.ashx",
        data: { 'makeCode': makeCode },
        success: function (data) {
            $("#loading").hide();
            $.each(data, function () {
                ddModel.append($("<option />").val(this.ModelCode).text(this.ModelName));
            });
        },
        error: function () {
            $("#loading").hide();
        }
    });
}
}

I have a generic http handler as follows

context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = Encoding.UTF8;
        if( context.Request["makeCode"]!=null)
        {
            var makeCode = context.Request["makeCode"].Trim();
            var lobjSearchProps = new CSearchAllProps
            {
                SearchValue = "%",
                MakeCode = makeCode
            };
            var allModelProps = new 
            CModelRestrictionLogic().GetModels(lobjSearchProps).ToList();
            context.Response.Write(JSONHelper.GetJSON(allModelProps));    
        }

where CModelRestrictionLogic().GetModels(lobjSearchProps) is my function which fetches data from the database.
and CModelRestrictionLogic().GetModels(lobjSearchProps) converts the list to JSON. Every thing is working fine. But whenever I post back it is giving an error:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

I know the reason because I have filled the dropdown list from jquery so it is getting an error.
I have also written function on init to fill the data from posted back values as follow

 protected override void OnInit(EventArgs e)
    {
        var postedOptionValue = Request.Form[ddModel.UniqueID];
        ddModel.Items.Add(new ListItem(postedOptionValue));
        base.OnInit(e);
    }

How can I get rid of this value? I don't want to use enableEventValidation="false" because it is a security concern.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...