zoukankan      html  css  js  c++  java
  • 如何解决asp.net 在vs2010(.net framework 4.0)中listview控件用jquery,javascript为模版中的服务端控件注入事件的方法?因为vs2010在这个方面有存在的bug!

    先说说具体的问题出在哪里?

    看下以下代码:

          protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
            {
                    if (e.Item.ItemType == ListViewItemType.InsertItem || e.Item.ItemType == ListViewItemType.DataItem)
                    {
                        DropDownList list1 = (DropDownList)e.Item.FindControl("DropDownList1");
                        TextBox LogoURLTextBox = (TextBox)e.Item.FindControl("LogoURLTextBox");

                        if (list1 != null && LogoURLTextBox != null)
                        {
                              list1.Attributes["onchange"] = "Onlinkclick(this,'" + LogoURLTextBox.ClientID + "')";//如果是这样写的话,在javascript中调用时,看下面

             }

                        }
                    }

            }

           function Onlinkclick(src,logotext) {
                if ($(src).val() == "Text") {
                    $("#" + logotext).hide();//这样看上去是没有错误的,但是却这个代码是功能却不能实现。

                }
                else {
                    $("#" + logotext).show();
                }

    第一种解决方案:

    1.直接将目标框架转为.net framework 3.5,这样是可以的,因为VS2008是可以运行实现上述功能。但是大家可能会遇到这样的问题,在

    .net framework 3.5中在是不支持 protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)事件中调用

     e.Item.FindControl是提示不出来的,不能通过这种方式来找到控件。必须通过以下方式找到服务端控件LogoURLTextBox。

                 ListViewDataItem lvdataview = (ListViewDataItem)e.Item;
                TextBox LogoURLTextBox = (TextBox)lvdataview.FindControl("LogoURLTextBox");

    第二种解决方案:

    1.在listview中有这样一个属性,不知道大家有没有注意。叫做ClientIDMode,一般默认是Inherit这样,将他改为AutoID,当你打开浏览器浏览调试的页面时,

    去找那个你附加  list1.Attributes["onchange"]属性的,控件,看看它渲染出来的id是多少。

    当ClientIDMode为Inherit,右击浏览的页面,查看源文件,看下他们各自的id是多少?

     

     当设置ClientIDMode为AutoID时, 右击浏览的页面,查看源文件,看下他们各自的id是多少?

    而这两个字符串,就相差一个字符ListView1_,所以就可以通过拼字符串来解决。

    //第二种办法,将上诉赋属性的代码改为(要将listview中的clientIDMode设置为AutoID)

    //list1.Attributes["onchange"] = "Onlinkclick(this,'ListView1_" + LogoURLTextBox.ClientID + "')";

    第三种解决方案:

    往你要注入事件的服务端控件,添加额外myId属性等于LogoURLTextBox.ClientID注入事件的客户端ID,LogoURLTextBox(要注入事件的代码).Attributes["myId"] =LogoURLTextBox.ClientID;

    然后右击浏览的页面,查看源文件。你会发现。

    这样在javascript中可以这样调

            function Onlinkclick(src,logotext) {
                if ($(src).val() == "Text") {
                    $("input[myid = " + logotext + "]").hide();             }//z这个是jquery属性选择器,不懂的你可以去W3Cschool里面学习。
                else {

          $("input[myid = " + logotext + "]").show();             }
            }

    如果大家有更好的办法,大家可以给我留言,大家都是学习的哈!如果大家觉得我解释不清楚,可以加QQ 381619650 谢谢。

  • 相关阅读:
    Oracle常用命令大全(很有用,做笔记)
    表格驱动编程在代码中的应用
    mac 利用svn下载远程代码出现Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
    FAILURE: Build failed with an exception.
    There is an internal error in the React performance measurement code.Did not expect componentDidMount timer to start while render timer is still in progress for another instance
    react native TypeError network request failed
    Android向系统相册中插入图片,相册中会出现两张 一样的图片(只是图片大小不一致)
    react-native Unrecognized font family ‘Lonicons’;
    react-native SyntaxError xxxxx/xx.js:Unexpected token (23:24)
    Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.
  • 原文地址:https://www.cnblogs.com/nx520zj/p/2669119.html
Copyright © 2011-2022 走看看