简体   繁体   English

在ASP.NET自动回发之前更改下拉项文本

[英]Changing dropdown items text before asp.net autopostback

I am taking over a project in which a client has some annoying issues. 我正在接管一个客户遇到一些烦人的问题的项目。 They have a dropdown that autopostbacks on change to place the selected text into a t-sql query. 它们具有一个下拉列表,该下拉列表会在更改时自动回传以将所选文本放入t-sql查询中。 Any value that has an apostrophe is causing an query error due to not being escaped 任何带有撇号的值都会由于未转义而导致查询错误

I do not have access to compiled code, but was hoping to write a quick band-aid fix to on selected changed, before posting replace an apostrophe to a double apostrophe to escape it as it goes into query. 我无权访问已编译的代码,但希望在选定的更改上写一个快速的创可贴修复程序,然后再将替换的撇号发布到双撇号以在查询时转义它。

I wrote a javscript ddl.change function that works at changing the text. 我编写了一个javscript ddl.change函数,该函数可用于更改文本。

This however is not working even though the apostrophe does change into two. 但是,即使撇号确实变成了两个,这也不起作用。 I was wondering if someone could help understand why. 我想知道是否有人可以帮助理解原因。

I have two thoughts of scenarios causing the issue. 我对导致此问题的方案有两种想法。

  1. On autopostback, it triggers before javascript change function does, therefore passing the original value before javascript has a change to modify it. 在自动回发时,它会在javascript更改功能之前触发,因此会在javascript进行更改以对其进行修改之前传递原始值。

  2. The server side code only understands what it originally placed into the dropdown and therefore no matter how much I manipulate the client code, it will only see what it placed? 服务器端代码仅了解其最初放置在下拉列表中的内容,因此无论我操作客户端代码有多少,它都只会看到其放置了什么内容?

Can anyone confirm either of these scenarios? 任何人都可以确认这两种情况吗? Help would be appreciated! 帮助将不胜感激!

EDIT: I REVERSE ENGINEERED THE CODE, YES ITS VERY UGLY (AND SQL INJECTABLE) BUT IS NOT MINE AND I CANNOT MODIFY IT 编辑:我反转了该代码,是的,它非常丑陋(并且SQL可注射)但是不是我的,并且我无法对其进行修改

C# Code C#代码

protected void ddls_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddls.SelectedIndex == 0)
        {
            this.pnlA.Visible = false;
        }
        else
        {
            this.pnlA.Visible = true;
            string text = Common.GetSql("~/Sql/" + this._Conn + "/PropertyAddressReverseSearch.sql", false, true).Split(new char[]
            {
                Conversions.ToChar(this._Delimiter)
            })[4];
            text = string.Concat(new string[]
            {
                "SELECT * FROM (",
                text,
                ") a WHERE StreetName='",
                this.ddls.SelectedItem.Text,
                "' "
            });
            this.Bind(this.ddla, text);
            this.ddla.Items.Insert(0, new ListItem("I'm not sure of the house number...", Conversions.ToString(-1)));
            this.ddla.Items.Insert(0, new ListItem("", Conversions.ToString(0)));
            this.map.Visible = false;
        }
    }

Javscript + Control Javscript +控制

<asp:dropdown runat="server" id="ddls" autopostback="true">
<script type="javascript/text">
    $(document).ready(function() {
       $("select[id$='adsearch_ddls']").change(function() {
           var ddlsValue = $("select[id$='adsearch_ddls'] option:selected").text();
            ddlsValue = ddlsValue.replace(/'/g,"\'\'");
           $("select[id$='adsearch_ddls'] option:selected").text(ddlsValue);
           return false;
       });
    });

</script>

You can not modify (or add/remove) the Select/dropdown list items client-side, and simply get the same server-side items, with ASP.NET Webforms when viewstate is enabled. 当启用viewstate时,您无法使用ASP.NET Webforms修改(或添加/删除)客户端的“选择/下拉列表”项,而仅获得相同的服务器端项。

Unless you send back the modified items in another way, like for example in this answer where list items are copied to a hidden field: 除非您以其他方式发回修改过的项目,例如在此答案中将列表项目复制到隐藏字段中,例如:

function SaveList()
{
//Clear the hidden field
var hField =  document.getElementById('<%= YourHiddenField.ClientID %>'); 
hField.value = '' ;

var selectedList = document.getElementById('<%= YourDropDownList.ClientID %>')
for(i = 0; i < selectedList.options.length; ++i)
{         
hField.value = hField.value + ',' + selectedList.options[i].value;
}

That is, assuming by "On selected index change calls server side code" you mean a postback is triggered? 也就是说,假设“在选定的索引更改上调用服务器端代码”表示您触发了回发?

Disabling the ViewState will cause other problems (like SelectedIndexChanged not triggering, etc). 禁用ViewState会导致其他问题(例如SelectedIndexChanged无法触发等)。

You could handle the selection change through your own (AJAX) postback. 您可以通过自己的(AJAX)回发处理选择更改。 But the difference between server- and client-side list items would still remain. 但是服务器端和客户端端列表项之间的差异仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM